17

我已经实现了查找心率监视器并连接到它的 Android LE 蓝牙示例。然而,这个例子有一个类来定义 GATT 配置文件,如下所示:

 private static HashMap<String, String> attributes = new HashMap();
public static String HEART_RATE_MEASUREMENT = "00002a37-0000-1000-8000-00805f9b34fb";
public static String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";

static {
    // Sample Services.
    attributes.put("0000180d-0000-1000-8000-00805f9b34fb", "Heart Rate Service");
    attributes.put("0000180a-0000-1000-8000-00805f9b34fb", "Device Information Service");
    // Sample Characteristics.
    attributes.put(HEART_RATE_MEASUREMENT, "Heart Rate Measurement");
    attributes.put("00002a29-0000-1000-8000-00805f9b34fb", "Manufacturer Name String");
}

public static String lookup(String uuid, String defaultName) {
    String name = attributes.get(uuid);
    return name == null ? defaultName : name;
}

现在,我想要做的是更改它,以便该程序找到任何带有蓝牙 le 的设备,但我不知道谷歌如何获得客户端特征配置的心率测量信息。

4

4 回答 4

79

蓝牙SIG维护一个“分配号码”列表,其中包括示例应用程序中的 那些UUID : https ://www.bluetooth.com/specifications/assigned-numbers/

尽管 UUID 的长度为 128 位,但为蓝牙 LE 分配的数字被列为 16 位十六进制值,因为低 96 位在一类属性中是一致的。

例如,所有 BLE 特征UUID的形式为:

0000XXXX-0000-1000-8000-00805f9b34fb

为心率测量特征 UUID 分配的编号列为0x2A37,示例代码的开发人员可以这样得出:

00002a37-0000-1000-8000-00805f9b34fb
于 2013-11-23T08:17:06.697 回答
7

除了@Godfrey Duke 的回答,这是我用来提取 UUID 有效位的一种方法:

private static int getAssignedNumber(UUID uuid) {
    // Keep only the significant bits of the UUID
    return (int) ((uuid.getMostSignificantBits() & 0x0000FFFF00000000L) >> 32);
}

使用示例:

    // See https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml
    private static final int GATT_SERVICE_HEART_RATE = 0x180D;

    (...)

        for (BluetoothGattService service : services) {
            if (getAssignedNumber(service.getUuid()) == GATT_SERVICE_HEART_RATE) {
                // Found heart rate service
                onHeartRateServiceFound(service);
                found = true;
                break;
            }
        }
于 2014-05-31T13:47:18.800 回答
-1

我只是想添加一些东西:

  1. 0000-1000-8000-00805f9b34fb不是蓝牙 UUID 的唯一根源。
  2. *-0002a5d5c51b也很常见(见https://uuid.pirate-server.com/search?q=0002a5d5c51b
  3. anything可以使用,并且已经使用:

  4. 随意使用您想使用的任何东西,但如果您非常关心安全性,请注意使用 UUIDv1 UUID,因为它们嵌入了时间戳和 mac 地址,可以在某种程度上用于跟踪您,即使您使用在线生成器,有关使用https://www.famkruithof.net/uuid/uuidgen?typeReq=-1作为生成器的 UUID 的所有示例 ,请参见https://uuid.pirate-server.com/search?q=0800200c9a66

于 2019-01-13T23:01:10.430 回答
-3

这是有关 gatt 回调的页面:https ://developer.android.com/reference/android/bluetooth/BluetoothGatt.html

您需要使用 BluetoothGatt.discoverServices();

然后在回调 onServicesDiscovered(...) 我认为你需要使用BluetoothGatt.getServices();

于 2013-09-28T18:39:45.357 回答