Handle barcode scanner value via Android device

Anas BEKRI picture Anas BEKRI · Jul 5, 2012 · Viewed 10.6k times · Source

I'm trying to handle value from a barcode scanner USB via my Android 3.2 tablet, the scanner is succefuly working in the OS but i want to get the value in the program without a edittext, usbmanager host and accessory did not list it with the connected devices via USB.

Answer

FoamyGuy picture FoamyGuy · Jul 5, 2012

most plug in barcode scanners (that I've seen) are made as HID profile devices so whatever they are plugged into should see them as a Keyboard basically. I think this is why they do not show up in the USB host APIs list of accessories. You should be able to get raw input from them the same way you would a keyboard inside your Activity by overriding Activity.onKeyDown(int keycode, KeyEvent ke)

Something like this in your activity:

@Override
protected boolean onKeyDown(int keyCode, KeyEvent event) {
     Log.i("TAG", ""+ keyCode);
     //I think you'll have to manually check for the digits and do what you want with them.
     //Perhaps store them in a String until an Enter event comes in (barcode scanners i've used can be configured to send an enter keystroke after the code)
     return true;
 }