批量传输和 Android USB API
Posted
技术标签:
【中文标题】批量传输和 Android USB API【英文标题】:BulkTransfer & Android USB API 【发布时间】:2017-02-28 10:10:58 【问题描述】:我有一个程序,我尝试通过 USB 将我的 android 设备连接到网络摄像头。我在一些事情上遇到了麻烦,即正确传输数据。我尝试过使用 bulkTransfer,但似乎没有人意识到它正在被使用。我一直在尝试找到可能对我有帮助的示例,例如here,但没有一个对我有帮助-它们的结构似乎比我的要好,但是每当我在加载时切换程序崩溃。
我相当有信心我的字节声明也不正确,我应该以某种方式将我的数据转发到那里,但我不确定如何。在如何数据传输和如何构建我的代码方面的任何帮助将不胜感激。
一些声明:
private byte[] bytes = 1,2;
private static int TIMEOUT = 0;
private boolean forceClaim = true;
在创建时:
UsbDevice device = (UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext())
device = deviceIterator.next();
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this,0,new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
mUsbManager.requestPermission(device, mPermissionIntent);
UsbDeviceConnection connection = mUsbManager.openDevice(device);
Log.d("CAM Connection", " " + connection);
Log.d("CAM UsbManager", " " + mUsbManager);
Log.d("CAM Device", " " + device);
UsbInterface intf = device.getInterface(0);
Log.d("CAM_INTF Interface!!!!", " " + intf );
UsbEndpoint endpoint = intf.getEndpoint(0);
Log.d("CAM_END Endpoint", " " + endpoint );
connection.claimInterface(intf, forceClaim);
StringBuilder sb = new StringBuilder();
if(connection.bulkTransfer(endpoint,bytes,bytes.length,TIMEOUT) < 2)
Log.d("test", "");
//Log.d("BULK", ""+ connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT)); //do in another thread
附加相关代码:
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver()
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(EXTRA_PERMISSION_GRANTED, false))
if(device != null)
//call method to set up device communication
else
Log.d("Deny:", "permission denied for device " + device);
;
【问题讨论】:
【参考方案1】:您的问题之一是寻找端点。 endpoint0 用于控制任务,您应该在代码中找到适当的 IN 和 OUT 端点。
UsbInterface usbInterfaceTemp = null;
usbInterface = null;
endpointIN = null;
endpointOUT = null;
for (int i = 0; i < usbGotPermiDVC.getInterfaceCount(); i++)
usbInterfaceTemp = usbGotPermiDVC.getInterface(i);
if (usbInterfaceTemp.getEndpointCount() >= 2)
for (int j = 0; j < usbInterfaceTemp.getEndpointCount(); j++)
UsbEndpoint usbEndpointTemp = usbInterfaceTemp.getEndpoint(j);
if (usbEndpointTemp.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
if (usbEndpointTemp.getDirection() == UsbConstants.USB_DIR_IN)
endpointIN = usbEndpointTemp;
else if (usbEndpointTemp.getDirection() == UsbConstants.USB_DIR_OUT)
endpointOUT = usbEndpointTemp;
if (endpointIN != null && endpointOUT != null)
usbInterface = usbInterfaceTemp;
if (usbInterface == null)
return;
usbDeviceConnection = usbManager.openDevice(usbSelectedDevice);
if (!(usbDeviceConnection != null && usbDeviceConnection.claimInterface(usbInterface, true)))
usbDeviceConnection = null;
return;
usbDeviceConnection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
usbDeviceConnection.controlTransfer(0x21, 32, 0, 0, new byte[](byte) 0x80,
0x25, 0x00, 0x00, 0x00, 0x00, 0x08, 7, 0);
Toast.makeText(getApplicationContext(), "Device opened and Interface claimed!", Toast.LENGTH_SHORT).show();
其中usbGotPermiDVC为获得USB访问权限的设备。
【讨论】:
以上是关于批量传输和 Android USB API的主要内容,如果未能解决你的问题,请参考以下文章
桌面 java 应用程序通过 USB 复制和传输 android 数据