-2

I am trying to develop an application which reads bar code via USB bar code scanner..My problem is that I don't know how to access this bar code scanner through application and read the values..I'd like to know how to detect bar code scanner and use it..Any idea or link would be helpful that I can use to learn about these things..I am new to android..

Akshay
  • 43
  • 7

2 Answers2

4

finally I get this working.

Important! on android 2.3 you can catch barcode in OnKeyDown event, but in 4.3 your real scanner will press on any focused button, so put the code into dispatchKeyEvent and return true.

Some button still will be focused (selected, pre-pressed, highlighted, only god knows what is it), but press event won't be fired. If anybody knows how to avoid this (except auto moving focus...) tell me

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    //barcode scanner
    int c=event.getUnicodeChar();
    //accept only 0..9 and ENTER
    if ((c>=48 && c<=57) || c==10){
        if (event.getAction()==0) {
            if (c >= 48 && c <= 57)
                barcode += "" + (char) c;
            else {
                if (!barcode.equals("")) {
                    final String b = barcode;
                    barcode = "";
                    new Thread(new Runnable() {
                        public void run() {
                            checkBarcode(b); 
                            //there you get a string and compare it or store etc
                        }
                    }).start();
                }
            }
        }
        return true;
    }
    return super.dispatchKeyEvent(event);
}
djdance
  • 3,110
  • 27
  • 33
  • Thank you, would you know how to also accept a to z, (, ) and . (dot)? Maybe post answer here > https://stackoverflow.com/questions/52927248/usb-scanning-on-android – BENN1TH Oct 22 '18 at 10:34
0

Assuming your USB barcode scanner is in keyboard-wedged mode (i.e. when you scan a barcode, it's as if you were typing the numbers to the active window), then you don't have to do anything. Just plug the barcode into the mini-usb-to-usb adaptor and scan away. Retrieve the barcode data from the onKeyDown event.

F0r3v3r-A-N00b
  • 2,903
  • 4
  • 26
  • 36