我正在尝试扫描 BLE 设备并仅使用我的设备 UUID 过滤结果。
我使用了 here 中的谷歌示例代码
它确实找到了设备并列出了它们。
但是当我尝试按 UUID 进行过滤时,方法 device.getUuids() 始终返回 null,而不是返回包含设备 UUID 的有效数组。
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
List<String> uuids = new ArrayList<String>();
uuids.add("MY_VALID_UID");
ParcelUuid[] deviceUuids = device.getUuids();
// this if is never exeuted device deviceUuids is always null.
if (deviceUuids != null) {
for (ParcelUuid uuid : device.getUuids()) {
if (uuids.contains(uuid.toString())) {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
}
}
}
});
}
};
我还尝试将我的 uuid 作为过滤器传递给 startLEScan 方法,如下所示,但没有返回结果。
String s = "MY_UUID";
String s2 = s.replace("-", "");
UUID uuid = new UUID(
new BigInteger(s2.substring(0, 16), 16).longValue(),
new BigInteger(s2.substring(16), 16).longValue());
System.out.println(uuid);
UUID[] uuids = {uuid};
mBluetoothAdapter.startLeScan(uuids, mLeScanCallback);
或者
UUID.fromString("MY UUID");
一个
最佳答案
中央应用程序可以通过两种方式了解信标支持的服务 UUID。
一种方法是从信标发出的广告数据包中提取信息。但如果信标不发出此类数据包(例如 AD 类型为 0x03 的数据包),则中央应用程序无法通过这种方式获取有关服务 UUID 的信息。
另一种方法是连接目标信标并明确要求有关信标支持的服务 UUID 的信息。
This method does not start a service discovery procedure to retrieve the UUIDs from the remote device. Instead, the local cached copy of the service UUIDs are returned.
因此,(1) 如果信标不发出包含有关服务 UUID 信息的数据包,并且 (2) 如果您不要求信标返回有关服务 UUID 的信息(例如,如果您没有执行“服务发现”) , getUuids() 返回 null。
关于java - 安卓 BLE。 device.getUUids() 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32250054/