Java 将多个uint8和uint16组合成1字节数组

Java 将多个uint8和uint16组合成1字节数组,java,android,bluetooth,Java,Android,Bluetooth,我对java编程相当陌生,我正在尝试为Android(4.3+)开发一款蓝牙低能耗(4.0)健身设备。我从不同的硬件制造商那里买了一堆不同的设备进行测试,向设备发送值的说明如下: 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: distanc

我对java编程相当陌生,我正在尝试为Android(4.3+)开发一款蓝牙低能耗(4.0)健身设备。我从不同的硬件制造商那里买了一堆不同的设备进行测试,向设备发送值的说明如下:

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
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();
问题是:

我有5个int值需要放入一个字节数组中,该数组将被写入设备。 以这些值为例:

16 (# days in month)
450 (distance walked)
334 (distance ran)
800 (steps taken)
4 (number of users)
我不知道如何将这些不同的uint8_t和uint16_t值放入一个字节数组中,以便写入蓝牙设备。谁能告诉我怎么做

谢谢大家!

您可以按如下方式使用:

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
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();