我正在尝试开发一个应用程序来检测 USB 设备并在连接时显示一个弹出窗口。我遵循了您可以在 Android 开发者网站 ( http://developer.android.com/guide/topics/connectivity/usb/host.html )中找到的基本 USB 教程
这是我的 Android 清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.habeis"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
</application>
</manifest>
还有我的 device_filter.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="0x1BCF" product-id="0x0007" />
</resources>
我的活动
package br.com.habeis;
import java.util.HashMap;
import java.util.Iterator;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
@Override
public 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();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext()){
UsbDevice device = deviceIterator.next();
Toast.makeText(this,"Value of device :" +device.getDeviceName(), Toast.LENGTH_LONG).show();
}
}
}
有谁知道我的代码中缺少什么?