简体   繁体   English

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

[英]USB communication between Android (accessory mode) and Windows PC (host)

I try to make an USB connection between my notebook (win7) and my android phone (Android 4.2). 我尝试在我的笔记本(win7)和我的Android手机(Android 4.2)之间建立USB连接。 The notebook should act as host to power the android phone. 笔记本应该作为主机为Android手机供电。 The goal is that notebook and phone can send and receive xml strings 目标是笔记本和手机可以发送和接收xml字符串

I tried to follow the the android page that explains accessory mode ( http://developer.android.com/guide/topics/connectivity/usb/accessory.html ). 我试着按照说明附件模式的android页面( http://developer.android.com/guide/topics/connectivity/usb/accessory.html )。

  • 1: Must I define a accessory filter like they did here: 1:我必须像在此处一样定义附件过滤器:

     <?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 (eg I plug the phone in another pc). 我想要识别所有类型的Windows计算机(例如我将手机插入另一台PC)。

  • 2: I've done nothing on the windows side right now. 2:我现在在窗户方面什么也没做。 I just followd the android page, pluged in the usb cable and watched the log. 我只是按照android页面,插入USB电缆并观看日志。 The app startet asks for permission, but the accessory is null. 应用程序startet请求权限,但附件为null。 Any hints why it is null? 有什么提示为什么它是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? 3:我是否可以使用任何libarys /项目来识别Windows端的USB连接?

  • 4: Any further things I should think about? 4:我还应该考虑进一步的事情吗? Things that are wrong? 出了什么问题?
  • 5: thx for your help :) 5:thx为你的帮助:)

You should have an application on the host side (Windows in your case) that will ask the Android to enter accessory mode. 您应该在主机端(在您的情况下是Windows)上有一个应用程序,它将要求Android进入附件模式。 When it asks, you will be presented with the option to give permission or not. 当它询问时,您将获得是否给予许可的选项。 You have null accessory because there is no accessory connected, that has followed the AOAP to initiate a communication. 您有零配件,因为没有连接配件,已跟随AOAP启动通信。 So it is possible to have accessory device, that is not running Android and to communicate with it using AOAP. 因此,可以使用未运行Android的附件设备并使用AOAP与其进行通信。

You can find an example for the Android side in the samples from your android SDK, in USB folder. 您可以在Android SDK的USB文件夹中找到Android端的示例。

The reason you got 0 accessory because you don't have accessory devices hooked to your Android USB port, you scenario is using Windows PC as the host (You may try write a customized WinUSB driver) and the Android device is a pure USB device, it has nothing to do with AOA, when you send mode switch request from host computer (Windows or macOS), the Android device turn into accessory mode and itself is an accessory, you will find one accessory by using getAccessoryList. 你没有附件设备挂钩到你的Android USB端口你有0配件的原因,你的情况是使用Windows PC作为主机(你可以尝试编写自定义的WinUSB驱动程序),Android设备是一个纯USB设备,它与AOA无关,当您从主机(Windows或macOS)发送模式切换请求时,Android设备变为附件模式并且本身是附件,您将通过使用getAccessoryList找到一个附件。 BTW, I got both Windows and macOS working with Android via libusb. 顺便说一句,我通过libusb使用Windows和macOS与Android合作。

是的,您需要在Windows端使用WinUSB / libusb API来执行主机工作,将Android转换为附件模式,然后互相交谈

Ok, I guess I found the answer myself. 好吧,我想我自己找到了答案。 The Android USB accessory mode is only possible between an android phone and another device that is running Android (such as Arduino). Android USB配件模式只能在Android手机和运行Android的其他设备(如Arduino)之间使用。 So this is not possible with this setting. 因此,此设置无法实现。

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

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