简体   繁体   English

通过USB电缆使用AOA协议在两个android设备之间建立通信

[英]Establish communication between two android devices using AOA protocal via USB cable

I am trying to connect two Android devices with API 14+ via USB OTG cable, 我正在尝试通过USB OTG电缆将两个Android设备与API 14+连接起来,

but I am getting null values when I was accessing two API s like followings 但是当我访问以下两个API时,我得到的是null

mUsbManager.getDeviceList();

mUsbManager.getAccessoryList();

Please help me sharing your ideas or any example of sample apps if you have any. 请帮助我分享您的想法或示例应用程序的任何示例(如果有)。

Are you requesting the permission through the intent to even see what is attached? 您是否出于意图要求许可,以查看附件内容?

Are you defining your own custom type and communication layer or trying to do so over adb....? 您是在定义自己的自定义类型和通信层,还是尝试通过adb ....来实现? More info here might be good. 这里的更多信息可能很好。 For now though, you need to know if you can even see the device. 但就目前而言,您需要知道是否可以看到该设备。

This problem is somewhat defined in the AOA v2 page : https://source.android.com/devices/accessories/aoa2.html 这个问题在AOA v2页面中有所定义: https : //source.android.com/devices/accessories/aoa2.html

Text copied from : http://mobilemerit.com/android-app-for-usb-host-with-source-code/ 文本复制自: http : //mobilemerit.com/android-app-for-usb-host-with-source-code/

private void checkInfo() {
        manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        /*
         * this block required if you need to communicate to USB devices it's
         * take permission to device
         * if you want than you can set this to which device you want to communicate   
         */
        // ------------------------------------------------------------------
        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
                ACTION_USB_PERMISSION), 0);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(mUsbReceiver, filter);
        // -------------------------------------------------------------------
        HashMap<string , UsbDevice> deviceList = manager.getDeviceList();
        Iterator<usbdevice> deviceIterator = deviceList.values().iterator();
        String i = "";
        while (deviceIterator.hasNext()) {
            device = deviceIterator.next();
            manager.requestPermission(device, mPermissionIntent);
            i += "\n" + "DeviceID: " + device.getDeviceId() + "\n"
                    + "DeviceName: " + device.getDeviceName() + "\n"
                    + "DeviceClass: " + device.getDeviceClass() + " - "
                    + "DeviceSubClass: " + device.getDeviceSubclass() + "\n"
                    + "VendorID: " + device.getVendorId() + "\n"
                    + "ProductID: " + device.getProductId() + "\n";
        }

        textInfo.setText(i);
    }

    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(
                            UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (device != null) {
                            // call method to set up device communication
                        }
                    } else {
                        Log.d("ERROR", "permission denied for device " + device);
                    }
                }
            }
        }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM