更新:对于Android O SDK中的最终API,ScanResult类(自Android 5.0添加)现在具有isConnectable()方法.只能在Android 8.0上检测可连接的广告.有关详细信息,请参阅此处:
https://developer.android.com/reference/android/bluetooth/le/ScanResult.html#isConnectable()
在Android 8.0之前,不幸的是它是不可能的.
可连接的广告由PDU标头字节0确定.您可以在下面的示例结构中看到这一点:
d6 be 89 8e # Access address for advertising data (this is always the same fixed value)
40 # Advertising Channel PDU Header byte 0. Contains: (type = 0), (tx add = 1), (rx add = 0)
24 # Advertising Channel PDU Header byte 1. Contains: (length = total bytes of the advertising payload + 6 bytes for the BLE mac address.)
05 a2 17 6e 3d 71 # Bluetooth Mac
问题出在Anroid 8.0之前的设备上,Android扫描API无法访问这些标头.你在Android 4.x的回调中得到了三个字段:
onLeScan(BluetoothDevice device, rssi, byte[] scan data)
扫描数据字节数组在上述头字节之后开始.从我所看到的BluetoothDevice定义中,没有任何字段或方法告诉您它是否是可连接的广告 – 该类只是蓝牙mac地址的容器,具有在蓝牙堆栈上运行功能的方法.并且IBluetooth.aidl中没有任何方法可以获得此标志,该方法是蓝牙堆栈的私有接口(以及BluetoothDevice调用以获取其信息的内容).
看来这些信息不会从Android 8.0之前的BlueDroid堆栈传递到Java层.