AndroidデバイスのマイクロUSBポートに接続する外部USBデバイスがあり、Androidロリポップが登場するまで、すべてのデバイスですべてが正常に機能していました。現在、Sony Xperia Z 携帯電話では機能しなくなりました。
そのため、ロリポップを搭載した Sony Xperia Z (ロリポップの前にソニーで正常に動作) を除いて、4.0 以降の (および USB ホスティングを備えた) すべてのデバイスで動作します。
問題は、なぜ lollipop で動作しなくなり、具体的には Xperia Z で動作しなくなるのでしょうか? その電話で何かが変わったが、何が変わったのかわからない.
これが、USB デバイスを登録するためのセットアップです。
マニフェスト ファイル
<activity
android:name="com.edgetechlabs.drinkmate_play.MainActivity"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:label="@string/app_name" >
<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"/>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/>
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
android:resource="@xml/device_filter" />
</activity>
デバイス フィルター
<resources>
<usb-device vendor-id="8352" product-id="16881"/>
</resources>
メインアクティビティ
@Override
public void onResume() {
super.onResume();
//get the intent of the activity
Intent intent = getIntent();
String action = intent.getAction();
Log.i(TAG, "MainActivity Resumed with intent " + action);
//if the device has been plugged in
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
Log.i(TAG, "Device Attached to start app");
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
setDeviceConnection(device);
}
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i(TAG, "New Intent Recieved " + intent.getAction() + " with dmPluggedIn = " + dmPluggedIn);
// whenever a new intent is received from an app re-launch due to the device being plugged in
// or an intent being manually set, set the intent of the app to it only if it was from
// a device being attached. That way, when the app goes to the background with the device still attached
// the ATTACHED intent remains (because the MAIN intent is blocked)
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
System.out.println("changing intent to usb");
setIntent(intent);
}
}
動作しない理由を特定するために、USB_DEVICE_ATTACHED インテントを登録しません (デバイスを接続してアプリを起動するとき、またはアプリの起動時にデバイスを接続するとき)。電話は、挿入したばかりのキーボードの種類を尋ねるだけです。デバイス自体 (他の誰かが作成したもの) についてはよくわかりませんが、要求された限り多くの情報を取得することはできます。このコードは、ロリポップが登場する前からまったく変わっていません。変更された唯一のことは、Lollipop を搭載した Sony Xperia Z では動作しなくなったことです。
更新 1
発見したログを読む
W/ContextImpl( 858): Calling a method in the system process without a
qualified user: android.app.ContextImpl.sendBroadcast:1355
com.android.server.usb.UsbSettingsManager.blacklistedDeviceDetached:777
com.android.server.usb.UsbHostManager.usbDeviceRemoved:397
com.android.server.usb.UsbHostManager.monitorUsbHostBus:-2
com.android.server.usb.UsbHostManager.access$000:54
W/BroadcastQueue( 858): Permission Denial: receiving Intent
{act=android.hardware.usb.action.USB_DEVICE_DETACHED flg=0x10 (has extras) }
to ProcessRecord{3f67a3f 9138:com.edgetechlabs.drinkmate_play/u0a276}
(pid=9138, uid=10276)
requires com.sonyericsson.permission.BLACKLISTED_USB_DEVICE due to sender android (uid 1000)
だから私は追加しました
<permission
android:name="com.sonyericsson.permission.BLACKLISTED_USB_DEVICE"
android:protectionLevel="signature" />
私のマニフェストに
W/PackageManager( 858): Package com.edgetechlabs.drinkmate_play
attempting to redeclare system permission
com.sonyericsson.permission.BLACKLISTED_USB_DEVICE; ignoring new declaration
USBデバイスの許可を得て何かが変更されましたが、問題を解決する方法がわかりません。デバイスがこの USB を受け入れるようにするにはどうすればよいですか?