请叫我杨工 2018-04-20 02:42 采纳率: 0%
浏览 2238
已结题

使用Android usb Host 调用claimInterface时崩溃

  @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条回答 默认 最新

  • devmiao 2018-04-20 02:50
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包