2012-03-05 158 views
5

我需要知道API 8(2.2)或可能的2.3.3上的UUID。如何獲取藍牙設備的UUID?

據我瞭解的文件,這應該被允許:

phoneDevice = blueAdapter.getRemoteDevice(phoneAddress); 
    ParcelUuid[] phoneUuids = phoneDevice.getUuids(); // Won't compile 

的Eclipse給我: 「的方法getUuids()是未定義的類型BluetoothDevice類」 但見: http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getUuids()

另外,我想知道怎樣的UUID被 「瓜分」 的ParcelUuid []內。如果我設法到達那裏,我如何從parcelUuid []中檢索UUID? Android藍牙的文檔似乎很差,在我看來。

真是個笑話! 現在,我嘗試從意圖得到它,但是這也給了:*「EXTRA_UUID不能得到解決或不是場」 *:

intent.getParcelableExtra(BluetoothDevice.EXTRA_UUID); 
+0

這將支持API級別15.您的API級別是多少? – 2012-03-05 14:25:11

+0

嗨,面臨同樣的問題,在我的項目中提供了從android 2.3.1,min api level 8的支持。請大家幫忙。 – 2014-03-21 11:43:58

+0

嗨。很長時間過去了這個職位,但我現在面臨這個完全相同的問題。你有什麼解決方法嗎(我的最小API是10)?謝謝你的任何線索 – stefat 2015-05-07 10:42:41

回答

2

//這會從API級別15以上支持。

Broadcast Action: This intent is used to broadcast the UUID wrapped as a ParcelUuid of the remote device after it has been fetched. This intent is sent only when the UUIDs of the remote device are requested to be fetched using Service Discovery Protocol 
    Always contains the extra field EXTRA_DEVICE 
    Always contains the extra field EXTRA_UUID 
    Requires BLUETOOTH to receive. 
    Constant Value: "android.bluetooth.device.action.UUID" 

//無法降級其硬件相關。也沒有支撐罐。 http://developer.android.com/sdk/compatibility-library.html

+0

好吧,但API 15是4.0.3發佈一個月前。我需要在2.2 API 8上使用藍牙! – Tombola 2012-03-05 14:31:29

+0

那麼,實現它的方法是創建UUID?這不是硬件的屬性,它只是我編制的一個數字,對嗎? – Tombola 2012-03-05 14:44:29

+0

@Tombola你能夠得到uuid的android 2.2連接另一個設備?在這裏發佈您的解決方案 – 2012-12-18 04:33:42

1

不幸的是,我不認爲有什麼好辦法來獲得UUID的由BluetoothDevice類與API級別支持< 15.我猜這就是爲什麼他們在API添加的新功能15

注意,從docs for BluetoothClass

BluetoothClass可用於粗略描述設備(用於在UI中顯示圖標的 示例),但不能可靠地描述 設備實際支持哪些藍牙配置文件或服務。準確的服務發現是通過SDP請求完成的,當創建帶有 createRfcommSocketToServiceRecord(UUID)和 listenUsingRfcommWithServiceRecord(String,UUID)的RFCOMM套接字時,將自動執行 。

因此,也許設備類可以用作提示哪些服務將可用,直到您執行列出的功能之一。當然,檢查課程並沒有什麼不好,因爲這不需要任何額外的藍牙操作。

請注意,服務類也是可用的(它是設備類的一部分),但這只是一個普通的類,而不是特定服務的列表(如來自SDP)。

5

你必須使用反射來對Android版本使用getUuids()和fetchUuidsWithSdp()< 3.所以,儘量代碼:

Method method = phoneDevice.getClass().getMethod("getUuids", null); 
ParcelUuid[] phoneUuids = (ParcelUuid[]) method.invoke(phoneDevice, null); 
0

如果你不能從getUuids()方法獲取UUID。請嘗試另一種方式。

掃描成功後,您應該收到byte[](scanRecord),所以從這個結果來看,如果您能識別UUID format,您可以一步一步分割以獲取正確的UUID作爲這些代碼。

P/s:重要的是,你應該知道UUID format正確地得到索引。

// Put item into hash map 
    // UUID from index 10 to 24 : 12233445566778899aabbccddeeff0 
    StringBuilder mSbUUID = new StringBuilder(); 
    for (int i = 0; i < scanRecord.length; i++) { 
     // UUID 
     if (i >= 10 & i <= 24) { 
      if (Integer.toHexString(
        scanRecord[i]).contains("ffffff")) { 
       mSbUUID.append(Integer.toHexString(scanRecord[i]).replace("ffffff", "") + "-"); 
      } else { 
       mSbUUID.append(Integer.toHexString(scanRecord[i]) + "-"); 
      } 
     } 
    } 
+0

這個問題來自'12,而ScanRecord是較新的藍牙LE API的一部分。此問題適用於Bluetooth Classic。 – jschlepp 2016-01-12 06:34:11