【问题标题】:Android usb host mode安卓usb主机模式
【发布时间】:2017-02-17 20:41:03
【问题描述】:

很抱歉,我最近问了一个问题,由于描述我的问题不准确而没有得到很好的回答。我已经编辑了我的问题。

我是一名初学者,正在尝试开发使用 USB 主机的应用程序。我已经阅读了 USB Host|Android 开发者教程,但我仍然不知道它最初是如何设置的。

我的意图是让应用程序使用枚举过程来定位设备,因为我不知道我连接的设备的供应商 ID 或产品 ID 是什么。当我尝试运行我所拥有的内容时,我收到 Fatal Exception: main 错误。

以下是我目前的代码。任何帮助将不胜感激。

主要活动类

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbDevice;
import android.content.Context;
import java.util.HashMap;
import java.lang.Object;
import android.content.Intent;
import android.util.Log;
import java.util.Iterator;
import java.util.Collection;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    HashMap <String, UsbDevice> deviceList = manager.getDeviceList();
    UsbDevice device = deviceList.get("deviceName");

}

清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.trapperdavis.ircontprototype2">

    <uses-sdk android:minSdkVersion="12" />

    <uses-feature android:name="android.hardware.usb.host" />



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />
        </activity>
    </application>
</manifest>

【问题讨论】:

  • 你不能只要求完整的代码。愿意为此付出努力的人并不多。您可以在那里找到帮助您入门的资源。然后用小问题跟进具体问题。这就是你会发现自己从人们那里得到反馈的方式。
  • 谢谢,我的基本意思是开始代码的开头部分。我将更新问题以更具体。

标签: android usb android-usb


【解决方案1】:

https://developer.android.com/guide/topics/connectivity/usb/host.html 上基本上是说有两种可能获取有关连接到android 的USB 设备的信息

  • 当设备连接时,操作系统(操作系统)会发送一个事件,该事件可以被IntentFilter(基于事件的编程)捕获

  • 应用可以查询已连接的设备

我认为您想查询已连接的 USB 设备并获取有关设备的信息。使用您的代码,您可以列出所有连接的 USB 设备,但无法获取有关它们的信息。以下代码将打印有关附加设备的更多信息,我从http://android-er.blogspot.de/2013/10/list-attached-usb-devices-in-usb-host.html处理它

 private void checkInfo() {
  UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
  HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
  Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

  String i = "";
  while (deviceIterator.hasNext()) {
   UsbDevice device = deviceIterator.next();
   i += "\n" +
    "DeviceID: " + device.getDeviceId() + "\n" +
    "DeviceName: " + device.getDeviceName() + "\n" +
    "DeviceClass: " + device.getDeviceClass() + " - " 
     + translateDeviceClass(device.getDeviceClass()) + "\n" +
    "DeviceSubClass: " + device.getDeviceSubclass() + "\n" +
    "VendorID: " + device.getVendorId() + "\n" +
    "ProductID: " + device.getProductId() + "\n";
  }

  textInfo.setText(i);
 }

完整的源代码在http://android-er.blogspot.de/2013/10/list-attached-usb-devices-in-usb-host.html

代码使用和Iterator 类访问由USB 管理器生成的deviceList 中的所有USB 设备。然后通过访问device 类的字段(device 类的字段由操作系统内部通过本机 C 函数 (JNI) 构建)从device 类中提取信息(vendorID,...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    相关资源
    最近更新 更多