在USB协议中,规定了一类大容量存储设备(mass storage device),U盘就属于大容量存储设备。大容量存储设备的接口类代码为0x08,接口子类代码U盘基本上为:0x06。协议代码为0x50,表示 Bulk-Only协议。
大容量存储设备文档:https://www.usb.org/sites/default/files/Mass_Storage_Specification_Overview_v1.4_2-19-2010.pdf
Bulk-Only协议文档:https://usb.org/sites/default/files/usbmassbulk_10.pdf
Bulk-Only协议
Bulk-Only协议是USB组织针对大容量存储设备制定的一种块存储类协议,大容量存储类设备几乎都采用或支持 Bulk-Only 标准。
协议规定了两个特殊的请求:GET MAX LUN与Bulk-Only Mass Storage Reset,通过控制传输发送至设备。
GET MAX LUN请求
LUN即:logical unit number(逻辑单元号)。获取最大逻辑单元请求,就相当于盘符,一般U盘也只有一个逻辑单元,LUN记为0。
public int getMaxLun() {
//接收响应数据
byte[] buffer = new byte[1];
usbDeviceConnection.controlTransfer(0xA1, 0xFE, 0, usbInterface.getId(), buffer, 1, 5000);
//转成int
return (buffer[0] & 0xFF) + 1;
}
GET MAX LUN请求格式如上图,bmRequestType的二进制值为10100001b转为十六进制值为0xA1,相应的bRequest值为0xFE,wIndex为请求的接口号,wLength表示传输的数据长度为1字节(设备传给主机),Data则是数据,如果Data为0表示有一个逻辑单元,1则是两个,以此类推。
usbConnection为封装类
public class UsbConnection {
private UsbDevice usbDevice;
private UsbInterface usbInterface;
private UsbEndpoint intEndpoint;
private UsbEndpoint outEndpoint;
private UsbDeviceConnection usbDeviceConnection;
public UsbConnection(UsbDevice usbDevice, UsbInterface usbInterface, UsbEndpoint intEndpoint, UsbEndpoint outEndpoint) {
this.usbDevice = usbDevice;
this.usbInterface = usbInterface;
this.intEndpoint = intEndpoint;
this.outEndpoint = outEndpoint;
}
public void connect(UsbManager usbManager) {
usbDeviceConnection = usbManager.openDevice(usbDevice);
//锁定此接口,同时只能一处使用
usbDeviceConnection.claimInterface(usbInterface, true);
}
public void close() {
usbDeviceConnection.releaseInterface(usbInterface);
usbDeviceConnection.close();
}
public int getMaxLun() {
//接收响应数据
byte[] buffer = new byte[1];
usbDeviceConnection.controlTransfer(0xA1, 0xFE, 0, usbInterface.getId(), buffer, 1, 5000);
//转成int
return (buffer[0] & 0xFF) + 1;
}
}
要获取UsbConnection对象需要通过UsbDevice和UsbManager对象获取,代码如下
public static UsbConnection isUsbMassStorage(UsbDevice usbDevice){
for (int i = 0;i< usbDevice.getInterfaceCount();i++){
UsbInterface usbInterface = usbDevice.getInterface(i);
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_MASS_STORAGE
&& usbInterface.getInterfaceSubclass() == 0x06
&& usbInterface.getInterfaceProtocol() == 0x50
){
UsbEndpoint outEndpoint = null,inEndpoint = null;
for (int j = 0; j<usbInterface.getEndpointCount();j++){
UsbEndpoint endpoint = usbInterface.getEndpoint(j);
if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK){//USB块传输模式通道
if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT){
outEndpoint = endpoint;
}else{
inEndpoint = endpoint;
}
return new UsbConnection(usbDevice,usbInterface,inEndpoint,outEndpoint);
}
}
}
}
return null;
}
private void showUsbDevice(UsbDevice usbDevice) {
UsbConnection usbConnection = UsbHelper.isUsbMassStorage(usbDevice);
if (usbConnection != null) {
usbConnection.connect(usbManager);
int maxLun = usbConnection.getMaxLun();
Log.i(TAG, "showUsbDevice maxLun " + maxLun);
}
}
通过以上方法既可以获取GET MAX LUN请求最简单的请求。
完整代码地址AndroidCarModule