Android(配件模式)与Windows PC(主机)之间的USB通信

15
我尝试在我的笔记本电脑(Win7)和我的Android手机(Android 4.2)之间建立USB连接。笔记本电脑应该作为主机来为Android手机供电。目标是让笔记本电脑和手机可以发送和接收XML字符串。
我尝试遵循Android页面上解释附件模式的说明(http://developer.android.com/guide/topics/connectivity/usb/accessory.html)。
  • 1: Must I define a accessory filter like they did here:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <usb-accessory model="DemoKit" manufacturer="Google" version="1.0"/>
    </resources>
    

    Because I don't want a special hardware to be recognized. I want all kind of windows computers to be recognized (e.g. I plug the phone in another pc).

  • 2: I've done nothing on the windows side right now. I just followd the android page, pluged in the usb cable and watched the log. The app startet asks for permission, but the accessory is null. Any hints why it is null? Code:

    public class MainActivity extends Activity {
    private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    private static final String TAG = "USB_PERMISSION";
    UsbAccessory accessory;
    
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    
    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0,
            new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);
    accessory = (UsbAccessory) getIntent().getParcelableExtra(
            UsbManager.EXTRA_ACCESSORY);
    manager.requestPermission(accessory, mPermissionIntent);
     }
    
    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) {
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    String manufacturer;
                    Log.d(TAG, "permission accepted for accessory " + accessory);
                    if (accessory != null) {
                        manufacturer = accessory.getManufacturer();
                        Log.d(TAG, "Manufacturer: " + manufacturer);                        }
                } else {
                    Log.d(TAG, "permission denied for accessory "+ accessory);
                }
            }
        }
    }
    };
    }
    
  • 3: Are there any libarys/projects I can use to identify the USB connection on the Windows side?

  • 4: Any further things I should think about? Things that are wrong?
  • 5: thx for your help :)

所以我想现在#1更清晰了。Intentfilter仅用于自动检测。 - B770
4个回答

5

您需要在主机端(在您的情况下是Windows)运行一个应用程序,以请求Android进入配件模式。当它请求时,您将有选择给予许可或不给予许可的选项。由于没有遵循AOAP(Android开放附件协议)来启动通信的连接配件,因此您现在拥有空配件。

因此,即使没有运行Android系统的配件设备,也可以使用AOAP与其通信。

您可以在Android SDK的USB文件夹中找到Android端的示例。


如何实现这个功能?有没有任何源应用程序可以使用? - Naveed Ali
2
你有找到任何从安卓设备接收数据的PC端示例吗? - badoualy
@seed 实际上我还没有使用过它。曾经有一次可以开始使用它,所以我熟悉了它,但是之后它被推迟了,我就没有再处理它了。不过我认为,一旦你知道协议,弄清楚它应该不是什么大问题。 - dragi
@helleye,你的意思是我的安卓手机应用可以通过USB实际响应Windows系统?我之前也有类似的需求,我使用Windows中的adb与我的应用进行通信,我的应用会在预定义的路径上创建文本文件,然后我的Windows应用程序会读取这些文件。 - Calvin
@Calvin 是的,这就是附件模式的作用。您应该遵循 AOA 协议,可以首先阅读 https://source.android.com/devices/accessories/protocol.html。 - dragi
@Calvin,你能否给我发送一些你所做的ADB通信的参考资料吗? - Chetan Joshi

0

你之所以得到0个附件,是因为你没有将附件设备连接到你的Android USB端口上。你的情况是使用Windows PC作为主机(你可以尝试编写一个自定义的WinUSB驱动程序),而Android设备是一个纯USB设备,它与AOA无关。当你从主机计算机(Windows或macOS)发送模式切换请求时,Android设备会进入附件模式,并且本身就是一个附件,你可以通过使用getAccessoryList找到一个附件。顺便说一句,我已经通过libusb使Windows和macOS都能够与Android配合使用了。


你能分享一下你是怎么做到让它工作的吗?谢谢。 - Ron

0

是的,在Windows端需要使用WinUSB/libusb API来完成主机工作,将Android设备转换为附件模式,然后进行通信。


-5

好的,我想我自己找到了答案。Android USB附件模式只能在Android手机和另一个运行Android(如Arduino)的设备之间进行。因此,这个设置不可能实现。


这个答案是不正确的 - Arduino不能运行Android,也没有必要使用USB主机。然而,它需要能够以特定的方式进行通信,以支持USB附件模式。 - Chris Stratton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接