android读取扫码模组数据的方法

本文实例为大家分享了android读取扫码模组数据的具体代码,供大家参考,具体内容如下

android读取扫码模组数据 **在dispatchKeyEvent方法中承接读取的数据**

在dispatchKeyEvent方法中承接读取的数据

private StringBuilder mScanCodeBuilder = new StringBuilder();
   @Override
    public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
        LogUtil.d(TAG, "dispatchKeyEvent " + event.getAction() + ",   " + event.getKeyCode());
           checkLetterStatus(event);//判断大小写字母
        if (event.getAction() == KeyEvent.ACTION_DOWN) {

             mScanCodeBuilder.append(getInputCode(event));
            if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                //若为回车键,直接返回
                String scanResult = mScanCodeBuilder.toString();
                mScanCodeBuilder.setLength(0);
                LogUtil.d(TAG, "dispatchKeyEvent, " + scanResult);
                String scanResults = scanResult.replace(" ", "");
            } else {
                //延迟post,若500ms内,有其他事件
                mHandler.removeCallbacks(mScanningFishedRunnable);
                mHandler.postDelayed(mScanningFishedRunnable, 500);
            }

            return true;
        }
        return super.dispatchKeyEvent(event);
    }

判断大小写字母

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) {
                //按着shift键,表示大写
                mCaps = true;
            } else {
                //松开shift键,表示小写
                mCaps = false;
            }
        }
    }

读取字符方法

private char getInputCode(KeyEvent event) {
    int keyCode = event.getKeyCode();
    char aChar;
    if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
        //字母
        aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
    } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
        //数字
        aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);
    } else {
        //符号
        switch (keyCode) {
            case KeyEvent.KEYCODE_GRAVE:
                aChar = '`';
                break;
            case KeyEvent.KEYCODE_COMMA:
                aChar = ',';
                break;
            case KeyEvent.KEYCODE_APOSTROPHE:
                aChar = '\'';
                break;
            case KeyEvent.KEYCODE_POUND:
                aChar = '#';
                break;
            case KeyEvent.KEYCODE_STAR:
                aChar = '*';
                break;
            case KeyEvent.KEYCODE_PERIOD:
                aChar = '.';
                break;
            case KeyEvent.KEYCODE_MINUS:
                aChar = mCaps ? '_' : '-';
                break;
            case KeyEvent.KEYCODE_SLASH:
                aChar = '/';
                break;
            case KeyEvent.KEYCODE_BACKSLASH:
                aChar = mCaps ? '|' : '\\';
                break;
            case KeyEvent.KEYCODE_EQUALS:
                aChar = '=';
                break;
            case KeyEvent.KEYCODE_SEMICOLON:
                aChar = ';';
                break;
            case KeyEvent.KEYCODE_PLUS:
                aChar = '+';
                break;
            case KeyEvent.KEYCODE_AT:
                aChar = '@';
                break;
            case KeyEvent.KEYCODE_LEFT_BRACKET:
                aChar = '[';
                break;
            case KeyEvent.KEYCODE_RIGHT_BRACKET:
                aChar = ']';
                break;
            default:
                aChar = ' ';
                break;
        }
    }
    Log.d(TAG, "char" + aChar);
    return aChar;
}

延迟500毫秒相关

private Handler mHandler = new Handler(this);
    private final Runnable mScanningFishedRunnable=new Runnable() {
        @Override
        public void run() {

        }
    };

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(android读取扫码模组数据的方法)