簡體   English   中英

禁用藍牙識別Android / Kotlin上的BLE設備

[英]identify BLE devices on Android / Kotlin with bluetooth off

我正在開發一種解決方案以檢查BLE設備,並且使用Android隨附的本機API來檢查BluetoothLeScanner。

想了解更好的操作,我選擇了位置許可和藍牙。

掃描開始后,我關閉了手機上的藍牙,在Moto G2 Android 6.0掃描,當我在獲得的日志中對Samsung S8 Android 9Sony Xperia T2 Ultra Android 5.1進行測試時,仍然保持預期的結果藍牙已禁用,掃描已停止。

我只能按以下方式購買時進行測試

 bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
 bluetoothAdapter = bluetoothManager.adapter

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            bluetoothScanner = bluetoothAdapter.bluetoothLeScanner
        }

@TargetApi(Build.VERSION_CODES.M)
    class BleScanCallback(resultMap: MutableMap) : ScanCallback() {

var resultOfScan = resultMap @RequiresApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.M) override fun onScanResult(callbackType: Int, result: ScanResult?) { addScanResult(result) Log.v("Main Activity", "I found a ble device ${result}") Log.v("Main Activity", "I found a ble device ${result?.device?.address}") } override fun onBatchScanResults(results: MutableList<ScanResult>?) { results?.forEach { result -> addScanResult(result) } } override fun onScanFailed(errorCode: Int) { Log.v("Main Activity","Bluetooth LE scan failed. Error code: $errorCode") } fun addScanResult(scanResult: ScanResult?) { val bleDevice = scanResult?.device val deviceAddress = bleDevice?.address resultOfScan.put(deviceAddress, bleDevice) }

藍牙在線時,scanResult會帶來必要的信息,我已經將其設置為下圖

https://i.stack.imgur.com/o9jGRm.png

我看到這使掃描均勻

無法通過藍牙關閉來檢測BLE設備

必須啟用藍牙

設置BLE

在您的應用程序可以通過BLE進行通信之前,您需要驗證設備是否支持BLE,如果支持,請確保已啟用。

在某些Android設備(包括Pixel手機,Android One設備和未修改的AOSP版本)上,在“快速設置”面板中關閉藍牙並不能真正關閉藍牙。 取而代之的是,它僅阻止藍牙連接和軟件配對,但允許藍牙LE掃描繼續進行而不會受到影響。 正如@Jorgesys正確指出的那樣,如果確實關閉了藍牙無線電,則不可能檢測到BLE設備,所以讓我再說一遍:盡管快速設置面板說的是,藍牙並不一定要關閉電源。

在受支持的設備上,只有在滿足以下兩個條件時才會發生這種情況:

  1. 在完整設置菜單中打開了藍牙(在Android 9上:設置->已連接設備->連接偏好設置->藍牙打開)
  2. 用戶已選擇“即使關閉了藍牙,也允許應用和服務隨時掃描附近的設備。例如,可用於改善基於位置的功能和服務。” (設置->安全和位置->位置->高級->掃描->藍牙掃描打開)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM