安卓和PC之间的USB通信 [英] USB Communication between android and PC
问题描述
背景:我曾尝试在 stackoverflow、android 开发人员和网络上的各种其他资源方面寻求帮助.这是我最后的希望.我是通信新手,从 USB 的实现开始.以下是我的问题:
Background :I have tried finding help on stackoverflow, android developers, and various other resources on web. This is my last hope. I am new to communication and starting with implementation of USB. Following are my questions:
1) 当我将手机连接到 Windows PC 时,哪一个是主机?假设我想创建一个可以发送数据的应用程序,我是在做我的手机主机吗?
1) When I connect my phone to windows PC, which one is host? Suppose I want to create an application that can send data, am I making my phone host?
2) 对于这种情况(Windows PC 和 Android 手机),另一个是外围设备还是设备?他们一样吗?
2) For the case (Windows PC and Android phone), the other one would be a peripheral or a device? are they same?
3) 从 android 开发者网站和关于 USB 的 windows 论坛,我了解到需要遵循某些步骤,就像- 创建一个 USBManager 实例.- 创建获取设备列表- 选择要建立连接的设备- 创建一个界面- 从该接口获取端点.- 创建 DeviceConnections 实例并调用 bulkTransfer 方法并发送数据.
3) From android developers website and a windows forum about USB, I understand that there are certain steps that need to be followed, and it's like - Create an instance of USBManager. - Create get list of devices - Choose a device from which you want to establish connection - Create an interface - get endpoint from that interface. - Create instance of DeviceConnections and call bulkTransfer method and send data.
但是当我尝试上述步骤时,我得到 device == null.不知道我对上述沟通方式的理解是否正确.
But when I tried above steps, I get device == null. I don't even know if my understanding for above communication method is correct.
有人可以帮助我理解并建立 PC 和 Android 手机之间的基本通信,并至少发送hello world".
Can someone please help me understand and establish basic communication between a PC and a android phone and send at least "hello world".
非常感谢您阅读这么长的问题.
Big thank you for even reading such a long question.
这是我所做的代码示例.这里 devicelist 返回 null.
Here is code sample of what I have done. here devicelist returns null.
public class MainActivity extends AppCompatActivity {
android.widget.Button usbButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CustomUSBManager cmanager = new CustomUSBManager();
//1) //Create instance of USB Manager using getSystemService
final UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
//2) //Create a list of devices
HashMap<String,UsbDevice> deviceList = manager.getDeviceList();
Log.d("Devicelist = ", String.valueOf(deviceList.get(0)));
//3) //Get a specific device from the list
//-----------------------------------------------------------------
Here is my manifest file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.neeraj.usbcommunication1">
<uses-feature android:name="android.hardware.usb.host"></uses-feature>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
final UsbDevice device = deviceList.get(0); //getting first device
推荐答案
这不会以您想要的方式工作.UsbManager.getDeviceList()
旨在用于带有 USB 端口的 Android 设备(例如平板电脑).但是,您将充当设备的 Android 设备连接到充当主机的 PC.
This will not work the way you want. The UsbManager.getDeviceList()
is meant to be used with Android devices with an USB port (for example a tablet). However, you connect an Android device acting as a device to your PC acting as a host.
如果您想在 Android USB 设备和某些 USB 主机之间进行通信,您需要使用附件模式(https://developer.android.com/guide/topics/connectivity/usb/).但此模式需要 USB 主机端(即您的 PC)的特殊驱动程序支持.
If you want to communicate between Android USB device and some USB host you need to use the accessory mode (https://developer.android.com/guide/topics/connectivity/usb/). But this mode requires special driver support on the USB host side (which is your PC).
还要注意 getDeviceList()
在附件模式下没有意义.这是因为连接的配件是 USB 主机,而不是 USB 设备.
Also note that getDeviceList()
makes no sense in Accessory mode. This is because the connected accessory is a USB host, not a USB device.
在这篇文章中阅读更多相关信息:Android 到 PC USB 读/写一个>
Read more about this in this post: Android to PC USB Read/Write
请注意,我的回答基于此答案:https://stackoverflow.com/a/14099963/5457878
Note that my answer is based on this answer: https://stackoverflow.com/a/14099963/5457878
这篇关于安卓和PC之间的USB通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!