如何禁用安卓中的蓝牙功能?


本示例演示了如何禁用安卓中的蓝牙功能。

第 1 步 − 在 Android Studio 中创建一个新项目,转至文件⇒新建项目,并填写所有必需的信息以创建一个新项目。

第 2 步 − 添加以下代码至 res/layout/activity_main.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:gravity = "center"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
   <TextView
      android:id = "@+id/text"
      android:textSize = "30sp"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent" />
</LinearLayout>

在以上代码中,我们采用文本视图来显示蓝牙状态。

第 3 步 − 添加以下代码至 src/MainActivity.java

package com.example.myapplication;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Build;
import android.os.Bundle;
import android.os.health.SystemHealthManager;
import android.provider.Telephony;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   TextView textView;
   @RequiresApi(api = Build.VERSION_CODES.N)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textView = findViewById(R.id.text);
      textView.setText("disable");
      final BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
      textView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            manager.getAdapter().disable();
         }
      });
   }
   @Override
   protected void onStop() {
      super.onStop();
   }
   @Override
   protected void onResume() {
      super.onResume();
   }
}

我们尝试运行应用程序。假设你已将实际的 Android 移动设备与计算机连接。要通过 android studio 运行应用程序,打开你的一个项目活动文件,然后从工具栏点击运行  图标。选择你的移动设备作为选项,然后查看移动设备,它会显示你的默认屏幕 –

点击 此处 下载项目代码

更新日期:2019 年 7 月 30 日

525 次浏览

开启你的 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.