2

I want to take the byte value out of this class so I can parse it, or if it's possible parse it inside and get the 5th and 6th byte value out.

private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        final byte[] data = characteristic.getValue();




        if (data != null && data.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for(byte byteChar : data)
                stringBuilder.append(String.format("%02X", byteChar));
            intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());




        } else {
            // For all other profiles, writes the data formatted in HEX.


        }

    }


    sendBroadcast(intent);

}

How this works is that I step on a scale and it sends me these bytes:

00 00 00 00 02 02 00 00 00 00 00 00 00 00 00 00

Which is stored at the variable 'data'. How do I take 'data' out to use in another class?

The weight data is on bytes 5 and 6. If you convert the hex values of bytes 5 and 6, in that example is '0202' it becomes 514 in decimal (51.4kg).

I need to take the bytes data to use in another class to get the kg data. How would I go around doing so?

Micer
  • 8,731
  • 3
  • 79
  • 73
Anj
  • 95
  • 13
  • `data[5]*256+data[6]` ? If you want to convert it to float (kg) value `double weight = (data[5]*256+data[6])/10.0` ? – Jean-Baptiste Yunès Nov 16 '16 at 08:42
  • Yup, I got that down. However, I need to get the byte data(which is stored in the variable 'data') out of the class to use it. I'm guessing through intents or something, I'm not too sure. Thanks. – Anj Nov 16 '16 at 08:47

1 Answers1

1

You can simply get those two bytes and apply below:

int result = ByteBuffer.wrap(bytes).getInt();

If you want more info: HERE1, HERE 2

Community
  • 1
  • 1
Sean83
  • 493
  • 4
  • 14
  • Would I be able to access 'result' through another class? – Anj Nov 16 '16 at 08:56
  • Ah OK. there was an another question. I see intent which seems like you are using Android. in that case are you talking about another activity instead of another class? – Sean83 Nov 16 '16 at 08:59
  • Oh, that one is separate. I want to get the value of result for example to another class. Like to call result to be able to take the value and manipulate it. Thanks mate. – Anj Nov 16 '16 at 09:01
  • OK. you can either apply above implementation where you receive it and if the rest of data are not needed then, you can only extract this and send. – Sean83 Nov 16 '16 at 09:04
  • Ah, how do you send? That's the part I'm a bit confused about. I can't access the data on my other class. – Anj Nov 16 '16 at 09:06
  • http://stackoverflow.com/questions/9137477/passing-data-from-broadcast-receiver-to-another-activity – Sean83 Nov 16 '16 at 09:10
  • I not very sure but I believe you omit to specify the class name of the intent. – Sean83 Nov 16 '16 at 09:11