iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >android商户扫码枪读取手机二维码
  • 319
分享到

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

2024-04-02 19:04:59 319人浏览 安东尼
摘要

扫码抢实现读取二维码信息,本地扫码枪是外接写入设备,本质是监控读写输入,下面介绍下扫码设备读取支付二维码。 1.引入扫码设备辅助类 public class ScanGunKey

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

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);
    }
 
 
    
    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;
//    }
 
//    
//    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;
//
//    }
 
    
    private boolean isInputDeviceExist(String deviceName) {
        int[] deviceIds = InputDevice.getDeviceIds();
 
        for (int id : deviceIds) {
            if (InputDevice.getDevice(id).getName().equals(deviceName)) {
                return true;
            }
        }
        return false;
    }
 
    
    @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,否则扫码枪事件传递到屏幕其他按钮上

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

--结束END--

本文标题: android商户扫码枪读取手机二维码

本文链接: https://www.lsjlt.com/news/137051.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作