Android到PC USB读/写 [英] Android to PC USB Read/Write

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

问题描述

我在PC上有一个程序,它从USB接收字符串输入(旧程序),我有一个Android(4.X)平板电脑,需要在USB上向运行在PC上的程序提供字符串输入.当我在Android上使用示例代码时,以下代码给出了空的hashmap.PC(在32位XP和64位Windows 7上尝试过)具有Android驱动程序.

  mManager =(UsbManager)getSystemService(Context.USB_SERVICE);HashMap< String,UsbDevice>devices = mManager.getDeviceList(); 

任何通过USB与PC进行通信的实际工作代码示例都将有所帮助.还指出在Android上与PC对话是否需要任何驱动程序等.

我已经尝试了附件模式和主机模式(以防万一)

解决方案

我不确定我是否完全遵循您在这里所做的事情,但是如果我正确理解了您的意见,那么-这样就行不通了. UsbManager.getDeviceList()旨在与具有USB 主机端口的Android设备一起使用,某些USB 设备已连接到该端口.但是,据我了解,您将充当设备的Android平板电脑连接到充当主机的PC(我想是这样,因为您写了有关驱动程序安装的文章).

如果您想在Android USB 设备和某些USB 主机之间进行通信(例如,因为您的Android设备没有USB主机功能),则需要使用附件模式(我建议您从此Android开发者博客文章开始).但是,此模式需要USB主机侧的特殊支持(必须使用 Android Open Accessory与设备通信协议).请注意,在附件模式下, getDeviceList()没有意义-首先,连接的附件是USB主机,而不是USB设备,并且USB总线上只能有一个USB主机./p>

如果要使用附件模式与PC进行通信,则可能需要尝试 解决方案

I'm not sure I exactly follow what you're doing here, but if I understand you correctly - it just won't work this way. The UsbManager.getDeviceList() is meant to be used with Android devices with USB host port, to which some USB devices are connected. But, as far as I understand, you connect Android tablet acting as a device to your PC acting as a host (I guess so, cause you wrote about driver installation).

If you want to communicate between Android USB device and some USB host (e.g. because your Android device has no USB host capabilities), you need to use accessory mode (I suggest you start with this Android Developers Blog post). But this mode requires special support on the USB host side (it must talk to the device with Android Open Accessory Protocol). Note, that getDeviceList() makes no sense in Accessory mode - first of all, connected accessory is a USB host, not a USB device, and there can be only one USB host on a USB bus.

If you want to communicate with PC using Accessory mode, you may want to try this AOAP implementation for PC. If all you need is to talk to Android device for some debugging needs, you may want to use ADB port forwarding and TCP connection instead.

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

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