Android USB读写封装

一个简单的USB读写封装类,支持Android AOA和ADB读写模式,支持不同方式的读写格式(ByteBuffer和byte[])。

public class UsbDeviceManager {
    private static final String TAG = ECUsbDevice.class.getSimpleName();

    // USB device types info
    private final static int VID_APPLE = 0x05ac;
    private final static int PID_RANGE_LOW = 0x1290;
    private final static int PID_RANGE_MAX = 0x12af;
    private final static int ADB_CLASS = 0xff;
    private final static int ADB_SUBCLASS = 0x42;
    private final static int ADB_PROTOCOL = 0x1;
    private final static int AOA_CLASS = 0xff;
    private final static int AOA_SUBCLASS = 0xff;
    private final static int AOA_PROTOCOL = 0x0;

    public final static int ANDROID_USB_ADB = 1;
    public final static int ANDROID_USB_AOA = 2;
	
    private Context mContext;
    private int mType = 1;
    private UsbDevice mUsbDevice;
    private UsbDeviceConnection mConnection;
    private UsbInterface mInterface;
    private UsbEndpoint mEpIn;
    private UsbEndpoint mEpOut;
    private byte[] mReadBuf = new byte[4096];
    private byte[] mWriteBuf = new byte[4096];

    private int mTimeout = 500;

    public UsbDeviceManager(Context context, int usbType, UsbDevice device) {
        mContext = context;
        mType = type;
        mUsbDevice = device;
    }

    public int open() {
        int index = -1;
        if(mType == ANDROID_USB_ADB) {
            index = getAdbInterface();
        } else if(mType == ANDROID_USB_AOA){
            index = getAoaInterface();
        }

        if (index < 0) {
            Log.e(TAG, "open: failed to get interface for type=" + mType);
            return -1;
        }
        mInterface = mUsbDevice.getInterface(index);
        Log.d(TAG, "UsbDevices open: deviceName=" + mUsbDevice.getDeviceName() + ", mTimeout=" + mTimeout);
        UsbManager usbManager = (UsbManager) mContext.getApplicationContext().getSystemService(Context.USB_SERVICE);
        try {
            mConnection = usbManager.openDevice(mUsbDevice);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG,"open: failed to openDevice");
            return -1;
        }

        if (mConnection == null) {
            Log.e(TAG,"open: failed to openDevice");
            return -1;
        }

        // get input and output endpoint
        for (int i = 0; i < mInterface.getEndpointCount(); ++i) {
            UsbEndpoint ep = mInterface.getEndpoint(i);
            if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                mEpIn = ep;
            } else {
                mEpOut = ep;
            }
        }

        if (mEpIn == null || mEpOut == null) {
            Log.e(TAG,"open: failed to get input and output endpoint");
            return -1;
        }

        return 0;
    }

    public int read(ByteBuffer byteBuffer, int length) {
       // Log.d(TAG,"UsbDevices read " + length);
        if(mConnection == null) {
            return -1;
        }
        int ret = 0;
        Log.d(TAG,"UsbDevices read length=" + length);
        int size = mConnection.bulkTransfer(mEpIn, mReadBuf, length, mTimeout);
        Log.d(TAG,"UsbDevices read length=" + length + ", size=" + size);

        if (size > 0) {
            byteBuffer.put(mReadBuf, 0, size);
            ret = size;
        }
        return ret;
    }

    public int read(byte[] buf, int offset, int length) {
        // Log.d(TAG,"UsbDevices read " + length);
        if(mConnection == null) {
            return -1;
        }
        int ret = 0;
        Log.d(TAG,"UsbDevices read offset=" + offset + ", length=" + length);
        int size = mConnection.bulkTransfer(mEpIn, buf, offset, length, mTimeout);
        Log.d(TAG,"UsbDevices read offset=" + offset + ", length=" + length + ", size=" + size);
        if (size > 0) {
            ret = size;
        }
        return ret;
    }

    public int write(ByteBuffer byteBuffer, int length) {
       // Log.d(TAG,"UsbDevices write " + length);
        if(mConnection == null) {
            return -1;
        }
        int ret = 0;
        byteBuffer.get(mWriteBuf, 0, length);
        int size = mConnection.bulkTransfer(mEpOut, mWriteBuf, length, mTimeout);
        if(size > 0) {
            ret = size;
        }
        return ret;
    }

    public int write(byte[] buf, int offset, int length) {
        // Log.d(TAG,"UsbDevices write " + length);
        if(mConnection == null) {
            return -1;
        }
        int ret = 0;
        int size = mConnection.bulkTransfer(mEpOut, buf, offset, length, mTimeout);
        if(size > 0) {
            ret = size;
        }
        return ret;
    }

    public void close() {
        if (mConnection == null) {
            Log.d(TAG,"UsbDevices close failed mConnection = null");
        }
        if (mConnection != null) {
            Log.d(TAG,"UsbDevices close start");
            mConnection.releaseInterface(mInterface);
            mConnection.close();
            mConnection = null;
            Log.d(TAG,"UsbDevices close successful device");
        }
    }

    private int getAdbInterface() {
        for (int i = 0; i < mUsbDevice.getInterfaceCount(); ++i) {
            UsbInterface usbif = mUsbDevice.getInterface(i);
            int classNum = usbif.getInterfaceClass();
            int subclassNum = usbif.getInterfaceSubclass();
            int protocol = usbif.getInterfaceProtocol();
            if (classNum == ADB_CLASS && subclassNum == ADB_SUBCLASS && protocol == ADB_PROTOCOL) {
                return i;
            }
        }

        Log.e(TAG,"getAdbInterface: failed to get adb interface from device=" + mUsbDevice);
        return -1;
    }

    private int getAoaInterface() {
        for (int i = 0; i < mUsbDevice.getInterfaceCount(); ++i) {
            UsbInterface usbif = mUsbDevice.getInterface(i);
            int classNum = usbif.getInterfaceClass();
            int subclassNum = usbif.getInterfaceSubclass();
            int protocol = usbif.getInterfaceProtocol();
            if (classNum == AOA_CLASS && subclassNum == AOA_SUBCLASS && protocol == AOA_PROTOCOL) {
                return i;
            }
        }

        Log.e(TAG,"getAoaInterface: failed to get aoa interface from device=" + mUsbDevice);
        return -1;
    }
}

 

你可能感兴趣的:(Android USB读写封装)