I was studying about bluetooth and I was trying to write the code to keep listening to the input stream while connected and i came across this following code snippet:
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);
}
Can someone explain what is the difference between 0x0A and 0x0D. And also give a brief explanation about this code. Kindly share your views.
The values starting 0x
are hexadecimals. 0x0A
is \n
newline character and 0x0D
is \r
return character. You can read more about how to convert them here, or use the conversion chart
The code essentially runs different blocks of logic depending on what value of data
is read from the mmInStream
Briefly:
data
is 0x0A
, the newline character \n
, it is skipped and not added to the arr_byte
data
is 0x0D
, the return character \r
, it builds a buffer from arr_byte
and send the buffer to the UI Activitydata
is any other character, it is added to arr_byte
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With