package info.izumin.android.bletia.util;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;

import java.util.Arrays;

import info.izumin.android.bletia.Bletia;

/**
 * Created by izumin on 9/15/15.
 */
public final class NotificationUtils {
    private NotificationUtils() {
        throw new AssertionError("constructor of the utility class should not be called");
    }

    public static BluetoothGattDescriptor getDescriptor(BluetoothGattCharacteristic characteristic, boolean enabled) {
        byte[] value = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(Bletia.CLIENT_CHARCTERISTIC_CONFIG);
        descriptor.setValue(value);
        return descriptor;
    }

    public static boolean isEnableNotificationDescriptor(BluetoothGattDescriptor descriptor) {
        return descriptor.getUuid().equals(Bletia.CLIENT_CHARCTERISTIC_CONFIG)
                && (Arrays.equals(descriptor.getValue(), BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)
                || Arrays.equals(descriptor.getValue(), BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE));
    }
}
@@ -164,16 +164,39 @@
 .Arrays;
+%0Aimport java.util.UUID;
 %0A%0Aimport
@@ -436,32 +436,198 @@
 alled%22);%0A    %7D%0A%0A
+    public static UUID DESCRIPTOR_UUID = Bletia.CLIENT_CHARCTERISTIC_CONFIG;%0A    public static int DESCRIPTOR_PERMISSION = BluetoothGattDescriptor.PERMISSION_WRITE;%0A%0A
     public stati
@@ -866,16 +866,16 @@
 _VALUE;%0A
-
         
@@ -977,16 +977,208 @@
 ONFIG);%0A
+        if (descriptor == null) %7B%0A            descriptor = new BluetoothGattDescriptor(DESCRIPTOR_UUID, DESCRIPTOR_PERMISSION);%0A            characteristic.addDescriptor(descriptor);%0A        %7D%0A
         

Add null guard to NotificationUtils.getDescriptor()