欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android商戶(hù)掃碼槍讀取手機(jī)二維碼

 更新時(shí)間:2021年09月28日 17:11:19   作者:Lewis_ll  
這篇文章主要為大家詳細(xì)介紹了android商戶(hù)掃碼槍讀取手機(jī)二維碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

掃碼搶實(shí)現(xiàn)讀取二維碼信息,本地掃碼槍是外接寫(xiě)入設(shè)備,本質(zhì)是監(jiān)控讀寫(xiě)輸入,下面介紹下掃碼設(shè)備讀取支付二維碼。

1.引入掃碼設(shè)備輔助類(lèi)

public class ScanGunKeyEventHelper {
 
    private final static long MESSAGE_DELAY = 500;             //延遲500ms,判斷掃碼是否完成。
    private StringBuffer mStringBufferResult;                  //掃碼內(nèi)容
    private boolean mCaps;                                     //大小寫(xiě)區(qū)分
    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();
            }
        };
    }
 
 
    /**
     * 返回掃碼成功后的結(jié)果
     */
    private void performScanSuccess() {
        String barcode = mStringBufferResult.toString();
        if (mOnScanSuccessListener != null)
            mOnScanSuccessListener.onScanSuccess(barcode);
        mStringBufferResult.setLength(0);
    }
 
 
    /**
     * 掃碼槍事件解析
     * @param event
     */
    public void analysisKeyEvent(KeyEvent event) {
 
        int keyCode = event.getKeyCode();
 
        //字母大小寫(xiě)判斷
        checkLetterStatus(event);
 
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
 
            char aChar = getInputCode(event);;
 
            if (aChar != 0) {
                mStringBufferResult.append(aChar);
            }
 
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                //若為回車(chē)鍵,直接返回
                mHandler.removeCallbacks(mScanningFishedRunnable);
                mHandler.post(mScanningFishedRunnable);
            } else {
                //延遲post,若500ms內(nèi),有其他事件
                mHandler.removeCallbacks(mScanningFishedRunnable);
                mHandler.postDelayed(mScanningFishedRunnable, MESSAGE_DELAY);
            }
 
        }
    }
    //檢查shift鍵
    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) {
                //按著shift鍵,表示大寫(xiě)
                mCaps = true;
            } else {
                //松開(kāi)shift鍵,表示小寫(xiě)
                mCaps = false;
            }
        }
    }
 
    //獲取掃描內(nèi)容
    private char getInputCode(KeyEvent event) {
 
        int keyCode = event.getKeyCode();
 
        char aChar;
 
        if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
            //字母
            aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
        } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
            //數(shù)字
            aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);
        } else {
            //其他符號(hào)
            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;
    }
 
 
    //部分手機(jī)如三星,無(wú)法使用該方法
//    private void hasScanGun() {
//        Configuration cfg = getResources().getConfiguration();
//        return cfg.keyboard != Configuration.KEYBOARD_NOKEYS;
//    }
 
//    /**
//     * 掃描槍是否連接
//     * @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;
//
//    }
 
    /**
     * 輸入設(shè)備是否存在
     * @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;
    }
 
    /**
     * 是否為掃碼槍事件(部分機(jī)型KeyEvent獲取的名字錯(cuò)誤)
     * @param event
     * @return
     */
    @Deprecated
    public boolean isScanGunEvent(KeyEvent event) {
        return event.getDevice().getName().equals(mDeviceName);
    }
 
}

2. active里面實(shí)現(xiàn)代理方法

 //實(shí)現(xiàn)上述類(lèi)接口‘
public class MainActivity extends AppCompatActivity implements
        ScanGunKeyEventHelper.OnScanSuccessListener
//重寫(xiě)掃碼槍識(shí)別返回?cái)?shù)據(jù)
@Override
       public void onScanSuccess(String barcode) {
 
        barCode = barcode;
        if (barcode != null && recordPrice > 0 && payString.equals
                ("readyPay")) {
            payDishs();
        }
    }
 
//重寫(xiě)捕捉到掃碼槍事件
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
 
    mScanGunKeyEventHelper.analysisKeyEvent(event);
        return true;
    }

dispatchKeyEvent里面分發(fā)事件一定設(shè)置 return true,否則掃碼槍事件傳遞到屏幕其他按鈕上

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論