Android(附件模式)与Windows PC(主机)之间的USB通信 [英] USB communication between Android (accessory mode) and Windows PC (host)

查看:102
本文介绍了Android(附件模式)与Windows PC(主机)之间的USB通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的笔记本电脑 (win7) 和我的安卓手机 (Android 4.2) 之间建立 USB 连接.笔记本电脑应作为主机为安卓手机供电.目标是笔记本和手机可以发送和接收xml字符串

I try to make an USB connection between my notebook (win7) and my android phone (Android 4.2). The notebook should act as host to power the android phone. The goal is that notebook and phone can send and receive xml strings

我试图按照解释配件模式的 android 页面(http://developer.android.com/guide/topics/connectivity/usb/accessory.html).

I tried to follow the the android page that explains accessory mode (http://developer.android.com/guide/topics/connectivity/usb/accessory.html).

  • 1:我是否必须像这里那样定义一个附件过滤器:

  • 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>

因为我不想识别特殊的硬件.我希望能够识别所有类型的 Windows 计算机(例如,我将手机插入另一台电脑).

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:我现在什么都没做.我只是跟着android页面,插上usb线看日志.应用启动请求许可,但附件为空.任何提示为什么它为空?代码:

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:是否有任何库/项目可以用来识别 Windows 端的 USB 连接?

  • 3: Are there any libarys/projects I can use to identify the USB connection on the Windows side?

    推荐答案

    您应该在主机端(在您的情况下为 Windows)有一个应用程序,它会要求 Android 进入附件模式.当它询问时,您将看到是否授予许可的选项.您的附件为空,因为没有连接附件,该附件已跟随 AOAP 发起通信.因此,可以使用未运行 Android 的附属设备并使用 AOAP 与其通信.

    You should have an application on the host side (Windows in your case) that will ask the Android to enter accessory mode. 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. So it is possible to have accessory device, that is not running Android and to communicate with it using AOAP.

    您可以在 Android SDK 的示例中找到 Android 端的示例,位于 USB 文件夹中.

    You can find an example for the Android side in the samples from your android SDK, in USB folder.

    这篇关于Android(附件模式)与Windows PC(主机)之间的USB通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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