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

Android設(shè)備獲取掃碼槍掃描內(nèi)容

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

條形碼掃碼槍現(xiàn)在隨處可見,可以很迅速地掃描出條形碼內(nèi)容,比什么手機(jī)相機(jī)掃碼快了不是一點(diǎn)兩點(diǎn)。
為了節(jié)約成本,掃碼槍可以直接通過藍(lán)牙連接android或其他設(shè)備。

那么android設(shè)備如何通過藍(lán)牙獲取掃描內(nèi)容的呢?

1. 藍(lán)牙配對,連接設(shè)備

打開系統(tǒng)設(shè)置,找到藍(lán)牙,打開掃碼槍,配對掃碼槍設(shè)備。輸入一個固定的配對碼,一般掃碼槍說明書里都有寫。配對完成后,顯示設(shè)備已連接。就ok。

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

android項(xiàng)目中的AndroidManifest.xml文件添加藍(lán)牙權(quán)限。

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

3.檢測掃碼槍的連接狀態(tài)

通常來說,掃碼槍設(shè)備也相當(dāng)于普通外接輸入設(shè)備類型,外接鍵盤。

我這款掃碼槍設(shè)備返回的是如下藍(lán)牙類型。

BluetoothClass.Device.Major.PERIPHERAL

一般而言,通過如下這種方式就可以獲得到我們掃碼槍設(shè)備的信息。

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) {
        //TODO 獲取掃碼槍設(shè)備信息
    }

}

開發(fā)過程中,必然會需要實(shí)時判斷設(shè)備是否正常連接。

mBluetoothAdapter.getBondedDevices()

這個方法僅僅只能夠判斷設(shè)備是否已配對綁定。但是綁定不代表連接,所以只能放棄。

public List getConnectedDevices (int profile)
public int getConnectionState (BluetoothDevice device, int profile)

接著又嘗試了這兩個方法,方法是可用,但是必須要求設(shè)備sdk>18,即android 4.3版本以上才可用。

后來轉(zhuǎn)頭一想,既然掃碼槍也是輸入設(shè)備,我們可以不同藍(lán)牙設(shè)備狀態(tài)檢測入手,改為從輸入設(shè)備檢測入手。于是,

private void hasScanGun() {
    Configuration cfg = getResources().getConfiguration();
    return cfg.keyboard != Configuration.KEYBOARD_NOKEYS;
}

搞定。

4.獲取掃碼槍掃描內(nèi)容

掃描槍,既然是一個外接輸入設(shè)備,那么很自然的,我們就從KeyEvent入手。

事件解析類

/**
 * 掃碼槍事件解析類
 */
public class ScanGunKeyEventHelper {

    //延遲500ms,判斷掃碼是否完成。
    private final static long MESSAGE_DELAY = 500;
    //掃碼內(nèi)容
    private StringBuffer mStringBufferResult = new StringBuffer();
    //大小寫區(qū)分
    private boolean mCaps;
    private OnScanSuccessListener mOnScanSuccessListener;
    private Handler mHandler = new Handler();

    private final Runnable 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);
    }

    //key事件處理
    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 {
        public void onScanSuccess(String barcode);
    }

    public void setOnBarCodeCatchListener(OnScanSuccessListener onScanSuccessListener) {
        mOnScanSuccessListener = onScanSuccessListener;
    }

    public void onDestroy() {
        mHandler.removeCallbacks(mScanningFishedRunnable);
        mOnScanSuccessListener = null;
    }

}

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

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

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

    /**
     * 顯示掃描內(nèi)容
     * @param barcode 條形碼
     */
    @Override
    public void onScanSuccess(String barcode) {
        //TODO 顯示掃描內(nèi)容
    }

詳細(xì)代碼參看:https://github.com/czhzero/scangon

注意點(diǎn):

1.部分機(jī)型無法判斷外接鍵盤信息,如三星。

private void hasScanGun() {
    Configuration cfg = getResources().getConfiguration();
    return cfg.keyboard != Configuration.KEYBOARD_NOKEYS;
}

