在BLE设备中访问Heart Service返回null

时间:2018-10-25 23:34:47

标签: android bluetooth-lowenergy

我正在尝试从BLE设备(这是一个智能手表,型号为CURREN R5 Pro)读取心率,但是当我获得心率服务时,它返回空值。

这是代码:

class Constants {
    final static UUID HEART_RATE_SERVICE_UUID = convertFromInteger(0x180D);
    final static UUID HEART_RATE_MEASUREMENT_UUID = convertFromInteger(0x2A37);
    final static UUID HEART_RATE_CONTROL_POINT_UUID = convertFromInteger(0x2A39);
    final static UUID CLIENT_CHARACTERISTIC_CONFIG_UUID = convertFromInteger(0x2902);

    private static UUID convertFromInteger(int i) {
        final long MSB = 0x0000000000001000L;
        final long LSB = 0x800000805f9b34fbL;
        long value = i & 0xFFFFFFFF;
        return new UUID(MSB | (value << 32), LSB);
    }
}


private final BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            bluetoothGatt.discoverServices();
            Log.d(TAG, "Connected");
        }
        if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.d(TAG, "Disconnected");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            //getCharacteristic(HEART_RATE_MEASUREMENT_UUID) returns null
            BluetoothGattCharacteristic characteristic = gatt.getService(HEART_RATE_SERVICE_UUID)
                    .getCharacteristic(HEART_RATE_MEASUREMENT_UUID);
            gatt.setCharacteristicNotification(characteristic, true);

            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(descriptor);
        }
    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        BluetoothGattCharacteristic characteristic = gatt.getService(HEART_RATE_SERVICE_UUID)
                .getCharacteristic(HEART_RATE_CONTROL_POINT_UUID);
        characteristic.setValue(DATA_STREAMING_COMMAND);
        gatt.writeCharacteristic(characteristic);
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        if (HEART_RATE_MEASUREMENT_UUID.equals(characteristic.getUuid())) {
            Log.d(TAG, "HEART_RATE_MEASUREMENT_UUID");
            //PROCESS DATA
        }
        if (HEART_RATE_CONTROL_POINT_UUID.equals(characteristic.getUuid())) {
            Log.d(TAG, "HEART_RATE_CONTROL_POINT_UUID");
            //PROCESS DATA
        }
    }
};

但是,唯一能够从设备读取心率的应用是Wearfit。其他应用程序无法从该设备读取心率。

我能获得的服务有:

  • 00001800-0000-1000-8000-00805f9b34fb
  • 00001801-0000-1000-8000-00805f9b34fb
  • 6e400001-b5a3-f393-e0a9-e50e24dcca9e
  • 00001530-1212-efde-1523-785feabcd123
  • 0000fee7-0000-1000-8000-00805f9b34fb

有人遇到过这种情况吗?我的代码有什么问题吗?感谢您的帮助。

0 个答案:

没有答案