android文档指出:
注意:您只能扫描蓝牙 LE 设备或扫描经典蓝牙设备,如蓝牙中所述。您无法同时扫描蓝牙 LE 和经典设备。
但是我注意到调用
mBtAdapter.startDiscovery();
会返回 classic 和 btle 设备。有人知道这里什么是正确的吗?
根据我的理解,文档的意思是你不能同时运行 startLeScan() 和 startDiscovery() 。原因可能是只有一个BluetoothAdapter对象(代表本地蓝牙硬件的对象),因此它不能同时执行使用BluetoothAdapter的两个不同操作。(如果有人知道它在背景,让我们知道)
startLeScan() -> 仅扫描 BLE 设备
startDiscovery() -> 发现所有蓝牙设备,而且它只扫描 12 秒,并且无法更改(请阅读方法说明)
注意: 在找到 BT 设备时执行 startDiscovery() 查询扫描后,您可以获取设备类型来识别每个设备是什么,例如:
int deviceType = device.getType();
if (deviceType == BluetoothDevice.DEVICE_TYPE_CLASSIC) {
} else if (deviceType == BluetoothDevice.DEVICE_TYPE_LE) {
} else if (deviceType == BluetoothDevice.DEVICE_TYPE_DUAL) {
} else if (deviceType == BluetoothDevice.DEVICE_TYPE_UNKNOWN) {
}
device: BluetoothDevice
when (device.type) {
1 -> edt.text = "DEVICE_TYPE_CLASSIC"
2 -> edt.text = "DEVICE_TYPE_LE"
3 -> edt.text = "DEVICE_TYPE_DUAL"
else -> edt.text = "DEVICE_TYPE_UNKNOWN"
}