这篇文章主要介绍了android中如何通过扫码枪读取手机二维码,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
1.引入扫码设备辅助类
public class ScanGunKeyEventHelper { private final static long MESSAGE_DELAY = 500; //延迟500ms,判断扫码是否完成。 private StringBuffer mStringBufferResult; //扫码内容 private boolean mCaps; //大小写区分 private final Handler mHandler; private final BluetoothAdapter mBluetoothAdapter; private final Runnable mScanningFishedRunnable; private OnScanSuccessListener mOnScanSuccessListener; private String mDeviceName; public ScanGunKeyEventHelperOnScanSuccessListener onScanSuccessListener) { mOnScanSuccessListener = onScanSuccessListener ; mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter); mStringBufferResult = new StringBuffer); mHandler = new Handler); mScanningFishedRunnable = new Runnable) { @Override public void run) { performScanSuccess); } }; } /** * 返回扫码成功后的结果 */ private void performScanSuccess) { String barcode = mStringBufferResult.toString); if mOnScanSuccessListener != null) mOnScanSuccessListener.onScanSuccessbarcode); mStringBufferResult.setLength0); } /** * 扫码枪事件解析 * @param event */ public void analysisKeyEventKeyEvent event) { int keyCode = event.getKeyCode); //字母大小写判断 checkLetterStatusevent); if event.getAction) == KeyEvent.ACTION_DOWN) { char aChar = getInputCodeevent);; if aChar != 0) { mStringBufferResult.appendaChar); } if keyCode == KeyEvent.KEYCODE_ENTER) { //若为回车键,直接返回 mHandler.removeCallbacksmScanningFishedRunnable); mHandler.postmScanningFishedRunnable); } else { //延迟post,若500ms内,有其他事件 mHandler.removeCallbacksmScanningFishedRunnable); mHandler.postDelayedmScanningFishedRunnable, MESSAGE_DELAY); } } } //检查shift键 private void checkLetterStatusKeyEvent 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 getInputCodeKeyEvent 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 OnScanSuccessListener { void onScanSuccessString barcode); } public void onDestroy) { mHandler.removeCallbacksmScanningFishedRunnable); mOnScanSuccessListener = null; } //部分手机如三星,无法使用该方法 // private void hasScanGun) { // Configuration cfg = getResources).getConfiguration); // return cfg.keyboard != Configuration.KEYBOARD_NOKEYS; // } // /** // * 扫描枪是否连接 // * @return // */ // public boolean hasScanGun) { // // if mBluetoothAdapter == null) { // return false; // } // // Set<BluetoothDevice> blueDevices = mBluetoothAdapter.getBondedDevices); // // if blueDevices == null || blueDevices.size) <= 0) { // return false; // } // // for Iterator<BluetoothDevice> iterator = blueDevices.iterator); iterator.hasNext); ) { // BluetoothDevice bluetoothDevice = iterator.next); // // if bluetoothDevice.getBluetoothClass).getMajorDeviceClass) == BluetoothClass.Device.Major.PERIPHERAL) { // mDeviceName = bluetoothDevice.getName); // return isInputDeviceExistmDeviceName); // } // // } // // return false; // // } /** * 输入设备是否存在 * @param deviceName * @return */ private boolean isInputDeviceExistString deviceName) { int[] deviceIds = InputDevice.getDeviceIds); for int id : deviceIds) { if InputDevice.getDeviceid).getName).equalsdeviceName)) { return true; } } return false; } /** * 是否为扫码枪事件部分机型KeyEvent获取的名字错误) * @param event * @return */ @Deprecated public boolean isScanGunEventKeyEvent event) { return event.getDevice).getName).equalsmDeviceName); } }
2. active里面实现代理方法
//实现上述类接口‘ public class MainActivity extends AppCompatActivity implements ScanGunKeyEventHelper.OnScanSuccessListener //重写扫码枪识别返回数据 @Override public void onScanSuccessString barcode) { barCode = barcode; if barcode != null && recordPrice > 0 && payString.equals "readyPay")) { payDishs); } } //重写捕捉到扫码枪事件 @Override public boolean dispatchKeyEventKeyEvent event) { mScanGunKeyEventHelper.analysisKeyEventevent); return true; }
dispatchKeyEvent里面分发事件一定设置 return true,否则扫码枪事件传递到屏幕其他按钮上