繁体   English   中英

onCharacteristicChanged没有用BLE调用

[英]onCharacteristicChanged not called with BLE

我已连接血压设备并在onServiceDicovered中设置通知。

我为每个特征设置了通知。 但onCharacteristicChanged仍未调用。

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);

我想接受血压,但是特征变化从未打过电话。 但我可以用ios或其他示例代码接收。

谢谢!

首先, Blood Pressure Measurement的属性是Indicate ,而不是Notify

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

iOS的CBPeripheral setNotifyValue是自动设置IndicateNotify ,但android没有实现。

给你一些代码供参考

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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