Preface
Everyone should have some experience. In life, barcode scanning guns can be seen everywhere. 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 scan gun can be directly connected 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 instructions for scanning the code. 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 type of ordinary external input device 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, 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 method 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; }
Android system solves the problem of scanning code guns that cannot enter letters and characters
question:
When using the QR code scan gun, there are letters and characters, but they are not entered into the Android system. It is normal to enter it into the computer. This is very strange. It makes an upper-level development confused. Finally, I discussed with the system and found out that it was caused by the deletion of the mapping of the system keys.
Solution:
There is this file under the Android system layer frameworks/base/data/keyboards folder. This file is the corresponding table for Android's default key mapping, and other such as: files, and some custom code values kl files.
Open it and look at the type. Maybe you will understand.
key 11 0 key 2 1 key 3 2 key 4 3 key 5 4 key 6 5 key 7 6 key 8 7 key 9 8 key 10 9 key 12 MINUS key 13 EQUALS key 14 DEL key 15 TAB
It is a mapping between keys and key values, for example: the key corresponding to key value 11 is 0, and so on. Then the solution will be clear. It will be OK to add the key mapping of all letters and characters. As for the key value, I directly refer to the files of another platform. Recompile the system verification and this issue is resolved.
Confused questions:
- Use the same Android version to see the corresponding values of keyCode and the descriptions in the file. I haven't figured out why this problem has yet to be understood. The system says that the two are irrelevant?
- I found that most of the key values on the two platforms are consistent, but the differences are different, and I don’t understand how to define them. What is the basis?
Summarize
The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.