我正在尝试使用OTG线缆将USB设备连接到Samsumg Galaxy。我根据http://developer.android.com/guide/topics/connectivity/usb/host.html教程编写了一个简单的代码,但它根本不起作用。到目前为止我所意识到的是动作始终是android.intent.action.MAIN。也许这就是设备始终为空的原因。这个节目有人有运气吗?
这是我的代码:
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_microstrain);
EditText find_device = (EditText)findViewById(R.id.find_device);
Intent intent = getIntent();
String action = intent.getAction();
Toast toast = Toast.makeText(getApplicationContext(), action, Toast.LENGTH_SHORT);
toast.show();
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
UsbAccessory device = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
find_device.setText((CharSequence) device);
}
和清单:
<?xml version="1.0" encoding="utf-8"?>
<uses-feature android:name="android.hardware.usb.host"/>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:launchMode="singleTask">
<activity
android:name="com.example.microstrain.MICROSTRAIN"
android:label="@string/app_name" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
<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>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.microstrain"
android:label="microstrain" />
和xml / device_filter.xmk:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="6555" product-id="15717"/>
</resources>