Android USB Create Connection 完整过程

  private void connect2Usb() {
        Logger.t(TAG).d("connect2Usb : connect");
        // 1 获取Usbmanager
        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        // 2 获取UsbDevice
        HashMap<String, UsbDevice> usbDevices = mUsbManager.getDeviceList();
        // 3 遍历UsbDevices, 查找USB Device
        Set<Map.Entry<String, UsbDevice>> entrySet = usbDevices.entrySet();
        UsbDevice usbDevice= null;
        for (Map.Entry<String, UsbDevice> entry : entrySet) {
            Logger.t(TAG).d("connect2Usb : " + entry.getKey() + "," + entry.getValue());
            //find your usb device
            if (entry.getValue().getVendorId() == USB_VENDORID) {
                usbDevice= entry.getValue();
                break;
            }
        }

        if (usbDevice== null) {
            Logger.t(TAG).e("no usb devices");
            return;
        }

        if (usbDevice.getInterfaceCount() <= 0) {
            Logger.t(TAG).e("no usb devices interface ");
            return;
        }

        //[4] 查找Usb Interface
        mUsbInterface = usbDevice.getInterface(0);
        Logger.t(TAG).d("usb interface : " + mUsbInterface);
        //[5] 查找 Endpoints
        for (int i = 0; i < mUsbInterface.getEndpointCount(); i++) {
            UsbEndpoint endpoint = mUsbInterface.getEndpoint(i);
            if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
                mUsbEndpointIn = endpoint;
                if (mUsbEndpointIn.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                    Logger.t(TAG).d("usb bulk transfer");
                }
            }

            if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
                mUsbEndpointOut = endpoint;
            }
            //for test
            int direction = endpoint.getDirection();
            Logger.d("direction " + direction);
        }

        // [5] 检查usb权限
        boolean hasPermission = mUsbManager.hasPermission(usbDevice);
        if (!hasPermission) {
            Logger.t(TAG).e("no usb permission");
            //[7] 申请usb权限
            PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 9898, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT);
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            registerReceiver(mUsbReceiver, filter);
            mUsbManager.requestPermission(usbDevice, mPermissionIntent);
            return;
        }

        //[8] Open usb Device, create connection
        if (mUsbDeviceConnection == null) {
            mUsbDeviceConnection = mUsbManager.openDevice(usbDevice);
        }

        //[9] claim UsbInterface
        if (mUsbDeviceConnection != null) {
            boolean claimUsb = mUsbDeviceConnection.claimInterface(mUsbInterface, true);
            if (claimUsb) {
                Logger.t(TAG).d("claimed usbinterface");
            }
        }


        //开始USB Communication。
        //...
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值