set Characteristic Notification for bluetooth device - Android android.bluetooth

Android examples for android.bluetooth:Bluetooth

Description

set Characteristic Notification for bluetooth device

Demo Code

import java.util.UUID;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.os.Build;

public class Main {

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

  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
  static boolean setCharacteristicNotification(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
      boolean isEnabled) {
    if (characteristic != null) {
      gatt.setCharacteristicNotification(characteristic, isEnabled);
      BluetoothGattDescriptor descriptor = characteristic.getDescriptor(DEVICE_CONFIG_CHARACTERISTIC);
      if (descriptor != null) {
        descriptor.setValue(isEnabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 });
        return gatt.writeDescriptor(descriptor);
      } else// w  w w . j av a 2 s .c  om
        return false;
    } else
      return false;
  }

}

Related Tutorials