三星手機(jī)cfg.keyboard返回值等于Configuration.KEYBOARD_NOKEYS。

因此為了更好的兼容,可以采用如下方法

/**
 * 判斷是否已經(jīng)連接掃碼槍
 * @return
 */
protected boolean hasScanGun() {

    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) {
            return isInputDeviceUsed(bluetoothDevice.getName());
        }

    }

    return false;

}


private boolean isInputDeviceUsed(String deviceName) {

    int[] deviceIds = InputDevice.getDeviceIds();

    for (int id : deviceIds) {
        if (InputDevice.getDevice(id).getName().equals(deviceName)) {
            return true;
        }
    }

    return false;

}

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

相關(guān)文章

  • 解析Android框架之Volley源碼

    解析Android框架之Volley源碼

    我們知道Volley是在2013年Google I/O大會上推出了一個新的網(wǎng)絡(luò)通信框架,他的設(shè)計目的就是為了那些請求數(shù)據(jù)量不是特別大,但是又是特別頻繁的網(wǎng)絡(luò)操作非常適合。但是對于數(shù)據(jù)量較大的請求,比如說下載一個較大的文件,Volley可能相比于其他的框架,就有點(diǎn)不足了。
    2021-06-06
  • Android自定義動態(tài)壁紙開發(fā)(時鐘)

    Android自定義動態(tài)壁紙開發(fā)(時鐘)

    今天小編就為大家分享一篇關(guān)于Android自定義動態(tài)壁紙開發(fā)(時鐘),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 詳解android shape的使用總結(jié)

    詳解android shape的使用總結(jié)

    在Android程序開發(fā)中,我們經(jīng)常會去用到Shape這個東西去定義各種各樣的形狀,本篇文章主要介紹了android shape的使用,有興趣的可以一起了解一下。
    2016-12-12
  • Android自定義RadioGroupX實(shí)現(xiàn)多行多列布局

    Android自定義RadioGroupX實(shí)現(xiàn)多行多列布局

    這篇文章主要為大家詳細(xì)介紹了Android自定義RadioGroupX實(shí)現(xiàn)多行多列布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Flutter Navigator路由傳參的實(shí)現(xiàn)

    Flutter Navigator路由傳參的實(shí)現(xiàn)

    本文主要介紹了Flutter Navigator路由傳參的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android實(shí)現(xiàn)手勢控制ImageView圖片大小

    Android實(shí)現(xiàn)手勢控制ImageView圖片大小

    這篇文章主要介紹了Android實(shí)現(xiàn)手勢控制ImageView圖片大小的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • Android移動開發(fā)recycleView的頁面點(diǎn)擊跳轉(zhuǎn)設(shè)計實(shí)現(xiàn)

    Android移動開發(fā)recycleView的頁面點(diǎn)擊跳轉(zhuǎn)設(shè)計實(shí)現(xiàn)

    這篇文章主要介紹了Android移動開發(fā)recycleView的頁面點(diǎn)擊跳轉(zhuǎn)設(shè)計實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Android實(shí)現(xiàn)dialog的3D翻轉(zhuǎn)示例

    Android實(shí)現(xiàn)dialog的3D翻轉(zhuǎn)示例

    這篇文章主要介紹了Android實(shí)現(xiàn)dialog的3D翻轉(zhuǎn)示例,非常具有實(shí)用價值,需要的朋友可以參考下
    2017-08-08
  • Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù)的操作方法

    Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù)的操作方法

    這篇文章主要介紹了Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù),本次也是介紹了用另外一種方法來實(shí)現(xiàn)RecyclerView高效刷新數(shù)據(jù)的功能,需要的朋友可以參考下
    2022-10-10
  • Android中關(guān)于自定義相機(jī)預(yù)覽界面拉伸問題

    Android中關(guān)于自定義相機(jī)預(yù)覽界面拉伸問題

    這篇文章主要為大家詳細(xì)介紹了Android中關(guān)于自定義相機(jī)預(yù)覽界面拉伸問題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02

最新評論