0

I have a BLE device prototype. I am able to scan the device, connect to it. I am also able to write its password as BLE characteristic . Password is in String. But when I am trying to write other values which are in hex it is giving me write unsuccessful error with status 4 and status 13 at different times ? The same thing is working with nRF application. Where am I going wrong. Below is my code.

  public boolean writeCharacteristic(BluetoothGatt mBluetoothGatt) {

    //check mBluetoothGatt is available
    if (mBluetoothGatt == null) {
        Log.e("++++", "lost connection");
        return false;
    }
    String SERVICE_STRING = "3fc2d576-0249-11e7-93ae-----------";
    UUID SERVICE_UUID = UUID.fromString(SERVICE_STRING);
    BluetoothGattService Service = mBluetoothGatt.getService(SERVICE_UUID);
    if (Service == null) {
        Log.e("++++++", "service not found!");
        return false;
    }

    BluetoothGattCharacteristic charac = Service
            .getCharacteristic(UUID.fromString("3fc2d576-0249-11e7-93ae-------------"));
    if (charac == null) {
        Log.e("+++", "char not found!");
        return false;
    }
    String mValue = "0x01";
    byte[] value = mValue.getBytes();
    //value[0] = (byte) (0x00);
    String pwd = "WWW";
   // byte[] value = mValue.getBytes();
    charac.setValue(0x01,BluetoothGattCharacteristic.FORMAT_SINT8,0);
    boolean status = mBluetoothGatt.writeCharacteristic(charac);
    return status;
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pritish
  • 1,284
  • 1
  • 19
  • 42
  • 1
    How about `charac.setValue(new byte[]{0x01});` instead of `charac.setValue(0x01,BluetoothGattCharacteristic.FORMAT_SINT8,0);` – anhtuannd Dec 25 '17 at 03:46
  • Hey it worked,please write it as answer so that I can mark it :) @anhtuannd Thanks much :) – Pritish Dec 25 '17 at 04:13

1 Answers1

2

Let's change charac.setValue(0x01,BluetoothGattCharacteristic.FORMAT_SINT‌​8,0); into charac.setValue(new byte[]{0x01});

anhtuannd
  • 918
  • 9
  • 16