Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

How to integrate barcode scanner with android application?

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...