Arduino和android USB主机:openDevice方法返回null

Arduino和android USB主机:openDevice方法返回null,android,api,arduino,usb,host,Android,Api,Arduino,Usb,Host,我正在开发一个应用程序,使用android的USB主机API通过USB接口将arduino duemilanove板与android 4.1.1智能手机连接起来。我使用了windows的USB视图应用程序来查找arduino duemelanove板的USB描述符。通过使用这些描述符,我可以在android平台上找到端点和接口。描述词如下: Device Descriptor: bcdUSB: 0x0200 bDeviceClass: 0x00 bDevi

我正在开发一个应用程序,使用android的USB主机API通过USB接口将arduino duemilanove板与android 4.1.1智能手机连接起来。我使用了windows的USB视图应用程序来查找arduino duemelanove板的USB描述符。通过使用这些描述符,我可以在android平台上找到端点和接口。描述词如下:

Device Descriptor:
bcdUSB:             0x0200
bDeviceClass:         0x00
bDeviceSubClass:      0x00
bDeviceProtocol:      0x00
bMaxPacketSize0:      0x08 (8)
idVendor:           0x0403 (Future Technology Devices International Limited)
idProduct:          0x6001
bcdDevice:          0x0600
iManufacturer:        0x01
0x0409: "FTDI"
iProduct:             0x02
0x0409: "FT232R USB UART"
0x0409: "FT232R USB UART"
iSerialNumber:        0x03
0x0409: "A9GJFH5T"
bNumConfigurations:   0x01

Configuration Descriptor:
wTotalLength:       0x0020
bNumInterfaces:       0x01
bConfigurationValue:  0x01
iConfiguration:       0x00
bmAttributes:         0xA0 (Bus Powered Remote Wakeup)
MaxPower:             0x2D (90 Ma)

Interface Descriptor:
bInterfaceNumber:     0x00
bAlternateSetting:    0x00
bNumEndpoints:        0x02
bInterfaceClass:      0xFF
bInterfaceSubClass:   0xFF
bInterfaceProtocol:   0xFF
iInterface:           0x02
0x0409: "FT232R USB UART"
0x0409: "FT232R USB UART"

Endpoint Descriptor:
bEndpointAddress:     0x81
Transfer Type:        Bulk
wMaxPacketSize:     0x0040 (64)
bInterval:            0x00

Endpoint Descriptor:
bEndpointAddress:     0x02
Transfer Type:        Bulk
wMaxPacketSize:     0x0040 (64)
bInterval:            0x00
package com.example.arduinobasic;

