1

I am fairly new to java programming and I am trying to develop a Bluetooth Low Energy (4.0) fitness device for Android (4.3+). I bought a bunch of different devices from different hardware manufacturers for testing and one of the instructions for sending values to the device is as follows:

Characteristic Fitness Goals
size: 8 bytes    
D0 D1 D2 D3 D4 D5 D6 D7

D0: number of days in month, uint8_t   
D1 D2: distance walked , uint16_t
D3 D4: distance ran, uint16_t
D5 D6: steps taken, uint16_t
D7: number of users, uint8_t

Here is the problem:

I have 5 int values that I need to put into a byte array that will be written to the device. Take these values for example:

16 (# days in month)
450 (distance walked)
334 (distance ran)
800 (steps taken)
4 (number of users)

I am not sure how to take these different uint8_t and uint16_t values and put them into one byte array in order to write to the Bluetooth Device. Can anyone please tell me how this can be done?

Thank you!

1 Answer 1

1

You can use a ByteBuffer as follows:

ByteBuffer buf = ByteBuffer.allocate(8);

// Depending on the device you may need to include the following line
// buf.order(ByteOrder.LITTLE_ENDIAN);

buf.put((byte)16); // (# days in month)
buf.putShort(450); // (distance walked)
buf.putShort(334); // (distance ran)
buf.putShort(800); // (steps taken)
buf.put((byte)4);  // (number of users)

byte[] byteArray = buf.array();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.