我正在研究蓝牙,我正在尝试编写代码以在连接时继续收听输入流,我遇到了以下代码片段:
int data = mmInStream.read();
if(data == 0x0A) {
} else if(data == 0x0D) {
buffer = new byte[arr_byte.size()];
for(int i = 0 ; i < arr_byte.size() ; i++) {
buffer[i] = arr_byte.get(i).byteValue();
}
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothState.MESSAGE_READ
, buffer.length, -1, buffer).sendToTarget();
arr_byte = new ArrayList<Integer>();
} else {
arr_byte.add(data);
}
有人可以解释 0x0A 和 0x0D 之间的区别吗?并对这段代码进行简要说明。欢迎分享你的看法。
原文由 Rasik Suhail 发布,翻译遵循 CC BY-SA 4.0 许可协议
以
0x
开头的值是十六进制。0x0A
0x0D
\n
\r
您可以 在此处 阅读有关如何转换它们的更多信息,或使用 转换表代码基本上运行不同的逻辑块,具体取决于从
data
读取的mmInStream
的值简要地:
data
is0x0A
, the newline character\n
, it is skipped and not added to thearr_byte
data
is0x0D
, the return character\r
, it builds a buffer fromarr_byte
and send the buffer to the UI Activitydata
是任何其他字符时,它被添加到arr_byte
希望这可以帮助。