欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android商户扫码枪读取手机二维码

程序员文章站 2022-03-22 11:13:02
扫码抢实现读取二维码信息,本地扫码枪是外接写入设备,本质是监控读写输入,下面介绍下扫码设备读取支付二维码。1.引入扫码设备辅助类public class scangunkeyeventhelper {...

扫码抢实现读取二维码信息,本地扫码枪是外接写入设备,本质是监控读写输入,下面介绍下扫码设备读取支付二维码。

1.引入扫码设备辅助类

public class scangunkeyeventhelper {
 
    private final static long message_delay = 500;             //延迟500ms,判断扫码是否完成。
    private stringbuffer mstringbufferresult;                  //扫码内容
    private boolean mcaps;                                     //大小写区分
    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();
            }
        };
    }
 
 
    /**
     * 返回扫码成功后的结果
     */
    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内,有其他事件
                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;
            }
        }
    }
 
    //获取扫描内容
    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) {
            //数字
            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;
    }
 
 
    //部分手机如三星,无法使用该方法
//    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;
//
//    }
 
    /**
     * 输入设备是否存在
     * @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;
    }
 
    /**
     * 是否为扫码枪事件(部分机型keyevent获取的名字错误)
     * @param event
     * @return
     */
    @deprecated
    public boolean isscangunevent(keyevent event) {
        return event.getdevice().getname().equals(mdevicename);
    }
 
}

2. active里面实现代理方法

 //实现上述类接口‘
public class mainactivity extends appcompatactivity implements
        scangunkeyeventhelper.onscansuccesslistener
//重写扫码枪识别返回数据
@override
       public void onscansuccess(string barcode) {
 
        barcode = barcode;
        if (barcode != null && recordprice > 0 && paystring.equals
                ("readypay")) {
            paydishs();
        }
    }
 
//重写捕捉到扫码枪事件
    @override
    public boolean dispatchkeyevent(keyevent event) {
 
    mscangunkeyeventhelper.analysiskeyevent(event);
        return true;
    }

dispatchkeyevent里面分发事件一定设置 return true,否则扫码枪事件传递到屏幕其他按钮上

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。