How to read the QR Code of Mobile phone through Code scanning Gun in android
This article mainly introduces how to read the mobile phone QR code through the code scanning gun in android, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
1. Introduce the auxiliary class of code scanning equipment
Public class ScanGunKeyEventHelper {private final static long MESSAGE_DELAY = 500; / / delay 500ms to determine whether the scan code is completed. Private StringBuffer mStringBufferResult; / / scan code content private boolean mCaps; / / case sensitive private final Handler mHandler; private final BluetoothAdapter mBluetoothAdapter; private final Runnable mScanningFishedRunnable; private OnScanSuccessListener mOnScanSuccessListener; private String mDeviceName; public ScanGunKeyEventHelper (OnScanSuccessListener onScanSuccessListener) {mOnScanSuccessListener = onScanSuccessListener; mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter (); mStringBufferResult = new StringBuffer () MHandler = new Handler (); mScanningFishedRunnable = new Runnable () {@ Override public void run () {performScanSuccess ();}};} / * returns the result of successful code scan * / private void performScanSuccess () {String barcode = mStringBufferResult.toString () If (mOnScanSuccessListener! = null) mOnScanSuccessListener.onScanSuccess (barcode); mStringBufferResult.setLength (0);} / * Scancode gun event resolution * @ param event * / public void analysisKeyEvent (KeyEvent event) {int keyCode = event.getKeyCode (); / / letter case judgment checkLetterStatus (event) If (event.getAction () = = KeyEvent.ACTION_DOWN) {char aChar = getInputCode (event);; if (aChar! = 0) {mStringBufferResult.append (aChar);} if (keyCode = = KeyEvent.KEYCODE_ENTER) {/ / if it is an enter key, return mHandler.removeCallbacks (mScanningFishedRunnable) directly MHandler.post (mScanningFishedRunnable);} else {/ / delay post. If there are other events in the 500ms, mHandler.removeCallbacks (mScanningFishedRunnable); mHandler.postDelayed (mScanningFishedRunnable, MESSAGE_DELAY);}} / / check the 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 the shift key to indicate uppercase mCaps = true;} else {/ / release the shift key, indicating lowercase mCaps = false } / / get scan content private char getInputCode (KeyEvent event) {int keyCode = event.getKeyCode (); char aChar; if (keyCode > = KeyEvent.KEYCODE_A & & keyCode = KeyEvent.KEYCODE_0 & & keyCode