【问题标题】:USB Communication between android and PC安卓和PC之间的USB通信
【发布时间】:2018-07-20 07:50:24
【问题描述】:

背景:我曾尝试在 stackoverflow、android 开发人员和网络上的各种其他资源上寻求帮助。这是我最后的希望。 我是通信新手,从 USB 的实施开始。以下是我的问题:

1) 当我将手机连接到 windows PC 时,哪一个是主机? 假设我想创建一个可以发送数据的应用程序,我是在做我的手机主机吗?

2) 对于案例(Windows PC 和 Android 手机),另一个是外围设备还是设备?他们一样吗?

3)从android开发者网站和一个关于USB的windows论坛,我了解到需要遵循某些步骤,就像 - 创建一个 USBManager 的实例。 - 创建获取设备列表 - 选择您要建立连接的设备 - 创建一个界面 - 从该接口获取端点。 - 创建DeviceConnections实例并调用bulkTransfer方法发送数据。

但是当我尝试上述步骤时,我得到了 device == null。 我什至不知道我对上述沟通方式的理解是否正确。

谁能帮我理解和建立 PC 和安卓手机之间的基本通信,并至少发送“hello world”。

非常感谢您阅读这么长的问题。

这是我所做的代码示例。这里 devicelist 返回 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

【问题讨论】:

  • “但是当我尝试上述步骤时,我得到 device == null” 我们无法知道您可能做错了什么,因为您没有向我们展示您的代码。
  • 你熟悉ADB吗,我想你应该看看。另外,您的问题对于堆栈溢出平台来说太宽泛了。
  • 我读过关于亚行的文章,但在此之前,我需要了解我在任何地方的博客、网站上都找不到的基础知识。另外,对于我的用例,我认为亚行对我没有帮助。你能不能给我推荐一篇可以回答我问题的好文章,我已经放弃搜索了?

标签: android usb serial-communication


【解决方案1】:

这不会按您想要的方式工作。 UsbManager.getDeviceList() 旨在用于带有 USB 端口的 Android 设备(例如平板电脑)。但是,您将充当设备的 Android 设备连接到您的 PC充当主机

如果您想在 Android USB 设备和某些 USB 主机之间进行通信,您需要使用 附件模式 (https://developer.android.com/guide/topics/connectivity/usb/)。但此模式需要 USB 主机端(即您的 PC)的特殊驱动程序支持。

还要注意getDeviceList() 在附件模式下没有意义。这是因为连接的配件是 USB 主机,而不是 USB 设备。

在这篇文章中了解更多信息:Android to PC USB Read/Write

请注意,我的回答是基于这个答案:https://stackoverflow.com/a/14099963/5457878

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2013-04-03
    • 2012-05-20
    • 2012-12-25
    • 1970-01-01
    相关资源
    最近更新 更多