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

时间:2014-07-16 05:48:47

标签: android api arduino usb host

我正在开发一个应用程序,使用Android的USB主机API通过USB接口将arduino duemilanove板与android 4.1.1智能手机连接。我已经使用windows的USB View应用程序来查找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

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

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是ArduinoParams类的对象,ArduinoParams类的代码如下:

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;
        }
    }

很抱歉使用了很多if-else语句。我希望有人帮我解决这个问题。提前谢谢。

0 个答案:

没有答案