BulkTransfer & Android USB API

我与影子孤独终老i 提交于 2021-02-08 02:10:47

问题


I have a program in which I attempt to attach my android device to a webcam via USB. I'm having trouble with a few things, namely properly transferring data. I've tried using bulkTransfer and there seems to be no recognition of it being used. I've been trying to find examples that may assist me such as here but none are helping me - their structure seems to be better than mine but whenever I switch my program crashes on load.

I'm fairly confident my bytes declaration is also incorrect and I should be somehow forwarding my data there, but I'm unsure how. Any help in terms of how to data transfer and how to structure my code would be appreciated.

Some declarations:

private byte[] bytes = {1,2};
private static int TIMEOUT = 0;
private boolean forceClaim = true;

In On Create:

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
    }

Additional relevant code:

 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:


one of your problem is on finding endpoints. endpoint0 is for controlling task and you should find the appropriate IN and OUT endpoints in your code.

 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();

in which usbGotPermiDVC is the device that got the permission to access via USB.



来源:https://stackoverflow.com/questions/40120049/bulktransfer-android-usb-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!