java - Android 平板电脑和相机之间的 USB 批量传输

标签 java android usb android-camera

我想使用 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));
    }

}

最佳答案

我认为这个函数没有传递正确的端点

int status = connection.bulkTransfer(endpoint, getEvent,
                                     getEvent.length, TIMEOUT);

来自

UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);

可能是 USB 接口(interface)不正确。请检查索引0参数是否正确。

关于java - Android 平板电脑和相机之间的 USB 批量传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982264/

相关文章:

java - onJoin 事件不取代lastPlayer

java - 解密java中在php中使用AES加密的字符串

java - 如何从 Mono<Boolean> 类型字段中提取值?

linux - 在 Linux 中检测 USB 大容量存储弹出/卸载

java - 构建和运行 JavaFX .jar 文件时出现问题

文本模糊时,Android BlurMaskFilter 在 canvas.drawOval 中无效

android - 每次循环时更改 TextView 的名称或变量 - Android

java - 数据更改时的 Activity 转换

与控制台输出混淆,C/USB CDC/PIC18F2550

powershell - 获取 USB 端口名称和详细信息