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

Android設(shè)備藍(lán)牙連接掃描槍獲取掃描內(nèi)容

 更新時間:2021年09月28日 15:27:34   作者:weixin_40283969  
這篇文章主要為大家詳細(xì)介紹了Android設(shè)備藍(lán)牙連接掃描槍獲取掃描內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

條形掃描槍主要可以掃描條形碼和二維碼等,掃描速度比手機(jī)掃描設(shè)備快得多,本文簡單介紹android 通過藍(lán)牙監(jiān)聽藍(lán)牙連接,當(dāng)掃描設(shè)備連接完成后,掃描設(shè)備相當(dāng)于外接鍵盤,通過監(jiān)聽外接鍵盤輸入事件,獲取掃描出的內(nèi)容。
其他參照文檔:Android設(shè)備獲取掃碼槍掃描內(nèi)容

1.藍(lán)牙配對

打開系統(tǒng)設(shè)置,藍(lán)牙配對掃描槍, 一般掃描槍說明書都有寫,配對完成后,顯示已連接

2.AndroidManifest中配置權(quán)限

在中配置藍(lán)牙連接所需要的權(quán)限

<!-- 藍(lán)牙 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

3.獲取設(shè)備信息,判斷是否連接

這里藍(lán)牙設(shè)備返回的設(shè)備類型是BluetoothClass.Device.Major.PERIPHERAL

/**
     * 掃描槍是否連接
     * @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;
}

4.構(gòu)建掃描槍解析類ScanGunKeyEventHelper

使用掃描槍解析類需要在相關(guān)類中調(diào)用 analysisKeyEvent(KeyEvent event) ,傳入監(jiān)聽事件,當(dāng)解析相對應(yīng)事件獲得輸入內(nèi)容,通過OnScanSuccessListener 接口回調(diào)返回

/**
 * 掃碼槍事件解析類 by chen
 */
public class ScanGunKeyEventHelper {
    private final static long MESSAGE_DELAY = 500;//延遲500ms,判斷掃碼是否完成。
    private StringBuffer mStringBufferResult;//掃碼內(nèi)容
    private boolean mCaps;//大小寫區(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 ;
        //獲取系統(tǒng)藍(lán)牙適配器管理類
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//        BluetoothDevice printerdevice = mBluetoothAdapter.getRemoteDevice("ssss");
//        BluetoothSocket btSocket = printerdevice.createRfcommSocketToServiceRecord("ssss");
        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();
        //字母大小寫判斷
        checkLetterStatus(event);
        if (event.getAction() == KeyEvent.ACTION_DOWN) {

            char aChar = getInputCode(event);;

            if (aChar != 0) {
                mStringBufferResult.append(aChar);
            }
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                //若為回車鍵,直接返回
                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鍵,表示大寫
                mCaps = true;
            } else {
                //松開shift鍵,表示小寫
                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 {
            //其他符號
            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ī)如三星,無法使用該方法
//    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獲取的名字錯誤)
     * @param event
     * @return
     */
    @Deprecated
    public boolean isScanGunEvent(KeyEvent event) {
        return event.getDevice().getName().equals(mDeviceName);
}


    /**
     * 檢測藍(lán)牙是否開啟
     */
    public int checkBluetoothValid() {

        if(mBluetoothAdapter == null) {//你的設(shè)備不具備藍(lán)牙功能!
            return 1;
        }

        if(!mBluetoothAdapter.isEnabled()) {//藍(lán)牙設(shè)備未打開,請開啟此功能后重試!
            return 2;
        }
        return 3;//藍(lán)牙正常工作
    }


}

5.在Activity中使用解析類ScanGunKeyEventHelper

Activity中重寫dispatchKeyEvent方法,截取Key事件。

  /**
     * 截獲按鍵事件.發(fā)給ScanGunKeyEventHelper
     *
     * @param event
     * @return
     */
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (mScanGunKeyEventHelper.isScanGunEvent(event)) {
            mScanGunKeyEventHelper.analysisKeyEvent(event);
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

獲取掃描結(jié)果回調(diào),詳細(xì)代碼請查看TestScanner

/**
 * @author Wu JianCheng
 * @date on   2018/12/18 14:44
 * 測試藍(lán)牙連接掃描槍功能
 */
public class BleAct extends Activity implements ScanGunKeyEventHelper.OnScanSuccessListener {

    ...
    
    /**
     * 掃描結(jié)果回調(diào)
     * @param barcode
     */
    @Override
    public void onScanSuccess(String barcode) {
        showToast(barcode);
    }
    
    ...

}

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

相關(guān)文章

最新評論