我有一个外部USB设备连接到Android设备上的micro usb端口,一切都在所有设备上正常工作,直到android棒棒糖出来。现在它不再适用于Sony Xperia Z手机。
所以它适用于所有4.0或更高版本(并且有USB托管)的设备,除了带有棒棒糖的索尼Xperia Z(在棒棒糖之前在索尼上正常工作)。
所以问题是,为什么它会停止在棒棒糖上工作,而只是在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>
MainActivity
@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?
答案 0 :(得分:2)
在某些日子之前,我遇到了类似的问题。但由于Xperia-Z不是BLACKLISTED_USB_DEVICE,系统权限
<permission
android:name="com.sonyericsson.permission.BLACKLISTED_USB_DEVICE"
android:protectionLevel="signature" />
不会扼杀你。
您需要手动请求权限才能检测设备。有关详细信息,请参阅:http://developer.android.com/guide/topics/connectivity/usb/host.html#permission-d。
但是仍然需要启动USB附件上的应用程序。根据我的发现,可能有两个失败的原因:
本机代码存在一些问题,导致默认情况下将设备视为物理键盘。 (参考:Uinput virtual device is detected as physical keyboard on android)
或者Xperia设备存在问题。正如索尼公司本身所说,他们并不保证索尼设备会支持所有的USB设备。 (参考:http://support.sonymobile.com/global-en/xperiaz4tablet/userguide/Connecting-your-device-to-USB-peripherals/ https://support.sonymobile.com/gb/xperiaz3compact/userguide/Connecting-your-device-to-USB-peripherals/)