package com.astoev.cave.survey.service.bluetooth.lecommands;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothGatt;
import android.os.Build;
import android.util.Log;

import java.util.UUID;

import static com.astoev.cave.survey.Constants.LOG_TAG_BT;

public class WriteCharacteristicCommand extends AbstractBluetoothCommand {

    private UUID service;
    private UUID characteristic;
    private byte[] value;


    public WriteCharacteristicCommand(UUID aService, UUID aCharacteristic, byte[] aValue) {
        service = aService;
        characteristic = aCharacteristic;
        value = aValue;
    }


    @Override
    public boolean canProceedWithoutAnswer() {
        return true;
    }

    @Override
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public void execute(BluetoothGatt aBluetoothGatt) {
        Log.d(LOG_TAG_BT, "Send to " + service + ":" + characteristic + " : "  + new String(value));
        boolean success = aBluetoothGatt.getService(service).getCharacteristic(characteristic).setValue(value);
        if (!success) {
            Log.e(LOG_TAG_BT, "Failed to set characteristic value");
        }
    }
}

@@ -132,16 +132,70 @@
 thGatt;%0A
+import android.bluetooth.BluetoothGattCharacteristic;%0A
 import a
@@ -1001,23 +1001,46 @@
     
-boolean success
+BluetoothGattCharacteristic targetChar
  = a
@@ -1110,26 +1110,156 @@
 tic)
-.setValue(value);%0A
+;%0A        if (targetChar != null) %7B%0A            boolean success = targetChar.setValue(value) && aBluetoothGatt.writeCharacteristic(targetChar);%0A    
     
@@ -1274,24 +1274,28 @@
 !success) %7B%0A
+    
             
@@ -1351,16 +1351,91 @@
 alue%22);%0A
+            %7D%0A        %7D else %7B%0A            Log.e(LOG_TAG_BT, %22Not found%22);%0A
         

fix sending ble commands