我正在使用Galaxy SII(Android版本4.1.2),并且我正在尝试构建一个应用程序,可以告诉我是否连接了USB设备。
我还有一个OTG适配器(非常好用..)。当我连接键盘/鼠标时,我的Android会识别它们,并且我可以顺利地使用它们。
在我的代码中,我尝试迭代已连接的设备,但好像没有连接任何东西。这是我的代码:
这是清单文件:
感谢您的帮助 :)
我还有一个OTG适配器(非常好用..)。当我连接键盘/鼠标时,我的Android会识别它们,并且我可以顺利地使用它们。
在我的代码中,我尝试迭代已连接的设备,但好像没有连接任何东西。这是我的代码:
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
// Get the list of attached devices
HashMap<String, UsbDevice> devices = manager.getDeviceList();
Toast.makeText(this, "Number of devices: " + devices.size(), Toast.LENGTH_LONG).show();
// Iterate over all devices
Iterator<String> it = devices.keySet().iterator();
while (it.hasNext())
{
String deviceName = it.next();
UsbDevice device = devices.get(deviceName);
String VID = Integer.toHexString(device.getVendorId()).toUpperCase();
String PID = Integer.toHexString(device.getProductId()).toUpperCase();
Toast.makeText(this, deviceName + " " + VID + ":" + PID + " " + manager.hasPermission(device), Toast.LENGTH_LONG).show();
}
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ipad"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature android:name="android.hardware.usb.host"/>
<uses-sdk android:minSdkVersion="12" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.example.ipad.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
感谢您的帮助 :)