1. 程式人生 > >Android Bluetooth開發彙總

Android Bluetooth開發彙總

Android開啟藍芽開關

[Android:Bluetooth 的開啟和關閉]

檢查系統藍芽是否開啟

BluetoothManager bluetoothManager = (BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
mBluetoothAdapter.isEnabled();

開啟系統藍芽方式:

靜默開啟:
  • 註冊許可權:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

  • 開啟方式:
mBluetoothAdapter.enable();

以上靜默開啟 Bluetooth 開關是呼叫了 BluetoothAdapter.enable() 方法,首先需要獲取 BluetoothAdapter 物件,如果這個物件為 null 的話,說明當前裝置不支援 Bluetooth 功能。還有以下幾點需要注意: 1, 在 Nexus 5 Android 4.4.4 原生系統中,在沒有任何其它管理 Bluetooth 許可權的應用情況下,呼叫強制開啟 Bluetooth 的方法,沒有任何提示就直接開啟 Bluetooth 了。 2,在小米手機 MI 2SC / MIUI-4.7.11 (Android 4.1.1 JRO03L) 上系統自帶的 “安全中心” – “應用許可權管理” – “開啟藍芽” 中,有三種設定: 允許:呼叫強制開啟 Bluetooth 程式碼,沒有任何提示,Bluetooth 被成功開啟。 提示:會彈出提示框,提示安全警告 “ ***應用嘗試開啟藍芽”,可以選擇“拒絕”或“允許”,還有記住此次選擇(備註:如果不記住的話,下次還會彈出同樣的提示框,除非你自己去修改了應用開啟藍芽的許可權)。 拒絕:呼叫強制開啟 Bluetooth 程式碼,沒有任何提示,Bluetooth 強制開啟失敗。 備註:各種手機自帶的許可權管理功能或者第三方許可權管理應用略有不同。

3,對於 BluetoothAdapter.enable() 這個方法,API 中有以下說明 (備註:初始 API 中,如 Android 2.0 Eclair / API Level 5 中並沒有這段提示) Bluetooth should never be enabled without direct user consent. If you want to turn on Bluetooth in order to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that requests user permission to turn on Bluetooth. The enable() method is provided only for applications that include a user interface for changing system settings, such as a “power manager” app. 沒有直接的使用者的允許絕不要開啟 Bluetooth。如果你想要開啟 Bluetooth 建立一個無線連線,你應當使用 ACTION_REQUEST_ENABLE Intent,這樣會彈出一個提示框提示使用者是否開啟 Bluetooth,enable() 方法僅提供給有 UI 、更改系統設定的應用來使用,例如“電源管理”應用。 從以上官方 API 提示可以看出:不建議你呼叫此方法來開啟 Bluetooth,至少是在沒有任何使用者提醒的前提下!

呼叫系統彈出框提示使用者開啟
  • 註冊許可權:
<uses-permission android:name="android.permission.BLUETOOTH" />
  • 開啟方式:
Intent requestBluetoothOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
this.startActivityForResult(requestBluetoothOn, REQUEST_CODE_BLUETOOTH_ON);

對於以上彈出系統彈框提示使用者開啟 Bluetooth 的程式碼,有以下幾點需要注意: 1,這種呼叫系統的彈出框提示使用者開啟 Bluetooth 的方式,一般不會受到系統或者第三方許可權管理應用的阻止。只有當你不提示使用者的情況下,可以理解為“偷偷摸摸”的開啟 Bluetooth ,這個是被認為侵犯使用者的知情權,系統或者第三方許可權管理應用可能加以阻止:直接禁止不提示使用者的情況下開啟 Bluetooth,或者提示使用者,又或者是讓使用者自己選擇哪些應用可以強制開啟 Bluetooth。而在 Nexus 5 / Android 4.4.4 原生系統強制開啟 Bluetooth 是沒有任何提示,並且可以成功開啟。 2,彈出系統的提示框提醒使用者開啟 Bluetooth 的主要程式碼: this.startActivityForResult(requestBluetoothOn, REQUEST_CODE_BLUETOOTH_ON); 注意:這個方法是需要 Activity 的物件來呼叫的!並且需要在 Activity 中重寫 onActivityResult 方法來獲取使用者操作彈出提示框的結果! 3,這種彈出的系統彈框,根據系統的不同,UI 會有所不同。會導致使用者app視覺不統一。

跳轉到系統設定中讓使用者自己開啟:
startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));

考慮到涉及使用者隱私和使用者體驗,推薦以下方式開啟 Bluetooth : 1,採用強制開啟 Bluetooth 的方式開啟 Bluetooth ,但是呼叫強制開啟 Bluetooth 程式碼之前,我們自己在應用中提示使用者,我們的應用需要開啟 Bluetooth ,讓使用者自己選擇是否開啟 Bluetooth 。自己在應用中提示使用者我們需要開啟 Bluetooth 相對於彈出系統的提示框提示使用者當前應用需要開啟 Bluetooth 的優勢在於我們可以控制提示的內容和提示的方式以及 UI。 2,假若使用者選擇了開啟 Bluetooth,但是強制開啟 Bluetooth 失敗,比如系統自帶的許可權管理禁止你的應用開啟 Bluetooth ,我們不去提示使用者說當前系統禁止了應用開啟 Bluetooth,讓使用者自己去解除禁止。這樣顯然使用者體驗很差。這種情況下,我們再去呼叫彈出系統提示框提醒使用者開啟 Bluetooth 即可。這種方式一般系統或者第三方應用不會禁止。 3,如果彈出系統提示框提醒使用者開啟 Bluetooth 有問題的話,最後採用提示使用者自己去系統 Bluetooth 設定中開啟 Bluetooth,跳轉到系統的 Bluetooth 設定介面。