import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;

    public class ArduinoParams {


        public String devinfoRet(UsbDevice device) {
            String DevInfo = "";
            DevInfo += "Device Name:" + device.getDeviceName();
            DevInfo += "Device Id:" + device.getDeviceId();
            DevInfo += "Product Id:" + device.getProductId();
            DevInfo += "Vendor Id:" + device.getProductId();
            DevInfo += "Device Class:" + device.getDeviceClass();
            DevInfo += "Device Subclass" + device.getDeviceSubclass();
            DevInfo += "Device Protocol:" + device.getDeviceProtocol() + "\n";

            return DevInfo;

        }

        public UsbInterface findIntf(UsbDevice device) {
            UsbInterface intf = null;

            for (int i = 0; i < device.getInterfaceCount(); i++) {
                if (device.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC
                        && device.getInterface(i).getInterfaceSubclass() == 255
                        && device.getInterface(i).getInterfaceProtocol() == 255) {
                    intf = device.getInterface(i);
                }

            }
            return intf;

        }




        public UsbEndpoint findepIn(UsbInterface intf) {
            UsbEndpoint epin = null;

            for (int i = 0; i < intf.getEndpointCount(); i++) {
                if ((intf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
                        && (intf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
                    epin = intf.getEndpoint(i);
                }

            }

            return epin;
        }

        public UsbEndpoint findepOut(UsbInterface intf) {
            UsbEndpoint epout = null;

            for (int i = 0; i < intf.getEndpointCount(); i++) {
                if ((intf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_OUT)
                        && (intf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
                    epout = intf.getEndpoint(i);
                }

            }

            return epout;
        }
        public String intfPara(UsbInterface intf) {
            String intfPara = "";

            intfPara += "Interface id:" + intf.getId();
            intfPara += "Interface Class:" + intf.getInterfaceClass();
            intfPara += "Interface Subclass:" + intf.getInterfaceSubclass();
            intfPara += "Interface Protocol:" + intf.getInterfaceProtocol() + "\n";

            return intfPara;
        }
        public String epPara(UsbEndpoint ep) {
            String epPara = "";

            epPara += "Endpoint Address:" + ep.getAddress();
            epPara += "Endpoint Attributes:" + ep.getAttributes();
            epPara += "Endpoint Direction" + ep.getDirection();
            epPara += "Endpoint Number:" + ep.getEndpointNumber();
            epPara += "Endpoint max pckt size :" + ep.getMaxPacketSize();
            epPara += "Endpoint Interval :" + ep.getInterval();
            epPara += "Endpoint Type:" + ep.getType() + "\n";

            return epPara;
        }
    }
应用程序检测设备并在指定的文本视图中显示所有描述符。但问题是-当我尝试使用android USB主机API的UsbManager类的
UsbManager.openDevice(UsbDevice)
方法打开设备时,我发现该方法在
UsbDeviceConnection
变量中返回空值 这是实现打开USBC连接的所有方法的线程

private class SetupThread extends Thread {

    public void run() {

        String devParam = "";
        String intfepParam = "";
        // display device descriptor
        devParam = ardParam.devinfoRet(arduino);
        devDscrptor.setText(devParam);

        intf = ardParam.findIntf(arduino);

        if (intf != null) {

            intfepParam = ardParam.intfPara(intf);
            log1.setText("Interface found");

            epIn = ardParam.findepIn(intf);
            epOut = ardParam.findepOut(intf);

            if (epIn != null && epOut != null) {
                log2.setText("Both endpoints found");
                intfepParam += ardParam.epPara(epIn);
                intfepParam += ardParam.epPara(epOut);
                intfepDscrptor.setText(intfepParam);

                // just checking if Usbdevice arduino is null
                if (arduino != null) {
                  // this is where the problem comes
                    mConnection = mUsbManager.openDevice(arduino);
                    conxnInfo.setText("Arduino not null");
                }

                if (mConnection != null) {
                    mConnection.claimInterface(intf, false);
                    conxnInfo.setText("Connection opened");
                }

            }

            else {
                if ((epIn == null) && (epOut == null)) {
                    intfepDscrptor.setText(intfepParam);
                    log2.setText("Both endpoints null");

                }

                else if ((epIn == null) && (epOut != null)) {
                    intfepParam += ardParam.epPara(epOut);
                    intfepDscrptor.setText(intfepParam);
                    log2.setText("epIn  null");

                }

                else {
                    intfepParam += ardParam.epPara(epIn);
                    intfepDscrptor.setText(intfepParam);
                    log2.setText("epOut  null");

                }

            }

        } else {
            log1.setText("Interface is null");
        }

    }
}
ardParam是ArduInParams类的对象,ArduInParams类的代码如下:

Device Descriptor:
bcdUSB:             0x0200
bDeviceClass:         0x00
bDeviceSubClass:      0x00
bDeviceProtocol:      0x00
bMaxPacketSize0:      0x08 (8)
idVendor:           0x0403 (Future Technology Devices International Limited)
idProduct:          0x6001
bcdDevice:          0x0600
iManufacturer:        0x01
0x0409: "FTDI"
iProduct:             0x02
0x0409: "FT232R USB UART"
0x0409: "FT232R USB UART"
iSerialNumber:        0x03
0x0409: "A9GJFH5T"
bNumConfigurations:   0x01

Configuration Descriptor:
wTotalLength:       0x0020
bNumInterfaces:       0x01
bConfigurationValue:  0x01
iConfiguration:       0x00
bmAttributes:         0xA0 (Bus Powered Remote Wakeup)
MaxPower:             0x2D (90 Ma)

Interface Descriptor:
bInterfaceNumber:     0x00
bAlternateSetting:    0x00
bNumEndpoints:        0x02
bInterfaceClass:      0xFF
bInterfaceSubClass:   0xFF
bInterfaceProtocol:   0xFF
iInterface:           0x02
0x0409: "FT232R USB UART"
0x0409: "FT232R USB UART"

Endpoint Descriptor:
bEndpointAddress:     0x81
Transfer Type:        Bulk
wMaxPacketSize:     0x0040 (64)
bInterval:            0x00

Endpoint Descriptor:
bEndpointAddress:     0x02
Transfer Type:        Bulk
wMaxPacketSize:     0x0040 (64)
bInterval:            0x00
package com.example.arduinobasic;

import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;

    public class ArduinoParams {


        public String devinfoRet(UsbDevice device) {
            String DevInfo = "";
            DevInfo += "Device Name:" + device.getDeviceName();
            DevInfo += "Device Id:" + device.getDeviceId();
            DevInfo += "Product Id:" + device.getProductId();
            DevInfo += "Vendor Id:" + device.getProductId();
            DevInfo += "Device Class:" + device.getDeviceClass();
            DevInfo += "Device Subclass" + device.getDeviceSubclass();
            DevInfo += "Device Protocol:" + device.getDeviceProtocol() + "\n";

            return DevInfo;

        }

        public UsbInterface findIntf(UsbDevice device) {
            UsbInterface intf = null;

            for (int i = 0; i < device.getInterfaceCount(); i++) {
                if (device.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC
                        && device.getInterface(i).getInterfaceSubclass() == 255
                        && device.getInterface(i).getInterfaceProtocol() == 255) {
                    intf = device.getInterface(i);
                }

            }
            return intf;

        }




        public UsbEndpoint findepIn(UsbInterface intf) {
            UsbEndpoint epin = null;

            for (int i = 0; i < intf.getEndpointCount(); i++) {
                if ((intf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
                        && (intf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
                    epin = intf.getEndpoint(i);
                }

            }

            return epin;
        }

        public UsbEndpoint findepOut(UsbInterface intf) {
            UsbEndpoint epout = null;

            for (int i = 0; i < intf.getEndpointCount(); i++) {
                if ((intf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_OUT)
                        && (intf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
                    epout = intf.getEndpoint(i);
                }

            }

            return epout;
        }
        public String intfPara(UsbInterface intf) {
            String intfPara = "";

            intfPara += "Interface id:" + intf.getId();
            intfPara += "Interface Class:" + intf.getInterfaceClass();
            intfPara += "Interface Subclass:" + intf.getInterfaceSubclass();
            intfPara += "Interface Protocol:" + intf.getInterfaceProtocol() + "\n";

            return intfPara;
        }
        public String epPara(UsbEndpoint ep) {
            String epPara = "";

            epPara += "Endpoint Address:" + ep.getAddress();
            epPara += "Endpoint Attributes:" + ep.getAttributes();
            epPara += "Endpoint Direction" + ep.getDirection();
            epPara += "Endpoint Number:" + ep.getEndpointNumber();
            epPara += "Endpoint max pckt size :" + ep.getMaxPacketSize();
            epPara += "Endpoint Interval :" + ep.getInterval();
            epPara += "Endpoint Type:" + ep.getType() + "\n";

            return epPara;
        }
    }
package com.example.arduinobasic;
导入android.hardware.usb.UsbConstants;
导入android.hardware.usb.UsbDevice;
导入android.hardware.usb.UsbEndpoint;
导入android.hardware.usb.usb接口;
公共类ArdunoParams{
公共字符串DeviceForet(UsbDevice设备){
字符串DevInfo=“”;
设备名称:“+Device.getDeviceName()”;
DevInfo+=“设备Id:+Device.getDeviceId();
DevInfo+=“产品Id:+device.getProductId();
DevInfo+=“供应商Id:+device.getProductId();
DevInfo+=“设备类:”+Device.getDeviceClass();
DevInfo+=“设备子类”+设备.getDeviceSubclass();
设备协议:“+Device.getDeviceProtocol()+”\n”;
返回DevInfo;
}
公用USB接口findIntf(USB设备){
UsbInterface intf=null;
对于(int i=0;i
很抱歉使用了很多if-else语句。我希望有人能帮我解决这个问题。提前谢谢