Lewis Smith
Lewis Smith

Reputation: 1345

Reading the value from a hardware barcode scanner

I have a physical barcode scanner plugged into my device which I am trying to use to scan barcodes that are too small for the camera to focus on.

In my activity these are the 2 functions that I have used to check for a key press as I assume it is being handled as a hardware keyboard.

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    Log.v("KeyPressed", "here");
    return true;
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    Log.i("KeyPressed", String.valueOf(event.getKeyCode()));
    return super.dispatchKeyEvent(event);
}

The logs come out like this:

2018-11-28 12:27:17.176 23132-23132/uk.wefix I/KeyPressed: 59
2018-11-28 12:27:18.158 23132-23132/uk.wefix I/KeyPressed: 71
2018-11-28 12:27:18.171 23132-23132/uk.wefix I/KeyPressed: 71
2018-11-28 12:27:18.171 23132-23132/uk.wefix V/KeyPressed: here

However I am not sure how to get the actual content that the barcode scanner is sending. If I had a text input focused then I would get the following appear 034234243423

What would allow that to be pulled through?

Thanks in advance

Upvotes: 0

Views: 509

Answers (1)

karan
karan

Reputation: 8853

It is like handling input from keyboard, you would need to convert those values to characters. also it is better to use it in onKeyDown() Use below code.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    char pressedKey = (char) event.getUnicodeChar();
    Barcode += "" + pressedKey;
    Toast.makeText(getApplicationContext(), "Data: " + Barcode, 1)
            .show();
    return true;
}

Upvotes: 0

Related Questions