2013-10-28 180 views
14

我正在浏览Stack和互联网,获取我正在使用的设备的一个简单解决方案UUID。我偶然发现了posts like this,但他们似乎都没有帮助我。Android - 获取此设备的蓝牙UUID

的文档告诉我about thisgetUuids()功能,但通过DOC去为Android Bluetooth当我最终有BluetoothAdapter,但我需要一个BluetoothDevice执行此功能。

所以,我需要了解以下内容:

1)函数返回真正的设备UUID?因为名称表示复数(getUuid s

2)如何获得此BluetoothDevice的实例?

谢谢!

+0

无论如何,你怎么知道UUID是你的蓝牙设备@Ron? – gumuruh

回答

15

使用反射,你可以调用隐藏方法getUuids()BluetoothAdater

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 

Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null); 

ParcelUuid[] uuids = (ParcelUuid[]) getUuidsMethod.invoke(adapter, null); 

for (ParcelUuid uuid: uuids) { 
    Log.d(TAG, "UUID: " + uuid.getUuid().toString()); 
} 

这是在Nexus S的结果:

UUID: 00001000-0000-1000-8000-00805f9b34fb 
UUID: 00001001-0000-1000-8000-00805f9b34fb 
UUID: 00001200-0000-1000-8000-00805f9b34fb 
UUID: 0000110a-0000-1000-8000-00805f9b34fb 
UUID: 0000110c-0000-1000-8000-00805f9b34fb 
UUID: 00001112-0000-1000-8000-00805f9b34fb 
UUID: 00001105-0000-1000-8000-00805f9b34fb 
UUID: 0000111f-0000-1000-8000-00805f9b34fb 
UUID: 0000112f-0000-1000-8000-00805f9b34fb 
UUID: 00001116-0000-1000-8000-00805f9b34fb 

,其中例如,0000111f-0000-1000-8000-00805f9b34fbHandsfreeAudioGatewayServiceClass00001105-0000-1000-8000-00805f9b34fb用于OBEXObjectPushServiceClass。此方法的实际可用性可能取决于设备和固件版本。

+0

我使用SDK16,它不适合我。 'uuids'是'null'。此外,我想只有我的蓝牙uuid。你知道如何得到这个吗? – Ron

+0

设备暴露的每个蓝牙服务都有一个uuid;这就是为什么你会看到智能手机的多个uuids。我使用Android 4.1.2的Nexus S上的SDK和Android 4.3上的Galaxy Nexus执行测试。你使用哪种设备? –

+0

Galaxy S2 ...我是否需要启动蓝牙才能找回我的uuid? – Ron