我需要读笔驱动文件,我的代码如下:读取数据
private static final String ACTION_USB_PERMISSION = "android.hardware.usb.action.USB_ACCESSORY_ATTACHED";
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) {
UsbDevice 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
}
} else {
Log.d(TAG, "permission denied for device " + device);
}
}
}
}
};
要检测装置使用:
mgr = (UsbManager) getSystemService(Context.USB_SERVICE);
devs = new HashMap<String, UsbDevice>();
devs = mgr.getDeviceList();
并经过获取设备和接口,我得到端点如下:
public void getEndpointCount(View v) {
itf = value.getInterface(0);
endPointCount = itf.getEndpointCount();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("No. of Endpoints: " + endPointCount)
.setTitle("Endpoint Count").setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
((Button) findViewById(R.id.button4)).setEnabled(true);
}
public void getEndpointDirections(View v) {
endPoint = null;
UsbDeviceConnection conn = null;
if (mgr.hasPermission(value)) {
conn = mgr.openDevice(value);
conn.claimInterface(itf, true);
}
String str = null;
for (int i = 0; i < endPointCount; i++) {
((TextView) findViewById(R.id.tvLoopVal)).setText("" + i);
endPoint = itf.getEndpoint(i);
str = str + " Endpoint: " + i + " - " + endPoint.getDirection()
+ "\n";
if (endPoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
((TextView) findViewById(R.id.tvLoopVal)).setText("" + (i - 1));
if (endPoint.getDirection() == UsbConstants.USB_DIR_IN) {
Toast.makeText(this, "epin", Toast.LENGTH_SHORT).show();
epIn = endPoint;
} else {
epOut = endPoint;
Toast.makeText(this, "epout", Toast.LENGTH_SHORT).show();
}
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
str + " Mac packet Size = " + endPointUsed.getMaxPacketSize())
.setTitle("Endpoints Directions").setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
最后,我得到终点,现在我被卡住了什么是nex t步骤将数据读取或写入pendrive。我对此没有任何线索,需要指导现在的位置。
此外,如果我没有附加任何USB设备到我的平板电脑仍然说,发现1设备,当我通过OTG电缆连接pendrive它说2设备发现!这也怪
编辑:
我也使用USB请求方法初始化请求,但我仍然不明白它是如何工作
public void initializeRequest(View v) {
UsbRequest uReq = new UsbRequest();
Toast.makeText(this, "" + uReq.initialize(conn, epIn),
Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
" Is Request Initialized: = " + uReq.initialize(conn, epIn))
.setTitle("Request Initialization").setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
byte[] bb = new String("Hello").getBytes();
ByteBuffer bBuffer = ByteBuffer.allocate(5);
// bBuffer.put(bb);
uReq.queue(bBuffer, 5);
UsbRequest request = conn.requestWait();
// Object obj = uReq.getClientData();
// Log.v("Client Data", "" + obj.toString());
if (request != null) {
Toast.makeText(this, "Not null request", Toast.LENGTH_SHORT).show();
Log.v("Response of request wait", ""
+ request.getClientData().toString());
request.close();
}
uReq.close();
// Log.v("Response of request wait", "" + conn.requestWait());
// uReq.close();
}
我验证端点得到分配,但是当这种方法得到执行应用程序简单地等待,什么都不做。 Android有时要求关闭应用程序。 是否因为requestWait()函数应用程序等待某种类型的事件?
编辑
好吧,如果我们忘记从笔驱动器读取,只是需要从USB设备获取音频流,可以说在那里我不会看什么,我会抓住一切麦克风会给麦克风,那么我仍然需要做其他的事情,那么我在这里做了什么?
我究竟想要做的是有一个USB摄像头连接到Android手机,并从中获取视频凸轮。为什么我想阅读pendrive是为了了解usb设备如何与android设备进行通信。我不知道从哪里开始形式,除了usbhost类的android。在这一点上我看不到前进的方向 – Calvin
您是否尝试过枚举端点并使用UsbDeviceConnection从合适的端点读取数据? – Michael
烨我写的代码有问题 – Calvin