Following is my sample code to read bar code from a blue tooth scanner.
protected void connect(BluetoothDevice device) {
BluetoothSocket socket = null;
try {
socket = device.createRfcommSocketToServiceRecord(BluetoothProtocols.OBEX_PROTOCOL_UUID);
socket.connect();
while(true){
//on some condition break;
InputStream is=(InputStream) socket.getInputStream();
byte b[]=new byte[200];
is.read(b);
String data=new String(b);
Log.i("data",data);
}
} catch (Exception e) {
Log.e("Exception","Exception occured::"+e.toString());
Log.e(TAG, "", e);
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "", e);
}
} } }
I am getting some abrupt behavior like the following.
- Once I scan a barcode, the read method in InputStream is executed thrice.
- Even if a scan a new bar code I don't get this, rather I get the first scanned bar code and again repeated thrice.
Any help in this regard will be highly appreciated.