Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write "AFCF" as a characteristics value in android BLE?

I am using sample code of Bluetooth low energy and I have made some minor changes in it in order to write characteristics value. Here below is my code that I am using for writing characteristic value and it successfully write 1-byte(0xFF) value.

public void writeCharacteristicValue(BluetoothGattCharacteristic characteristic)
{
    byte[] value= {(byte) 0xFF};
    characteristic.setValue(bytesToHex(value));
    boolean status = mBluetoothGatt.writeCharacteristic(characteristic);
}


final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bytes) 
    {
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for ( int j = 0; j < bytes.length; j++ ) 
        {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

The above code works properly to write 1-byte(0xFF) value but I need to write 2-byte characteristic value.

When I change the value to 2-byte in the writeCharacteristicValue() method like byte[] value= {(byte) 0xFF,(byte) 0xFF}; then onCharacteristicWrite() callback method shows exception "A write operation exceeds the maximum length". Here you can see onCharacteristicWrite() callback method code

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");
            }
        }

Now the issue is that I want to write "AFCF" as a characteristics value. Please guide me in this respect and let me know what specific changes I would need in the code to write"AFCF" value.

Already I have consumed alot of time to solve the issue but so far my effort bring no fruit. Please help me and I would be very thankful to you for this act of kindness.

like image 459
user3131640 Avatar asked Nov 23 '25 02:11

user3131640


1 Answers

The error may be because you are putting your value as a "single value" in byte array. Since, byte array holds each index value of 1 byte. So, it was working perfect when you write 1 byte But when u try to write 2 bytes then do not use the above method to convert the hexadecimal value to bytes array, Use another.

Use:

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;
	}
like image 93
Rahul Rastogi Avatar answered Nov 24 '25 16:11

Rahul Rastogi



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!