【发布时间】: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