Barcode scanning guns are now available everywhere, and they can quickly scan the content of barcodes, which is no more than a little faster than scanning codes on mobile phone cameras.
To save costs, the code scanner can be connected directly to Android or other devices via Bluetooth.
So how do Android devices obtain scanned content through Bluetooth?
1. Bluetooth pairing, connecting to the device
Open system settings, find Bluetooth, turn on the QR code scan gun, and pair the QR code scan gun device. Enter a fixed pairing code, which is generally written in the code scan gun manual. After the pairing is complete, the device is connected. Just OK.
Configure permissions
Add Bluetooth permissions to files in android project.
<uses-permission android:name="" /> <uses-permission android:name=".BLUETOOTH_ADMIN" />
3. Detect the connection status of the code scan gun
Generally speaking, the code scanning gun device is also equivalent to the ordinary external input device type and an external keyboard.
My QR code scanner device returns the following Bluetooth type.
Generally speaking, information about our code scanning gun equipment can be obtained through the following method.
Set<BluetoothDevice> blueDevices = (); if (blueDevices == null || () <= 0) { return false; } for (Iterator<BluetoothDevice> iterator = (); (); ) { BluetoothDevice bluetoothDevice = (); if (().getMajorDeviceClass() == ) { //TODO Get the scanner device information } }
During the development process, it is inevitable that it will be necessary to determine whether the device is connected normally in real time.
()
This method can only determine whether the device has been paired and bound. But binding does not represent connection, so you can only give up.
public List getConnectedDevices (int profile)
public int getConnectionState (BluetoothDevice device, int profile)
Then I tried these two methods, which are available, but the device sdk>18, that is, Android version 4.3 or above is available.
Later, I turned around and thought that since the code scanning gun is also an input device, we can start with different Bluetooth device status detection and instead start with input device detection. then,
private void hasScanGun() { Configuration cfg = getResources().getConfiguration(); return != Configuration.KEYBOARD_NOKEYS; }
Get it done.
4. Obtain the scan content of the scan code gun
Since the scanner is an external input device, it is natural that we start with KeyEvent.
Event parsing class
/** * Scan code gun event analysis class */ public class ScanGunKeyEventHelper { //Depend 500ms, determine whether the scanning code is completed. private final static long MESSAGE_DELAY = 500; //Scan the QR code content private StringBuffer mStringBufferResult = new StringBuffer(); //Case difference private boolean mCaps; private OnScanSuccessListener mOnScanSuccessListener; private Handler mHandler = new Handler(); private final Runnable mScanningFishedRunnable = new Runnable() { @Override public void run() { performScanSuccess(); } }; //Return the scan result private void performScanSuccess() { String barcode = (); if (mOnScanSuccessListener != null) (barcode); (0); } //key event processing 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 { public void onScanSuccess(String barcode); } public void setOnBarCodeCatchListener(OnScanSuccessListener onScanSuccessListener) { mOnScanSuccessListener = onScanSuccessListener; } public void onDestroy() { (mScanningFishedRunnable); mOnScanSuccessListener = null; } }
Rewrite the dispatchKeyEvent method in the Activity to intercept the Key event.
/** * Activity intercepts key press event. Send to ScanGunKeyEventHelper * * @param event * @return */ @Override public boolean dispatchKeyEvent(KeyEvent event) { if (isScanGunEvent(event)) { (event); return true; } return (event); } /** * Show scanned content * @param barcode */ @Override public void onScanSuccess(String barcode) { //TODO displays scanned content }
For detailed code, please refer to: /czhzero/scangon
Note:
1. Some models cannot determine external keyboard information, such as Samsung.
private void hasScanGun() { Configuration cfg = getResources().getConfiguration(); return != Configuration.KEYBOARD_NOKEYS; }
The return value of Samsung phone is equal to Configuration.KEYBOARD_NOKEYS.
Therefore, for better compatibility, the following methods can be adopted
/** * Determine whether the scanner has been connected * @return */ protected boolean hasScanGun() { Set<BluetoothDevice> blueDevices = (); if (blueDevices == null || () <= 0) { return false; } for (Iterator<BluetoothDevice> iterator = (); (); ) { BluetoothDevice bluetoothDevice = (); if (().getMajorDeviceClass() == ) { return isInputDeviceUsed(()); } } return false; } private boolean isInputDeviceUsed(String deviceName) { int[] deviceIds = (); for (int id : deviceIds) { if ((id).getName().equals(deviceName)) { return true; } } return false; }
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.