找出Android蓝牙LE GATT配置文件

N0x*_*xus 16 android bluetooth

我已经实现了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;
}
Run Code Online (Sandbox Code Playgroud)

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

God*_*uke 66

Bluetooth SIG维护一个"已分配号码 " 列表,其中包括示例应用程序中的UUID:https: //www.bluetooth.org/en-us/specification/assigned-numbers

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

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

0000XXXX-0000-1000-8000-00805f9b34fb
Run Code Online (Sandbox Code Playgroud)

心率测量特征UUID的指定编号列为0x2A37,示例代码的开发人员可以如何到达:

00002a37-0000-1000-8000-00805f9b34fb
Run Code Online (Sandbox Code Playgroud)

  • 我只是想说我花了几个星期的时间编程,而且从未在任何我见过的地方解释过.我非常感谢你写这个答案,因为它让我的生活变得无比轻松. (8认同)

BoD*_*BoD 5

除了@Godfrey Duke的回答,这里有一个方法我用来提取UUID的重要部分:

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

用法示例:

    // 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;
            }
        }
Run Code Online (Sandbox Code Playgroud)


jne*_*ewt -3

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

您需要使用BluetoothGatt.discoverServices();

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