Я пытался прочитать значения ЧСС из моего Xiaomi Band 2. Для этого я попытался использовать и адаптировать проект BluetoothLeGatt из https://github.com/android/connectivity-samples/tree/master/BluetoothLeGatt.
До сих пор мне удавалось исследовать устройства BLE. рядом, поблизости. После этого я выбрал Xiaomi Band 2 и смог успешно перечислить все сервисы, предоставляемые этим устройством, включая Heart Rate Service. Внутри службы сердечного ритма Я нашел характеристику измерения сердечного ритма, которую я искал.
Я попытался напечатать данные из этой характеристики, но безуспешно. Мне не удалось увидеть какие-либо данные в моем журнале, хотя я и внес небольшие изменения.
Это мой LEScanCallback (с небольшими изменениями)
// Implements callback methods for GATT events that the app cares about. For example,
// connection change and services discovered.
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
BluetoothGattCharacteristic characteristic =
gatt.getService(HEART_RATE_SERVICE_UUID)
.getCharacteristic(HEART_RATE_MEASUREMENT_CHAR_UUID);
System.out.println("HEART_RATE_MEASUREMENT_CHAR_UUID: "+HEART_RATE_MEASUREMENT_CHAR_UUID);
/*gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
gatt.writeDescriptor(descriptor); */
setCharacteristicNotification(characteristic,true);
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
System.out.println("onCharacteristicRead: " + Arrays.toString(characteristic.getValue()));
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status){
super.onDescriptorWrite(gatt, descriptor, status);
System.out.println("onDescriptorWrite");
BluetoothGattCharacteristic characteristic = gatt.getService(HEART_RATE_SERVICE_UUID)
.getCharacteristic(HEART_RATE_MEASUREMENT_CHAR_UUID);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
characteristic.setValue(new byte[]{1, 1});
//gatt.writeCharacteristic(characteristic);
boolean success = gatt.readCharacteristic(characteristic);
System.out.println("success? "+success);
System.out.println("status: "+status);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
System.out.println("onCharacteristicChanged: " + Arrays.toString(characteristic.getValue()));
}
};
Я не получаюдо точки onCharacteristicRead () или onCharacteristicChanged () вызывается. Я полагаю, это потому, что gatt.readCharacteristic (характеристика) имеет значение ЛОЖЬ, а состояние равно 3 (GATT_WRITE_NOT_PERMITTED) в функции onDescriptorWrite ().
Может кто-нибудь помочь мне разобраться в проблеме? Я начинаю терять надежду ...