Handle barcode scanner value via Android device

12,858

Solution 1

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;
 }

Solution 2

you will get the result on Activity keydown event.

For Example:-

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

Hope this post will help you.

Share:
12,858

Related videos on Youtube

Anas BEKRI
Author by

Anas BEKRI

Updated on September 16, 2022

Comments

  • Anas BEKRI
    Anas BEKRI over 1 year

    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.

  • Jaldip Katre
    Jaldip Katre about 10 years
    @BlackBird_K Is any Arduino board is used as middle man for Communication?
  • Omar Faroque Anik
    Omar Faroque Anik about 8 years
    I tried this and worked but i had to press a button in barcodescanner. Can i do it programatically, then user does not need to press. It automatically detect and scan?
  • DoruChidean
    DoruChidean over 7 years
    I have a barcode scanner that sends Enter event on marshmellow but not on IcreaCream...any ideas?
  • Ulysse BN
    Ulysse BN almost 7 years
    Maybe should you consider giving an exemple?