[英]identify BLE devices on Android / Kotlin with bluetooth off
I am developing a solution to check BLE devices and I used the native API that comes with Android to check BluetoothLeScanner. 我正在开发一种解决方案以检查BLE设备,并且使用Android随附的本机API来检查BluetoothLeScanner。
Wanted to understand a little better operation, I take the location permission and bluetooth. 想了解更好的操作,我选择了位置许可和蓝牙。
After the scan starts, I turn Bluetooth on my phone to off, on Moto G2 Android 6.0
Scan still keeps giving me the expected result when I test on a Samsung S8 Android 9
and Sony Xperia T2 Ultra Android 5.1
in the log I get which was bluetooth disabled and the scan was stopped. 扫描开始后,我关闭了手机上的蓝牙,在
Moto G2 Android 6.0
扫描,当我在获得的日志中对Samsung S8 Android 9
和Sony Xperia T2 Ultra Android 5.1
进行测试时,仍然保持预期的结果蓝牙已禁用,扫描已停止。
I can only perform the test when I purchase it as follows 我只能按以下方式购买时进行测试
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 is bringing the necessary information when bluetooth is online, I already set it up as the image below 蓝牙在线时,scanResult会带来必要的信息,我已经将其设置为下图
https://i.stack.imgur.com/o9jGRm.png https://i.stack.imgur.com/o9jGRm.png
I see that this makes scanning even off 我看到这使扫描均匀
There is no way to detect BLE devices with bluetooth off 无法通过蓝牙关闭来检测BLE设备
Bluetooth must be enabled 必须启用蓝牙
Before your application can communicate over BLE, you need to verify that BLE is supported on the device, and if so, ensure that it is enabled.
在您的应用程序可以通过BLE进行通信之前,您需要验证设备是否支持BLE,如果支持,请确保已启用。
On some Android devices including Pixel phones, Android One devices, and unmodified AOSP builds, turning off bluetooth in the quick settings panel doesn't really turn off bluetooth. 在某些Android设备(包括Pixel手机,Android One设备和未修改的AOSP版本)上,在“快速设置”面板中关闭蓝牙并不能真正关闭蓝牙。 Instead, it merely blocks bluetooth connections and pairing in software, yet allows Bluetooth LE scans to continue unaffected.
取而代之的是,它仅阻止蓝牙连接和软件配对,但允许蓝牙LE扫描继续进行而不会受到影响。 As @Jorgesys correctly notes, it is impossible to detect BLE devices if the Bluetooth radio is really turned off, so let me say again: despite what the quick settings panel says, bluetooth is not necessarily powered off.
正如@Jorgesys正确指出的那样,如果确实关闭了蓝牙无线电,则不可能检测到BLE设备,所以让我再说一遍:尽管快速设置面板说的是,蓝牙并不一定要关闭电源。
On supported devices, this happens only if two things are true: 在受支持的设备上,只有在满足以下两个条件时才会发生这种情况:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.