Android蓝牙。 device.getUUids() 总是 return 空

Android BLE. device.getUUids() always return null

我正在尝试扫描 BLE 设备并仅使用我的设备 UUID 过滤结果。

我使用了 here

中的 google 示例代码

它确实找到了设备并列出它们。

但是当我尝试通过我的 UUID 进行过滤时,方法 device.getUuids() 总是 return null 而不是 return 使用设备的 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 方法,但没有结果被 returned。

        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 的信息。

JavaDoc of getUuids()方法如下:

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) 如果您不需要信标 return 有关服务 UUID 的信息(例如,如果您没有执行 'service discovery'), getUuids() returns null.