本文整理汇总了Java中android.bluetooth.BluetoothGattDescriptor.ENABLE_INDICATION_VALUE属性的典型用法代码示例。如果您正苦于以下问题:Java BluetoothGattDescriptor.ENABLE_INDICATION_VALUE属性的具体用法?Java BluetoothGattDescriptor.ENABLE_INDICATION_VALUE怎么用?Java BluetoothGattDescriptor.ENABLE_INDICATION_VALUE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.bluetooth.BluetoothGattDescriptor
的用法示例。
在下文中一共展示了BluetoothGattDescriptor.ENABLE_INDICATION_VALUE属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setCharacteristicNotification
/**
* Enables or disables notification on a give characteristic.
*
* @param characteristic Characteristic to act on.
* @param enabled If true, enable notification. False otherwise.
*/
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
Log.d(TAG,"Notification set for " + characteristic.getUuid().toString() + " : " + String.valueOf(enabled));
// step 1 - enable notifications locally
boolean success = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
if(!success) {
Log.e("------", "Seting proper notification status for characteristic failed!");
}
//step 2 - write to Clienc Characteristic Config Descriptor (2902) proper values
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if(descriptor != null) {
// byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
descriptor.setValue(val);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
示例2: start
@Override
protected void start(Connection connection, BluetoothGatt gatt) {
if (type == Type.UNSUBSCRIBE && connection.getCharacteristicsChangedListenerCount(characteristicUUID) > 0) {
NeatleLogger.d("Won't unsubscribe on " + characteristicUUID + " since it has registered listeners");
finish(CommandResult.createEmptySuccess(characteristicUUID));
return;
}
BluetoothGattService service = gatt.getService(serviceUUID);
if (service == null) {
finish(CommandResult.createErrorResult(characteristicUUID, SERVICE_NOT_FOUND));
return;
}
BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID);
if (characteristic == null) {
finish(CommandResult.createErrorResult(characteristicUUID, CHARACTERISTIC_NOT_FOUND));
return;
}
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
if (descriptor == null) {
finish(CommandResult.createErrorResult(characteristicUUID, DESCRIPTOR_NOT_FOUND));
return;
}
boolean turnOn;
byte[] valueToWrite;
switch (type) {
case Type.SUBSCRIBE_INDICATION:
NeatleLogger.d("Subscribing to indications on " + characteristicUUID);
valueToWrite = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
turnOn = true;
break;
case Type.SUBSCRIBE_NOTIFICATION:
NeatleLogger.d("Subscribing to notifications on " + characteristicUUID);
valueToWrite = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
turnOn = true;
break;
case Type.UNSUBSCRIBE:
NeatleLogger.d("Unsubscribing from notifications/indications on " + characteristicUUID);
valueToWrite = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
turnOn = false;
break;
default:
throw new IllegalStateException();
}
byte[] descriptorValue = descriptor.getValue();
if (Arrays.equals(descriptorValue, valueToWrite)) {
NeatleLogger.d("No subscription changes needed - is at " + valueToWrite[0] + " on " + characteristicUUID);
finish(CommandResult.createEmptySuccess(characteristicUUID));
return;
}
if (!gatt.setCharacteristicNotification(characteristic, turnOn)) {
NeatleLogger.e("Failed to change characteristics notification flag on " + characteristicUUID);
finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE));
return;
}
descriptor.setValue(valueToWrite);
NeatleLogger.e("Writing descriptor on " + characteristicUUID);
if (!gatt.writeDescriptor(descriptor)) {
NeatleLogger.e("Failed to write descriptor on " + characteristicUUID);
finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE));
}
}