如何停用安卓中的藍牙功能?


本示例演示瞭如何停用安卓中的藍牙功能。

第 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.