Проблема в том, что я могу получить данные, включив уведомление об этой характеристике даже без вызова readCharacteristic. Но я не могу успешно прочитать характеристику, вызвав readCharacteristic без включения уведомления.
Код: Выделить всё
mBluetoothGatt.readCharacteristic(characteristic)
Есть ли у кого-нибудь идеи о том, что здесь происходит? Мне действительно нужно прочитать характеристику отдельно. Потому что если я анализирую данные только на основе уведомлений, я не могу понять, где начинаются данные. Это потому, что мое устройство каждый раз отправляет 12 байт. И он будет постоянно отправлять массив байтов. Однако уведомление приносило мне данные по одному байту за раз. Поэтому я не знаю, какой байт является начальным в массиве байтов.
Сейчас я использую пример кода, предоставленный Android.
Вот фрагмент:
Код: Выделить всё
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
boolean status = mBluetoothGatt.readCharacteristic(characteristic);
System.out.println("Initialize reading process status:" + status);
}
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
// This is specific to Heart Rate Measurement.
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
Код: Выделить всё
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
System.out.println("In onCharacteristicRead!!!!!!!");
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
System.out.println("Received Data Success!!!!!!");
}
}
Подробнее здесь: https://stackoverflow.com/questions/226 ... ndroid-ble