繁体   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