嗨,目前我正在开发通过蓝牙与医疗保健设备通信的 Android。医疗保健设备可以发送如下格式的数据包
现在我想知道,如何分别识别 LSB 、 Acces code、 Header 、 Msb 和 Payload 。以及如何从该数据包中检索数据。确实,我对这种数据包开发很陌生。我用谷歌搜索过,但我只得到了理论解决方案。我还想知道我是否可以使用 Datagrampacket 或其他第 3 方 API。请有人为此建议我一些想法和教程。提前致谢。
最佳答案
尝试使用以下方法:
(2475位?可能应该是2472或2480,或者如果 header 是54位,这里应该是2474位) //读取字节
public byte[] readBytes(InputStream inputStream, int length)
throws IOException {
byte[] data = new byte[length];
int len = inputStream.read(data);
if (len != length) {
throw new IOException("Read the end of stream.");
}
return data;
}
//Get Header data
byte[] headerData = readBytes(inputStream, 9);
// I think header data need to parse again, its structure should look like the following format:
// | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
// | Version | Type | other values |
// You can parse them to use headerData
// #######################################
// write bytes
public class ByteWriter {
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
public void writeBytes(byte[] data) {
try {
outputStream.write(data);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public byte[] getBytes() {
return outputStream.toByteArray();
}
}
关于java - 使用java识别并分离Datagrampacke的数据包中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925918/