简体   繁体   中英

onCharacteristicChanged not called with BLE

I've connected a blood pressure device and set notification in onServiceDicovered.

I set notify with every Characteristic. But onCharacteristicChanged is still not called.

public final static UUID CLIENT_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");

gatt.setCharacteristicNotification(characteristic, enabled);
for (BluetoothGattDescriptor dp : characteristic.getDescriptors()) {
    dp = characteristic.getDescriptor(CLIENT_UUID);
    if (dp != null) {
      if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) { 
         descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
      } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) { 
         descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
      }

    gatt.writeDescriptor(dp);
    }
}


@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    super.onCharacteristicChanged(gatt, characteristic);

I want to receive blood pressure, but onCharacteristicChanged never called. But I can receive in ios or other sample code.

Thank you!

First, the property of Blood Pressure Measurement is Indicate , not Notify

https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.blood_pressure.xml

iOS the CBPeripheral setNotifyValue is automatic to set Indicate or Notify , but android not implement.

Give you some code for reference

if (gatt.setCharacteristicNotification(characteristic, true)) {
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
    if (descriptor != null) {
        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        } else {
            // The characteristic does not have NOTIFY or INDICATE property set;
        }

        if (gatt.writeDescriptor(descriptor)) {
            // Success
        } else {
            // Failed to set client characteristic notification;
        }
    } else {
        // Failed to set client characteristic notification;
    }
} else {
    // Failed to register notification;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM