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.
Advertisement
Answer
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:
- when the
datais0x0A, the newline charactern, it is skipped and not added to thearr_byte - when the
datais0x0D, the return characterr, it builds a buffer fromarr_byteand send the buffer to the UI Activity - when the
datais any other character, it is added toarr_byte
Hope this helps.