私は USB ホストをいじっており、Android Developersサイトのガイドラインに従って、特定の USB デバイスが接続されると起動する Hello World を作成することができました。インテントから接続されたデバイスを表す UsbDevice を取得します" null を返します。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
// device is always null
if (device == null){Log.i(TAG,"Null device");}
これが私のマニフェストです:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.MainActivity"
android:label="@string/app_name" >
<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>
そして、私の xml/device_filter.xml ( Android Developersサイトで説明されている列挙方法を使用して動作する同様のアプリがあるため、これらが正しい VID と PID であることはわかっています):
<resources>
<usb-device vendor-id="1234" product-id="1234"/>
</resources>