【问题标题】:Finding out Android Bluetooth LE GATT Profiles找出 Android 蓝牙 LE GATT 配置文件
【发布时间】:2013-09-09 13:23:03
【问题描述】:

我已经实现了一个 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 的设备,但我不知道 Google 是如何获得客户端特征配置的心率测量信息的。

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    蓝牙SIG维护一个“Assigned Numbers”列表,其中包括示例应用中的那些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
    

    【讨论】:

    • 我只想说我已经用 ble 编程了几个星期,但我从未见过任何地方对此进行过解释。我非常感谢您写下这个答案,因为它让我的生活变得无比轻松。
    【解决方案2】:

    除了@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;
                }
            }
    

    【讨论】:

      【解决方案3】:

      我只是想补充几点:

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

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

      【讨论】:

      【解决方案4】:

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

      您需要使用 BluetoothGatt.discoverServices();

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

      【讨论】:

        猜你喜欢
        • 2014-02-08
        • 2014-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-25
        • 1970-01-01
        相关资源
        最近更新 更多