Android realizes USB code scanning gun to obtain scanning content

  • 2021-12-21 05:02:43
  • OfStack

Recently, I have done a project about externally connecting scanning code on Android equipment. In this record 1, I have a problem about obtaining content by Android USB scanning gun

First of all, I use the code scanning gun of USB HID, which is plug and play. I only need to have an EditText with focus on the interface to get the content scanned by the code scanning gun.

It feels very simple, but today I am talking about getting the content scanned by the code scanning gun without EditText.

USB HID code scanning gun will convert the scanned content into keyboard event, which corresponds to KeyEvent event in Android, so we only need to be in our activity

Override the onKeyDown method


@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        checkLetterStatus(event);
        keyCodeToNum(keyCode);
        if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
            Log.e(" Keyboard event ", buffer.toString());
            buffer.delete(0, buffer.length());
            return true;
        }
 
        return false;
}

As we said above, the code scanning gun responds to our keyboard events, so when the code scanning gun scans one character, it is equivalent to pressing the corresponding key on our keyboard, that is, keyCode, so we only need to deal with this keyCode.

Next, I compare checkLetterStatus () 's method of checking for case with keyCodeToNum () 's method of converting corresponding numbers and letters according to the corresponding keycode


// Check shift Key 
    private void checkLetterStatus(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                // Press shift Key, representing uppercase 
                mCaps = true;
            } else {
                // Loosen shift Key for lowercase 
                mCaps = false;
            }
        }
    }
    // According to keycode Get the corresponding letters and numbers 
    private void keyCodeToNum(int keycode) {
        if (keycode >= KeyEvent.KEYCODE_A && keycode <= KeyEvent.KEYCODE_Z) {
            if (mCaps) {
                buffer.append(map.get(keycode).toUpperCase());
            } else {
                buffer.append(map.get(keycode));
            }
 
        } else if ((keycode >= KeyEvent.KEYCODE_0 && keycode <= KeyEvent.KEYCODE_9)) {
            buffer.append(keycode - KeyEvent.KEYCODE_0);
        } else {
            // Special symbols are not processed for the time being 
        }
 
    }

There is an map in the above method, which is used to store letters


Map<Integer, String> map = new HashMap<>();        
        map.put(29, "a");
        map.put(30, "b");
        map.put(31, "c");
        map.put(32, "d");
        map.put(33, "e");
        map.put(34, "f");
        map.put(35, "g");
        map.put(36, "h");
        map.put(37, "i");
        map.put(38, "g");
        map.put(39, "k");
        map.put(40, "l");
        map.put(41, "m");
        map.put(42, "n");
        map.put(43, "0");
        map.put(44, "p");
        map.put(45, "q");
        map.put(46, "r");
        map.put(47, "s");
        map.put(48, "t");
        map.put(49, "u");
        map.put(50, "v");
        map.put(51, "w");
        map.put(52, "x");
        map.put(53, "y");
        map.put(54, "z");

Finally, an StringBuffer is used to receive the processed data. That's about it!


Related articles: