5

I am trying to write hex value 0xFF in the fragrance dispenser device using BluetoothGattCharacteristic method setValue(..) .I do get success status code 0 in the call back method onCharacteristicWrite() But device does not perform any action, ideally it should emit fragrance.

below is my sample code to write to the characteristics

private void writeCharacteristic(CallbackContext callbackContext, UUID serviceUUID, UUID characteristicUUID, byte[] data, int writeType) {

    boolean success = false;

    if (gatt == null) {
        callbackContext.error("BluetoothGatt is null");
        return;
    }
    BluetoothGattService service = gatt.getService(serviceUUID);
    BluetoothGattCharacteristic characteristic = findWritableCharacteristic(service, characteristicUUID, writeType);
            if (characteristic == null) {
        callbackContext.error("Characteristic " + characteristicUUID + " not found.");
    } else {
        int data2=0xFF;
        characteristic.setValue(data2, BluetoothGattCharacteristic.FORMAT_UINT16, 0);
        characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        writeCallback = callbackContext;   
        if (gatt.writeCharacteristic(characteristic)) {
            success = true;
            System.out.println(" writeCharacteristic success");
        } else {
            writeCallback = null;
            callbackContext.error("Write failed");
        }
}

Please suggest way to write hex data in setValue() method of BluetoothGattCharacteristic .

Thanks

zapl
  • 63,179
  • 10
  • 123
  • 154
Smit K
  • 121
  • 1
  • 7

2 Answers2

1

0xFF in BluetoothGattCharacteristic.FORMAT_UINT16 means you'll send FF 00 because you set it to send a 16 bit unsigned number. To send only 0xFF (and I don't know if that makes a difference) you'll have to set the format to UINT8.

characteristic.setValue(data2, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
zapl
  • 63,179
  • 10
  • 123
  • 154
  • I tried with BluetoothGattCharacteristic.FORMAT_UINT8 but it is also not working ..callback method readCharacteristic() is also not getting invoked , – Smit K Nov 19 '15 at 08:48
  • @SmitK I guess your problem isn't the 0xff then. Ble is unfortunately very tricky. Maybe you have the wrong characteristic, try to write the wrong value or the protocol of your device needs you to do something else on top – zapl Nov 19 '15 at 08:56
1

You can send byte array to charcteristics.

Convert your hex to byte array using below method.link

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

convert your number to hex first ...

public static String toHex(String arg) 
{
    try 
    {
        return String.format("%01x", new BigInteger(1, arg.getBytes("UTF-8")));
    }
    catch (UnsupportedEncodingException e) 
    {
        e.printStackTrace();
        return "";
    }
}

//set data
characteristic.setValue(hexStringToByteArray(toHex(255+""));
Community
  • 1
  • 1
shreyash mashru
  • 301
  • 2
  • 9
  • Above method gives value in bytes as -1. but i need to send hex value of 0xFF which is 255 in the BLE device. I am not getting any response in readCharacteristic() method , so not sure if i am able to write to the BLE device successfully or not . – Smit K Nov 19 '15 at 08:45
  • @SmitK you want to send 255 number to device? – shreyash mashru Nov 19 '15 at 09:18
  • yes i am trying to send 255. Actually i have to send hex value 0xFF to device but characteristic.setValue(..) support only int ,byte[] and string ,so i am converting hex value 0xFF to 255 before sending it to device. – Smit K Nov 19 '15 at 10:05