我错了吗? 首先,我只想获取连接设备的列表:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mytest.otg">
<uses-feature android:name="android.hardware.usb.host" />
<uses-sdk android:minSdkVersion="12" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<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>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device />
</resources>
(我也尝试使用VID / PID)
package mytest.otg;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
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");
TextView t = (TextView) findViewById(R.id.text);
t.setText( "devs: "+deviceList.size() );
Toast.makeText( this, "devs: "+deviceList.size(), Toast.LENGTH_SHORT );
}
}
但始终为0个设备。 (而且,Toast也未显示...)
我尝试过使用鼠标-设备正在使用它,但是我的应用程序-不可以。我做错了什么?