Reputation: 153
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.
Upvotes: 14
Views: 16312
Reputation: 467
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);
}
Upvotes: 0
Reputation: 101
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
Log.i("TAG", event.getCharacters());
return true;
}`
Upvotes: 0
Reputation: 82
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.
Upvotes: 0
Reputation: 305
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.
Upvotes: 4
Reputation: 46856
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;
}
Upvotes: 12