Android连接打印机时,获取USB权限

/**
 * 获得 usb 权限
 */
private void openUsbDevice(){
    //before open usb device
    //should try to get usb permission
    tryGetUsbPermission();
}
UsbManager mUsbManager;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";

private void tryGetUsbPermission(){
    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbPermissionActionReceiver, filter);

    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

    //here do emulation to ask all connected usb device for permission
    for (final UsbDevice usbDevice : mUsbManager.getDeviceList().values()) {
        //add some conditional check if necessary
        //if(isWeCaredUsbDevice(usbDevice)){
        if(mUsbManager.hasPermission(usbDevice)){
            //if has already got permission, just goto connect it
            //that means: user has choose yes for your previously popup window asking for grant perssion for this usb device
            //and also choose option: not ask again
            afterGetUsbPermission(usbDevice);
        }else{
            //this line will let android popup window, ask user whether to allow this app to have permission to operate this usb device
            mUsbManager.requestPermission(usbDevice, mPermissionIntent);
        }
        //}
    }
}


private void afterGetUsbPermission(UsbDevice usbDevice){
    //call method to set up device communication
    //Toast.makeText(this, String.valueOf("Got permission for usb device: " + usbDevice), Toast.LENGTH_LONG).show();
    //Toast.makeText(this, String.valueOf("Found USB device: VID=" + usbDevice.getVendorId() + " PID=" + usbDevice.getProductId()), Toast.LENGTH_LONG).show();

    doYourOpenUsbDevice(usbDevice);
}

private void doYourOpenUsbDevice(UsbDevice usbDevice){
    //now follow line will NOT show: User has not given permission to device UsbDevice
    UsbDeviceConnection connection = mUsbManager.openDevice(usbDevice);
    //add your operation code here
}

private final BroadcastReceiver mUsbPermissionActionReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    //user choose YES for your previously popup window asking for grant perssion for this usb device
                    if(null != usbDevice){
                        afterGetUsbPermission(usbDevice);
                    }
                }
                else {
                    //user choose NO for your previously popup window asking for grant perssion for this usb device
                    Toast.makeText(context, String.valueOf("Permission denied for device" + usbDevice), Toast.LENGTH_LONG).show();
                }
            }
        }
    }
};
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值