Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write “0xFF” as a characteristics value in android BLE?

Tags:

android

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

like image 394
Smit K Avatar asked Oct 31 '22 15:10

Smit K


1 Answers

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);
like image 68
zapl Avatar answered Nov 15 '22 06:11

zapl