Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write 2-byte or 4-byte BLE characteristics value in android?

I am developing an automation system in which I am using android phone(having low energy Bluetooth) as a remote control.

I can write 1-byte(0xFF) characteristic value successfully. Here is my sample code.

byte[] value= {(byte) 0xFF};
characteristic.setValue(value);
mBluetoothGatt.writeCharacteristic(characteristic);

Now the issue is that I want to write more than one-byte characteristic value like:

byte[] value= {(byte) 0xFF,(byte) 0xFF,(byte) 0xFF};
characteristic.setValue(value);
mBluetoothGatt.writeCharacteristic(characteristic);

When I change the value to 2-byte or 3-byte to write the characteristic value then in onCharacteristicWrite()callback method else if (status == BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH) part is executed of the conditional statement. You can see my sample code below. Now please guide me how can I write 2-byte or 3-byte value of characteristic, I would be very thankful to you in this respect. Thanks in andvace.

   public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) 
        {

            if (status == BluetoothGatt.GATT_SUCCESS) 
            {
                broadcastUpdate(ACTION_DATA_WRITE, characteristic);
                Log.e("WRITE SUCCESS", "onCharacteristicWrite() - status: " + status + "  - UUID: " + characteristic.getUuid());
            }
             ...
             ...
             ...
            else if (status == BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH) 
            {
                Log.e("WRITE PROB", "A write operation exceeds the maximum length of the attribute");
            }
        }
like image 556
user3131640 Avatar asked Nov 27 '25 14:11

user3131640


1 Answers

You need to modify the GATT server from the remote device (the one that your phone connects with).

Clearly that characteristic on the server has been defined as 1-byte in length. That's why you cannot write more than one byte.

like image 112
Bogdan Alexandru Avatar answered Nov 30 '25 05:11

Bogdan Alexandru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!