-3

I apologize, I recently asked a question that was not received well due to the inaccuracy of describing my problem. I have edited my question.

I am a beginner trying to develop an app that utilizes usb host. I have read through the USB Host|Android Developer tutorial, but I am still lost as to how it is initially set up.

My intent is for the app to use the Enumeration process to locate the device, as I do not know what my connected device's vendor-id or product-id is. I receive a Fatal Exception: main error when I attempt to run what I have.

Below is my code so far. Any help would be greatly appreciated.

Main Activity class

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 file

<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>
4

1 回答 1

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访问deviceList由 USB 管理器生成的所有 USB 设备的类。device然后通过访问类的字段从类中提取信息(vendorID,...)(类的字段device由操作系统内部通过本机 C 函数(JNI)构建)

于 2017-02-19T10:39:55.743 回答