作者:iidt | 来源:互联网 | 2023-10-10 12:06
privateBluetoothGattCharacteristicmNotifyCharacteristic;BluetoothGattServiceservicegatt.ge
private BluetoothGattCharacteristic mNotifyCharacteristic;
BluetoothGattService service = gatt.getService(UUID.fromString(serviceUuid));
mNotifyCharacteristic = service.getCharacteristic(UUID.fromString(characterUuid));
if (mNotifyCharacteristic != null)
{
//使能Notify
setCharacteristicNotification(mNotifyCharacteristic, true);
}
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled)
{
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
// This is specific to BLE SPP Notify.
// if (characterUuid.equals(characteristic.getUuid()))
{
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
1、连接成功后
2、onServicesDiscovered回调中发现服务后,调用setCharacteristicNotification
3、接收通知是在onCharacteristicChanged