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