android usb HID意锐键盘sdk封装


public class UsbKeyBoard {
    private static volatile UsbKeyBoard usbKeyBoard;
    private  UsbEndpoint inEndpoint;
    private  UsbEndpoint outEndpoint;
    private  UsbDeviceConnection connection;

    private  KeyBoardListener keyBoardListener;

    private  boolean isNeedRead=false;

    private  boolean isConnect=false;

    public  static UsbKeyBoard getInstance(Context context) {
        if (usbKeyBoard == null) {
            synchronized (UsbKeyBoard.class) {
                if (usbKeyBoard == null) {
                    usbKeyBoard = new UsbKeyBoard();
                    /*try {
                        if (!openUsb(context)){
                            return null;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }*/
                }
            }
        }
        return usbKeyBoard;
    }


    public  boolean openUsb(Context context) throws Exception {
        UsbManager usbManager = (UsbManager) context.getApplicationContext().getSystemService(Context.USB_SERVICE);
        if (usbManager == null) {
            //throw new Exception("no usb service");
            ToastUtil.showToastWithImg("暂无键盘设备", R.drawable.ico_fail);
            return false;
        }else {
            //获取usb设备列表
            Map<String, UsbDevice> devices = usbManager.getDeviceList();
            if (devices.size()==0){
                ToastUtil.showToastWithImg("暂无键盘设备", R.drawable.ico_fail);
                return false;
            }else {
                for (UsbDevice device : devices.values()) {
                    //判断是否为意锐键盘
                    if (device.getProductName().equals("INSPIRY READER")){
                        UsbInterface usbInterface = device.getInterface(0);
                        //USBEndpoint为读写数据所需的节点
                        inEndpoint = usbInterface.getEndpoint(0);  //读数据节点
                        outEndpoint = usbInterface.getEndpoint(1); //写数据节点
                        connection = usbManager.openDevice(device);
                        if (!connection.claimInterface(usbInterface, true)) {
                            Log.e("键盘连接","失败");
                            return false;
                        }
                        if (Build.VERSION.SDK_INT >= 21) {
                            connection.setInterface(usbInterface);
                        }
                        break;
                    }
                }
                this.keyBoardListener=keyBoardListener;
                new ReadThread().start();
                return true;
            }
        }
    }

    public void regist(KeyBoardListener keyBoardListener){
        this.keyBoardListener=keyBoardListener;
        new ReadThread().start();
    }

    public void unRegist(){
        this.keyBoardListener=null;
        isNeedRead=false;
    }

     class ReadThread extends Thread{
        public void run() {
            while (AppApplication.getInstance().isConnectHID()){
                byte[] buffer = new byte[8];

                    try {
                        int bytesRead = connection.bulkTransfer(inEndpoint, buffer, 8, -1);
                        Log.e("inEndpoint",inEndpoint+"");
                        if (bytesRead < 8) {
                        buffer = Arrays.copyOfRange(buffer, 0, bytesRead);
                        //Log.e("是否插上键盘", AppApplication.getInstance().isConnectHID()+"");
                        }
                    }catch (Exception e){
                        handler.sendEmptyMessage(0);
                        return;
                    }

                Log.d("全部",HexBin.encode(buffer).substring(4,6));
                if (!HexBin.encode(buffer).substring(4,6).equals("00")){
                    keyBoardListener.read(Integer.parseInt(HexBin.encode(buffer).substring(4,6),16));
                }
            }
        }
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ToastUtil.showToastWithImg("键盘已拔出",R.drawable.ico_fail);
        }
    };

    public void Write(byte[] data){
        try{
            int result=connection.controlTransfer(0x21, 0x09, 0x02, 0, data, 2, 20000);
            if (result!=-1){
                Log.e("发送键盘指令result",result+"");
            }else {
                Log.e("发送键盘指令result",result+"");
            }
        }catch (Exception e){
            handler.sendEmptyMessage(0);
            return;
        }

    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你丶快乐吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值