2012-03-05 2802 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