[英]Android - Get Bluetooth UUID for this device
我正在瀏覽Stack和互聯網,以獲得一個簡單的解決方案來獲取我正在使用的設備的UUID
。 我偶然發現了這樣的帖子,但他們似乎都沒有幫助我。
doc講述了這個 getUuids()
函數,但是在瀏覽Android藍牙的文檔時,我最終得到了一個BluetoothAdapter,但是我需要一個BluetoothDevice
來執行這個功能。
所以我需要知道以下內容:
1)函數是否真的返回設備UUID
? 因為這個名字叫復數(getUuid s
)
2)如何獲得此BluetoothDevice
的實例?
謝謝!
使用反射,您可以在BluetoothAdater
上調用隱藏方法getUuids()
:
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-00805f9b34fb
用於HandsfreeAudioGatewayServiceClass
, 00001105-0000-1000-8000-00805f9b34fb
用於OBEXObjectPushServiceClass
。 此方法的實際可用性可能取決於設備和固件版本。
要實現此目的,您必須定義藍牙權限:
<uses-permission android:name="android.permission.BLUETOOTH"/>
然后你可以使用反射調用方法getUuids()
:
try {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null);
ParcelUuid[] uuids = (ParcelUuid[]) getUuidsMethod.invoke(adapter, null);
if(uuids != null) {
for (ParcelUuid uuid : uuids) {
Log.d(TAG, "UUID: " + uuid.getUuid().toString());
}
}else{
Log.d(TAG, "Uuids not found, be sure to enable Bluetooth!");
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
您必須啟用藍牙才能獲得Uuids。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.