我需要讀筆驅動文件,我的代碼如下:讀取數據
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