2017-02-17 125 views
-3

我很抱歉,我最近问这是不是因为我的描述问题的不准确性收好问题。我编辑了我的问题。Android的USB主机模式

我试图开发利用USB主机的应用程序初学者。我已经通过了USB主机阅读| Android开发者教程,但我还是输了,以它的初始设置。

我的意图是,让应用程式使用枚举过程来定位设备,因为我不知道我的连接设备的供应商ID或产品ID为。我收到致命异常:主要错误,当我尝试运行我有什么。

下面是我到目前为止的代码。任何帮助将不胜感激。

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> 
+1

您不能只问为完整的代码。没有多少人愿意为此付出努力。你可以在那里找到资源来开始。然后对小问题进行后续处理以解决具体问题。这就是你会发现自己得到人们的反馈。 –

+0

谢谢,我基本上的意思是代码的开始部分,以使它开始。我会更新这个问题,以便更具体。 –

回答

0

https://developer.android.com/guide/topics/connectivity/usb/host.html基本上是说,有两种可能性,以获取有关连接到Android

  • USB设备的信息主要活动当一个设备连接时,操作系统(操作系统)发送一个事件可以通过IntentFilter(基于事件的编程)被捕获

  • 应用程序可以查询已连接的

设备,我认为你要查询已连接的USB设备和获取有关设备的信息。使用您的代码,您可以列出所有连接的USB设备,但无法获取它们的信息。下面的代码将打印更详细的信息所连接的设备,I应对它从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

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