Android 盒子硬件 代码申请usb 权限 代码

/**
     * 检查iC 读卡器usb 权限
     */
    private void checkICUsbPermissson() {
        manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        // 获取一个已连接的USB设备,并且包含方法,以访问其标识信息、 接口和端点
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        if (deviceList.size() == 0) {
            LogUtil.e("--未找到IC读卡器");
            ToastUtil.show(this, "--未找到IC读卡器");
            return;
        }
        // 获取deviceList迭代器
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        // 判断迭代器中是否有元素
        while (deviceIterator.hasNext()) {
            // 如果有,获取元素
            UsbDevice usbDevice = deviceIterator.next();
            //product-id="1638" vendor-id="1137
            if (!(usbDevice.getProductId() == 1638 && usbDevice.getVendorId() == 1137)) {
//            if (!ReaderAndroidUsb.isSupported(usbDevice)) {
                Log.e("lsw", "is not Supported" + usbDevice.getDeviceName() + " -- " + usbDevice.getDeviceName());
                continue;
            }
            Log.e("lsw", " Supported" + usbDevice.getDeviceName());
            // 判断是否拥有该设备的连接权限
            if (!manager.hasPermission(usbDevice)) {
                // 如果没有则请求权限
                PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0,
                        new Intent(Device_USB), PendingIntent.FLAG_UPDATE_CURRENT);
                /*
                 * 展示征求用户同意连接这个设备的权限的对话框。 当用户回应这个对话框时,
                 * 广播接收器就会收到一个包含用一个boolean值来表示结果的EXTRA_PERMISSION_GRANTED字段的意图。
                 * 在连接设备之前检查这个字段的值是否为true和设备之间的“交流”
                 */
                manager.requestPermission(usbDevice, mPermissionIntent);
                LogUtil.e(" -- not  hasPermission IC  --  ");
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        start2XICDevice();
                    }
                }, 1000);
            } else {
                LogUtil.e(" -- hasPermission IC ");
                start2XICDevice();
            }
        }
    }

需要和硬件厂商沟通 ,知道硬件的 pid 和vid 如: product-id="1638" vendor-id="1137 

private String Device_USB = "com.android.example.USB";

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值