使用Android usb Host 调用claimInterface时崩溃 10C
  @Override
    public void onResume() {
        super.onResume();
         mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
       // mUsbManager.requestPermission(device, mPermissionIntent);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(mUsbReceiver, filter);
    }
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mUsbReceiver);
    }
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if(device != null){
                            //call method to set up device communication

                            Log.d(TAG,"usb EXTRA_PERMISSION_GRANTED");
                        }
                    }
                    else {
                        Log.d(TAG,"permission denied for device"+device);
                    }
                }
            }
        }
    };

    public int initUsb(){
        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while(deviceIterator.hasNext()){
            device = deviceIterator.next();
            //your code
            if(mVendorID == device.getVendorId() && mProductID == device.getProductId()) {
                if(mUsbManager.hasPermission(device)) {

                } else {
                    mUsbManager.requestPermission(device,mPermissionIntent);
                    break;
                }
                break;
            } else {
                device = null;
            }
        }
        return 0;
    }

    private int initCommunication() {
        /*
        if(mVendorID == device.getVendorId() && mProductID == device.getProductId()) {
            int interfaceCount = device.getInterfaceCount();
            for (int interfaceIndex = 0; interfaceIndex < interfaceCount; interfaceIndex++) {
                UsbInterface usbInterface = device.getInterface(interfaceIndex);

                for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
                    UsbEndpoint ep = usbInterface.getEndpoint(i);
                    if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                        if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
                            mUsbEndpointOut = ep;
                        } else {
                            mUsbEndpointIn = ep;
                        }
                    }
                }

                if ((null == mUsbEndpointIn) || (null == mUsbEndpointOut)) {
                    mUsbEndpointIn = null;
                    mUsbEndpointOut = null;
                    mUsbInterface = null;
                } else {
                    mUsbInterface = usbInterface;
                    mUsbDeviceConnection = mUsbManager.openDevice(device);
                    if(mUsbDeviceConnection==null){
                        Log.d(TAG, " connection is null!");
                        return -1;
                    }
                    if(mUsbDeviceConnection!=null&&mUsbDeviceConnection.claimInterface(mUsbInterface, true)) {
                        Log.d(TAG, " connection succeeded!");
                    }else{
                        Log.d(TAG, " connection failed!");
                    }
                    break;
                }
            }
        }*/

        if(device == null) {
            Log.d(TAG, "Please insert USB flash disk!");
            return -1;
        }
        if(device.getInterfaceCount() != 1) {
            Log.d(TAG, "Not a USB flash disk!");
            return-2;
        }
        UsbInterface intf = device.getInterface(0);
        if(intf.getEndpointCount() != 2) {
            Log.d(TAG, "Not a USB flash disk!");
            finish();
            return-3;
        } else {
            mUsbEndpointIn = intf.getEndpoint(0);   // Bulk-In端点
            mUsbEndpointOut = intf.getEndpoint(1);  // Bulk_Out端点
        }
        if (device != null) {
            UsbDeviceConnection connection = mUsbManager.openDevice(device);
            if (connection != null && connection.claimInterface(intf, true)) {
                Log.d(TAG, "Make connection succeeded!");
                mUsbDeviceConnection = connection;
                mUsbInterface = intf;
            } else {
                Log.d(TAG, "Make connection failed!");
                mUsbDeviceConnection = null;
                finish();
            }
        }
        return 0;
    }

    public int closeDev(){
        device = null;
        return 0;
    }

我调用的顺序是initUsb onResum,并在Ocreat中调用,然后调用的initCommunication。在运行过程中,程序跑到claimInterface这里就崩溃了,请各位帮我找下问题或者提出建议,谢谢大家了, 我很急~~~
initCommunication中我写了两种方法,注释掉的是第一种,在claimInterFace时崩溃,没注释的部分还在等测试结果

1个回答

qq_22701117
qq_22701117 我看过这篇文章,基本也是按照描述来写的
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!