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

Romko Smuk
  • 57
  • 10

1 Answers1

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.

March3April4
  • 2,131
  • 1
  • 18
  • 37
  • 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? – Romko Smuk Apr 06 '21 at 04:43
  • 1
    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. – March3April4 Apr 06 '21 at 04:55
  • 1
    Or maybe, is your desired value just 13.35 and 12.34 ? Well I think It's up to the ble programmer. – March3April4 Apr 06 '21 at 04:57
  • 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 – Romko Smuk Apr 06 '21 at 05:52
  • So I believe I am reading asci values just something is funky with my mplab which is what I use for the microcontoller but my teammates has her working fine so I will ask her to reprogram the board. So if I'm reading asci values what would I use then to convert the byte array? – Romko Smuk Apr 06 '21 at 06:13
  • Just to clarify after your code is run I get ascii values. So from there how am I able to divide the data from the string and have them be floats? – Romko Smuk Apr 06 '21 at 06:34
  • 1
    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. – March3April4 Apr 06 '21 at 07:37
  • Okie so we fixed the data that is being transmitted and after using your code we get the ascii of the floating numbers. So the length is 20 of the byte but not all the hex values are the numbers we need. In the byte array there are two different numbers we sent. Is there a way to convert the byte to a float? – Romko Smuk Apr 06 '21 at 22:12
  • For example the byte array after conversion to Hex is : 36 32 35 2e 30 30 20 00 4e 52 33 31 34 2e 30 30 20 00 cb b7 and that is 625.00 NR314.00 E – Romko Smuk Apr 06 '21 at 23:13