Gil Gil - 4 months ago 20
Android Question

Bluetooth barcode getInputStream()

I am using a bluetooth barcode reader to deliver text to an edit text. The problem is that it cuts the barcode text for about half the time and delivers me an incomplete text.

I am using the class "BluetoothSocket".
This is my reading code:

int bytes = socket.getInputStream().read(buffer);

message = message + new String(buffer, 0, bytes);

// Send the obtained bytes to the UI Activity
handler.post(new MessagePoster(textView, message));


I am looking for a way to make the action of getInputStream() be longer

Answer

Something like this should work, just keep going until the string is the expected length. I'm assume the stream just keeps going.

InputStream stream = socket.getInputStream();
StringBuilder builder = new StringBuilder();
do {
    byte [] buffer = new byte[256];
    int bytes = stream.read(buffer);
    builder.append(new String(buffer, 0, bytes));
} while ((builder.length() < 10));
String completeStr = builder.toString();