android merchant scan code gun reads mobile phone QR code
- 2021-12-21 04:59:51
- OfStack
Scan code to read 2-D code information. The local scan code gun is an external writing device, which is essentially to monitor read-write input. The following describes the scan code device to read and pay 2-D code.
1. Introduce the auxiliary class of code scanning equipment
public class ScanGunKeyEventHelper {
private final static long MESSAGE_DELAY = 500; // Delay 500ms And judging whether the code scanning is completed.
private StringBuffer mStringBufferResult; // Scan code content
private boolean mCaps; // Case sensitivity
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();
}
};
}
/**
* Return the result after successful code scanning
*/
private void performScanSuccess() {
String barcode = mStringBufferResult.toString();
if (mOnScanSuccessListener != null)
mOnScanSuccessListener.onScanSuccess(barcode);
mStringBufferResult.setLength(0);
}
/**
* Analysis of Code Scanning Gun Event
* @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 Enter key, return directly
mHandler.removeCallbacks(mScanningFishedRunnable);
mHandler.post(mScanningFishedRunnable);
} else {
// Delay post If 500ms Inside, there are other events
mHandler.removeCallbacks(mScanningFishedRunnable);
mHandler.postDelayed(mScanningFishedRunnable, MESSAGE_DELAY);
}
}
}
// Check 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 shift Key, representing uppercase
mCaps = true;
} else {
// Loosen shift Key for lowercase
mCaps = false;
}
}
}
// Get scanned content
private char getInputCode(KeyEvent event) {
int keyCode = event.getKeyCode();
char aChar;
if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
// Alphabet
aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
} else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
// Figures
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() {
mHandler.removeCallbacks(mScanningFishedRunnable);
mOnScanSuccessListener = null;
}
// Some mobile phones such as 3 Star, this method cannot be used
// private void hasScanGun() {
// Configuration cfg = getResources().getConfiguration();
// return cfg.keyboard != Configuration.KEYBOARD_NOKEYS;
// }
// /**
// * Is the scanning gun connected
// * @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 isInputDeviceExist(mDeviceName);
// }
//
// }
//
// return false;
//
// }
/**
* Does the input device exist
* @param deviceName
* @return
*/
private boolean isInputDeviceExist(String deviceName) {
int[] deviceIds = InputDevice.getDeviceIds();
for (int id : deviceIds) {
if (InputDevice.getDevice(id).getName().equals(deviceName)) {
return true;
}
}
return false;
}
/**
* Is it a code scanning gun event ( Partial models KeyEvent Wrong name obtained )
* @param event
* @return
*/
@Deprecated
public boolean isScanGunEvent(KeyEvent event) {
return event.getDevice().getName().equals(mDeviceName);
}
}
2. Implementation of proxy method in active
// Implement the above class interface '
public class MainActivity extends AppCompatActivity implements
ScanGunKeyEventHelper.OnScanSuccessListener
// Rewrite the code scanning gun to identify the returned data
@Override
public void onScanSuccess(String barcode) {
barCode = barcode;
if (barcode != null && recordPrice > 0 && payString.equals
("readyPay")) {
payDishs();
}
}
// Rewrite captures code scanning gun event
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
mScanGunKeyEventHelper.analysisKeyEvent(event);
return true;
}
dispatchKeyEvent distributes event 1 to set return true, otherwise the code scanning gun event will be transferred to other buttons on the screen