批量转移Android USB API [英] BulkTransfer & Android USB API

查看:55
本文介绍了批量转移Android USB API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,试图将我的android设备通过USB连接到网络摄像头.我在几件事上遇到麻烦,即正确传输数据.我试过使用bulkTransfer,似乎没有人意识到它正在被使用.我一直在尝试寻找可能对我有帮助的示例,例如此处但没有一个对我有帮助-它们的结构似乎比我的好,但是每当我切换程序时,加载时程序崩溃.

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.

一些声明:

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

推荐答案

您的问题之一是找到端点.endpoint0用于控制任务,您应该在代码中找到合适的IN和OUT端点.

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

其中usbGotPermiDVC是获得通过USB访问权限的设备.

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

这篇关于批量转移Android USB API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