Scan the code to grab the QR code information to realize the reading and reading information. The local code scanner is an external writing device, and its essence is to monitor read and write input. The following is the scanning device to read the payment QR code.
1. Introduce code scanning equipment auxiliary category
public class ScanGunKeyEventHelper { private final static long MESSAGE_DELAY = 500; //Depend 500ms, determine whether the scanning code is completed. private StringBuffer mStringBufferResult; //Scan the QR code content private boolean mCaps; //Case difference 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 = (); mStringBufferResult = new StringBuffer(); mHandler = new Handler(); mScanningFishedRunnable = new Runnable() { @Override public void run() { performScanSuccess(); } }; } /** * Return the result after the code is successfully scanned */ private void performScanSuccess() { String barcode = (); if (mOnScanSuccessListener != null) (barcode); (0); } /** * Analysis of QR code scan gun incident * @param event */ public void analysisKeyEvent(KeyEvent event) { int keyCode = (); //Case judgment of letters checkLetterStatus(event); if (() == KeyEvent.ACTION_DOWN) { char aChar = getInputCode(event);; if (aChar != 0) { (aChar); } if (keyCode == KeyEvent.KEYCODE_ENTER) { //If it is the Enter key, return directly (mScanningFishedRunnable); (mScanningFishedRunnable); } else { //Delay post, if there are other events within 500ms (mScanningFishedRunnable); (mScanningFishedRunnable, MESSAGE_DELAY); } } } //Check shift key private void checkLetterStatus(KeyEvent event) { int keyCode = (); if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) { if (() == KeyEvent.ACTION_DOWN) { //Press shift key to indicate capitalization mCaps = true; } else { //Release shift key to lowercase mCaps = false; } } } //Get scanned content private char getInputCode(KeyEvent event) { int keyCode = (); char aChar; if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) { //letter aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A); } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) { //number aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0); } else { //Other symbols 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 onScanSuccess(String barcode); } public void onDestroy() { (mScanningFishedRunnable); mOnScanSuccessListener = null; } // Some mobile phones such as Samsung cannot use this method// private void hasScanGun() { // Configuration cfg = getResources().getConfiguration(); // return != Configuration.KEYBOARD_NOKEYS; // } // /** // * Is the scanner connected? // * @return // */ // public boolean hasScanGun() { // // if (mBluetoothAdapter == null) { // return false; // } // // Set<BluetoothDevice> blueDevices = (); // // if (blueDevices == null || () <= 0) { // return false; // } // // for (Iterator<BluetoothDevice> iterator = (); (); ) { // BluetoothDevice bluetoothDevice = (); // // if (().getMajorDeviceClass() == ) { // mDeviceName = (); // return isInputDeviceExist(mDeviceName); // } // // } // // return false; // // } /** * Whether the input device exists * @param deviceName * @return */ private boolean isInputDeviceExist(String deviceName) { int[] deviceIds = (); for (int id : deviceIds) { if ((id).getName().equals(deviceName)) { return true; } } return false; } /** * Is it a QR code scan gun event (the name obtained by some models KeyEvent is incorrect) * @param event * @return */ @Deprecated public boolean isScanGunEvent(KeyEvent event) { return ().getName().equals(mDeviceName); } }
2. Implement proxy methods in active
//Implement the above-mentioned interface'public class MainActivity extends AppCompatActivity implements //Rewrite the code scan gun to recognize and return data@Override public void onScanSuccess(String barcode) { barCode = barcode; if (barcode != null && recordPrice > 0 && ("readyPay")) { payDishs(); } } //Rewrite and capture the scan gun incident @Override public boolean dispatchKeyEvent(KeyEvent event) { (event); return true; }
dispatchKeyEventThe distribution event is setreturn true, otherwise the scan gun event will be passed to other buttons on the screen
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.