Gil
Gil

Reputation: 578

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

Upvotes: 2

Views: 445

Answers (2)

Adam
Adam

Reputation: 36703

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();

Upvotes: 1

Shashank Kadne
Shashank Kadne

Reputation: 8101

You can use something like this to read from the InputStream...

InputStream i = socket.getInputStream();
        int c;
        String message;
        StringBuffer s = new StringBuffer("");
        while((c=i.read())!=-1)
        {
            s.append((char)c);
        }
        message = s.toString();

Upvotes: 0

Related Questions