Android 蓝牙:获取已发现设备的 UUID [英] Android Bluetooth: Get UUIDs of discovered devices
问题描述
由于我目前正在为 Android 开发一个小型蓝牙库,我正在尝试获取我在周围发现的设备的所有服务 uuid.
As I'm currently working on a little bluetooth library for Android, I'm trying to get all the service uuids of the devices I discovered in my surrounding.
当我的广播接收器获得 BluetoothDevice.ACTION_FOUND
意图时,我正在提取设备并调用:
When my broadcast receiver gets the BluetoothDevice.ACTION_FOUND
intent, I'm extracting the device and call:
device.fetchUuidsWithSdp();
这将为找到的每个设备产生 BluetoothDevice.ACTION_UUID
意图,我正在使用相同的接收器处理它们:
This will result BluetoothDevice.ACTION_UUID
intents for each device found and I'm handling them with the same receiver:
BluetoothDevice d = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
if(uuidExtra == null) {
Log.e(TAG, "UUID = null");
}
if(d != null && uuidExtra != null)
Log.d(TAG, d.getName() + ": " + uuidExtra.toString());
问题是,uuidExtra
总是 null
.
如何获取周围设备的所有 UUID?
How can i get all the UUIDs of the surrounding devices?
我在 Nexus 7 上工作.我尝试了在互联网上找到的代码,这也给了我一个 NullPointerException:http://digitalhacksblog.blogspot.de/2012/05/android-example-bluetooth-discover-and.html
Im working on a Nexus 7. I tried code i found on the internet and this also gives me a NullPointerException: http://digitalhacksblog.blogspot.de/2012/05/android-example-bluetooth-discover-and.html
谢谢.
推荐答案
文档 在这个状态...
总是包含额外的字段BluetoothDevice.EXTRA_UUID
然而,就像你一样,我发现这不是真的.
However, just like you, I have found this not to be true.
如果您在设备发现过程中调用 fetchUuidsWithSdp()
,BluetoothDevice.EXTRA_UUID
可以为 null.
If you call fetchUuidsWithSdp()
while device discovery is still taking place BluetoothDevice.EXTRA_UUID
can be null.
您应该等到收到 BluetoothAdapter.ACTION_DISCOVERY_FINISHED
后再调用 fetchUuidsWithSdp()
.
You should wait until you receive BluetoothAdapter.ACTION_DISCOVERY_FINISHED
before you make any calls to fetchUuidsWithSdp()
.
这篇关于Android 蓝牙:获取已发现设备的 UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!