Android平板电脑和相机之间的USB bulkTransfer

我想使用bulkTransfer函数在相机和Android平板设备之间交换数据/命令。 我写了这个Activity,但bulkTransfer方法返回-1 (错误状态)。 为什么它会返回错误?

 public class MainActivity extends Activity { private TextView text; private int TIMEOUT = 1000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.txt); usbTest(); } private void usbTest() { UsbDevice device = (UsbDevice) getIntent().getParcelableExtra( UsbManager.EXTRA_DEVICE); if (device == null) text.setText("device null"); else text.setText("device not null"); UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); text.setText(text.getText() + "\nDevices connected: " + deviceList.values().size()); Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); while (deviceIterator.hasNext()) { device = deviceIterator.next(); text.setText(text.getText() + "\nDevice name: " + device.getDeviceName()); text.setText(text.getText() + "\nDevice protocol: " + device.getDeviceProtocol()); text.setText(text.getText() + "\nDevice id: " + device.getDeviceId()); text.setText(text.getText() + "\nDevice product id: " + device.getProductId()); text.setText(text.getText() + "\nDevice vendor id: " + device.getVendorId()); text.setText(text.getText() + "\nDevice class: " + device.getDeviceClass()); text.setText(text.getText() + "\nDevice subclass: " + device.getDeviceSubclass()); text.setText(text.getText() + "\nDevice interface count: " + device.getInterfaceCount()); text.setText(text.getText() + "\n\n"); } // communicate with device UsbInterface intf = device.getInterface(0); UsbEndpoint endpoint = intf.getEndpoint(0); UsbDeviceConnection connection = manager.openDevice(device); connection.claimInterface(intf, true); for (int i = 0; i < intf.getEndpointCount(); i++) { UsbEndpoint ep = intf.getEndpoint(i); if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { endpoint = ep; text.setText("Found: "+i); } } } // byte[] opensession = { 0x0C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, // 0x10, // 0x00, 0x00, 0x00, 0x00 }; // connection.bulkTransfer(endpoint, opensession, opensession.length, // TIMEOUT); byte[] getEvent = { 0x0C, 0x00, 0x00, 0x00, 0x01, 0x00, toByte(0xC7), toByte(0x90), 0x00, 0x00, 0x00, 0x00 }; int status = connection.bulkTransfer(endpoint, getEvent, getEvent.length, TIMEOUT); //text.setText("Status: " + status); byte[] capture = { 0x14, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0E, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; connection.bulkTransfer(endpoint, capture, capture.length, TIMEOUT); // teminate communication BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) { UsbDevice device = (UsbDevice) intent .getParcelableExtra(UsbManager.EXTRA_DEVICE); if (device != null) { // call your method that cleans up and closes // communication with the device } } } }; } public static byte toByte(int c) { return (byte) (c <= 0x7f ? c : ((c % 0x80) - 0x80)); } } 

Solutions Collecting From Web of "Android平板电脑和相机之间的USB bulkTransfer"

我怀疑你没有正确地search接口和端点。

见下面的解释。

build议:testing端点是否有效,并尝试将其与设备描述符进行匹配。


一个界面可以有多个设置。

AFAIK,对于相机来说,它们应该是相机接口的三个备用设置。

  • 零带宽 – 内部没有端点 – 最有可能的替代设置0
  • 同步(如果相机支持它) – 等时端点内
  • 散装 – 散装terminal内的< – 你感兴趣的地方

(我不知道实际的USB摄像头规格,但是,这应该适用于USB上的所有stream媒体协议(正如我在audio类中看到的))

您需要search批量备用设置,然后在其中的端点上执行通信。