我如何获得蓝牙设备的UUID?

时间:2012-03-05 14:13:25

标签: android bluetooth uuid

我需要知道API 8(2.2)或可能2.3.3上的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); 

5 个答案:

答案 0 :(得分:5)

你必须使用反射来在android版本上使用getUuids()和fetchUuidsWithSdp()<所以,试试代码:

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

答案 1 :(得分: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

答案 2 :(得分:1)

不幸的是,我不认为有任何好的方法可以让具有API级别的BluetoothDevice支持UUID< 15.我想这就是他们在API 15中添加新功能的原因。

注意,来自docs for BluetoothClass

  

BluetoothClass可用作粗略描述设备的提示(for   示例在UI中显示图标,但不能可靠地描述   a实际支持哪些蓝牙配置文件或服务   设备。通过SDP请求完成准确的服务发现   在创建RFCOMM套接字时自动执行   createRfcommSocketToServiceRecord(UUID)和   listenUsingRfcommWithServiceRecord(String,UUID)。

因此,在执行列出的某个功能之前,设备类可能会被用作提示哪些服务可用。当然,检查课程并没有什么坏处,因为这不需要任何额外的蓝牙操作。

请注意,服务类也可用(它是设备类的一部分),但这只是一个通用类,而不是特定服务的列表(例如来自SDP)。

答案 3 :(得分:0)

尝试使用BluetoothAdapter类

如有任何问题,请阅读:http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html

答案 4 :(得分: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]) + "-");
            }
        }
    }
相关问题