在调用 getUuids() 方法之前,需要先调用 fetchUuidsWithSdp() 方法来获取设备的 UUID 列表。示例代码如下:
BluetoothDevice device = ...; BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
// 发送服务搜索请求,获取设备的 UUID 列表 device.fetchUuidsWithSdp();
// 等待一段时间,等待设备响应服务搜索请求 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
// 获取设备的 UUID 列表 ParcelUuid[] uuids = device.getUuids(); if (uuids != null) { for (ParcelUuid uuid : uuids) { Log.d(TAG, "Device UUID: " + uuid.toString()); } } else { Log.d(TAG, "Device UUIDs are null."); }
需要注意的是,fetchUuidsWithSdp() 方法会发送一个服务搜索请求到设备,该请求需要一定时间才能得到响应。因此,在调用 getUuids() 方法之前,需要等待一段时间让设备响应服务搜索请求。通常情况下,等待时间为 1 秒左右就可以得到设备的 UUID 列表。