1

I am creating an app and have data coming in the function onCharacteristicChanged. The data I am getting is byte[]. I need to be able to take the data type and be able to have float numbers which are sent via Bluetooth to the app.

Here is the code:

@Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicChanged(gatt, characteristic);

        byte[] value = characteristic.getValue();
        Log.i(TAG, "" + value);
}

The values I am receiving are:

[B@37719f8
[B@16fced1
[B@444ed36

The byte array length is 2.

I tried using:

float foo = ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getFloat();

but I get a BufferUnderflowException.

I also tried:

int intBits = value[0] << 16 | (value[1] & 0xFF);
Float.intBitsToFloat(intBits);

but am not getting the right values.

Can anyone please help me? I need to convert from byte array to float values and byte[] value has a length of 2. I am new to android apps. Thanks

3
  • Although this question is for Kotlin is uses the standard Java library so the same solution will work for you: stackoverflow.com/questions/44067297/… Commented Apr 6, 2021 at 4:03
  • What should I use for the index? I used 0, 3 nothing and get errors Commented Apr 6, 2021 at 4:20
  • f.getFloat(0), getFloat(1) and so forth but get errors Commented Apr 6, 2021 at 4:32

1 Answer 1

2

I think you should check what the byte from onCharacteristicsChanged really represents.

The below code will print your bytearray into some readable hex string.

  public static String byteToHexString(byte[] data) {
    StringBuffer buf = new StringBuffer();
    if (data != null) {
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9)) {
                    buf.append((char) ('0' + halfbyte));
                } else {
                    buf.append((char) ('a' + (halfbyte - 10)));
                }
                halfbyte = data[i] & 0x0F;
            } while (two_halfs++ < 1);

            buf.append(' ');
        }
    }
    return buf.toString();
}

I've worked on some project with custom ble devices, and most of(I think all of them perhaps) programmers I've worked together used an ascii byte to return something to me.

Sign up to request clarification or add additional context in comments.

9 Comments

I have added your code and ran it and am getting 13 35, 12 34, 13 34. Do I convert those numbers from hex to decimal?
35, 34 represents 5 and 4 on ascii, but I wonder what 13 and 12 are for. Maybe it's not ascii which it represents. I tried putting these numbers inside a four byte array and converted it to float, but the result was '6.53E-42', which I don't think is something you want.
Or maybe, is your desired value just 13.35 and 12.34 ? Well I think It's up to the ble programmer.
No the values are not supposed to be 5 or 4 and not 13.35 or 12.34. I used another app and I was getting the same results which means I am reading the data on my app but it is being sent oddly
My code above just shows the byte as a hex value for you to visualize. Which means, you don't have to convert the strings again to some bytes, but rather use the original byte array. If it's ascii after all, feed each to some char to make it visible, or if it's a raw float byte, add some padding to match 4bytes and convert it to float. Still, it's up to your counterpart to give some insights about how those bytes should be used.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.