Android扫码器通过USB数据通信而不是串口数据通信!dispatchKeyEvent代替EditText控件

拦截dispatchKeyEvent方法

重写dispatchKeyEvent方法

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    toolScanner.analysisKeyEvent(event);
    return true;
}

相关逻辑处理工具类:UsbTransferDataUtils

/**
 * 扫描事件处理
 */
public class UsbTransferDataUtils {
    private final Handler mHandler;
    private boolean mCaps;
    private IOnScanSuccessListener mOnScanSuccessListener;
    private StringBuffer mStringBufferResult;
    private Runnable mScanningFishedRunnable;

    public UsbTransferDataUtils(IOnScanSuccessListener onScanSuccessListener) {
        mOnScanSuccessListener = onScanSuccessListener;
        mStringBufferResult = new StringBuffer();
        mHandler = new Handler();
        mScanningFishedRunnable = new Runnable() {
            @Override
            public void run() {
                performScanSuccess();
            }
        };
    }

    public void analysisKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode(); //字母大小写判断
        checkLetterStatus(event);
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            char aChar = getInputCode(event);
            if (aChar != 0) {
                mStringBufferResult.append(aChar);
            }
            if (keyCode == KeyEvent.KEYCODE_ENTER) { //若为回车键,直接返回
                mHandler.removeCallbacks(mScanningFishedRunnable);
                mHandler.post(mScanningFishedRunnable);
            } else { //延迟post,若500ms内,有其他事件
                mHandler.removeCallbacks(mScanningFishedRunnable);
                mHandler.postDelayed(mScanningFishedRunnable, 500);
            }
        }
    }

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

    //获取扫描内容 6921734962300
    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_PERIOD:
                    aChar = '.';
                    break;
                case KeyEvent.KEYCODE_MINUS:
                    aChar = mCaps ? '_' : '-';
                    break;
                case KeyEvent.KEYCODE_SLASH:
                    aChar = '/';
                    break;
                case KeyEvent.KEYCODE_BACKSLASH:
                    aChar = mCaps ? '|' : '\\';
                    break;
                default:
                    aChar = 0;
                    break;
            }
        }
        return aChar;
    }

    //接口回调
    public interface IOnScanSuccessListener {
        void onScanSuccess(String barcode);
    }

    private void performScanSuccess() {
        String barcode = mStringBufferResult.toString();
        if (mOnScanSuccessListener != null) mOnScanSuccessListener.onScanSuccess(barcode);
        mStringBufferResult.setLength(0);
    }
}

处理回调结果

 /**
 * s扫描事件监听
 *
 * @param barcode
 */
@Override
public void onScanSuccess(String barcode) {
    mealCode = barcode;
    Log.e("yezhou", barcode);
    if (mealCode.length() > 0) {
        //扫码调接口

    }
}

自定义Edittext

public class ScannerEditText extends AppCompatEditText implements TextWatcher {

    private ScannerTextInterface textInterface;

    public ScannerEditText(Context context) {
        super(context);
    }

    public ScannerEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        addTextChangedListener(this);
    }

    public void ScannerTextCallBack(ScannerTextInterface anInterface){
        this.textInterface = anInterface;
    }

    public interface ScannerTextInterface{
        void getScavengingText(String s);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String str = s.toString();
        if (str.length() > 2) {
            if (str.endsWith("\n")) {
                if (textInterface != null){
                    textInterface.getScavengingText(str);
                }
                s.clear();
            }
        }
    }
}

在对应的Activity implements ScannerEditText.ScannerTextInterface接口,重写方法getScavengingText(String s)在这个方法中参数就是我们扫描获取到的结果

问题缺陷:重写dispatchKeyEvent方法后,这个方法依赖Activity,确实可以监听到扫码器通过USB传递过来的数据,但是当这个Activity界面不可见时也就是执行onPause方法后就不会执行该方法。第二种方法也是依赖Activity

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/29/android-scanner-communicates-through-usb-data-instead-of-serial-data-dispatchkeyevent-replaces-edittext-control/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android扫码器通过USB数据通信而不是串口数据通信!dispatchKeyEvent代替EditText控件
拦截dispatchKeyEvent方法 重写dispatchKeyEvent方法 @Override public boolean dispatchKeyEvent(KeyEvent event) { toolScanner.analysisKeyEvent(event……
<<上一篇
下一篇>>
文章目录
关闭
目 录