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.
-
Which Bar Code scanner are you using? I am also working on same kind of project, It will be helpful for me. – Jaldip Katre Feb 22 '14 at 11:33
-
I also had same problem. Please check my solution [here](https://stackoverflow.com/a/69105676/13321079) – MohamedHarmoush Sep 08 '21 at 15:21
5 Answers
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;
}

- 46,603
- 18
- 125
- 156
-
@BlackBird_K Is any Arduino board is used as middle man for Communication? – Jaldip Katre Feb 22 '14 at 13:45
-
1I 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? – Omar Faroque Anik Apr 27 '16 at 01:40
-
I have a barcode scanner that sends Enter event on marshmellow but not on IcreaCream...any ideas? – DoruChidean Aug 31 '16 at 13:18
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.

- 285
- 1
- 2
- 13
I also had same problem,but when i used onKeyDown or onKeyUp,it was not called every time i mean for every character for barcode.I Used DiapatchKeyEvent,and it worked nice.

- 82
- 1
- 5
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
Log.i("TAG", event.getCharacters());
return true;
}`

- 91
- 2
dispatchKeyEvent
can be used instead of onKeyDown
.
Because, dispatchKeyEvent will pass the event to onKeyListener if there is activity. This is when onKey is called.
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int c=event.getUnicodeChar();
Log.d("C:", "" + c);
}

- 369
- 2
- 17