我有一个外部usb设备,连接到android设备的微型usb端口,在android棒棒糖出来之前,所有设备上的一切都很好。现在它已经不能在索尼Xperia Z手机上运行了。
所以它适用于所有4.0或更高版本的设备(并且有usb主机),除了带有棒棒糖的索尼Xperia Z(在没有棒棒糖的索尼上运行良好)。
所以问题是,为什么它会停止在棒棒糖上工作,而只在Xperia Z上工作?电话里有些东西变了,但我不知道是什么。
这是我注册usb设备的设置。
Manifest File
<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>
Device Filter
<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意图(无论是当我插入设备启动应用程序时,还是当应用程序启动时插入它)。手机只会问你刚刚插入的是哪种键盘。我不太了解这个设备本身(它是别人制造的),但我可以尝试获得尽可能多的信息。但是它没有改变,这个代码从棒棒糖出现之前就没有改变过。唯一的变化是,它不再适用于索尼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" />
到我的manifest但是我得到
W/PackageManager( 858): Package com.edgetechlabs.drinkmate_play
attempting to redeclare system permission
com.sonyericsson.permission.BLACKLISTED_USB_DEVICE; ignoring new declaration
所以一些改变与许可的usb设备,但我不知道如何解决这个问题。我怎样才能让设备接受这个usb?
我有类似的问题前几天。但是由于Xperia-Z不是blacklistd_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虚拟设备被检测为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/)