为什么 usbManager.getAccessoryList();总是返回 null?
我正在使用 Android 2.3.4 的 Samsung Galaxy S2 中测试我的应用程序,
我正在关注
http://developer.android.com/guide/topics/usb /accessory.html
说明。
当我尝试运行它时,usbManager.getAccessoryList();给空。
我什至尝试过将 OTG 数据线和随身碟连接到它。
我的清单。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature android:name="android.hardware.usb.accessory" />
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<uses-library android:name="com.android.future.usb.accessory" />
<activity
android:name=".SplashScreen"
android:clearTaskOnLaunch="true"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Advanced"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Edit"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Add"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="Main"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/accessory_filter" />
</activity>
</application>
</manifest>
I am testing my app in Samsung Galaxy S2 with Android 2.3.4
I am following
http://developer.android.com/guide/topics/usb/accessory.html
instructions.
When ever I try to run it, usbManager.getAcccessoryList(); giving null.
Even I tried by connecting OTG cabel and pendrive to it.
My manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature android:name="android.hardware.usb.accessory" />
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<uses-library android:name="com.android.future.usb.accessory" />
<activity
android:name=".SplashScreen"
android:clearTaskOnLaunch="true"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Advanced"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Edit"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Add"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="Main"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/accessory_filter" />
</activity>
</application>
</manifest>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也遇到了同样的问题,但我的平板电脑有 ICS。如果尝试我所做的,可能会有效。我已经为此进行了很多搜索,但找不到它,但是感谢所有花了一些时间回答我们问题的人以及网络上的 wiki 资源,我找到了它。
某些 Android 版本中缺少一些文件,例如 AINOL NOVO 7 PALADIN。我认为这适用于任何具有相同问题的 ICS 平板电脑。我知道 Galaxy Tab 也有同样的问题,但由于我没有,所以无法确认这一点。如果您这样做,请告诉我它是否适合您。
因此,让我们将文件推入并看看效果如何。
将一些丢失的文件复制到系统驱动器中:
注意:如果出现错误,显示内存不足,这是正确的,您必须从系统驱动器中删除一些文件。我推荐一些来自系统/应用程序的动态壁纸。
我知道,要写入系统驱动器,您应该具有root访问权限,当我这样做时,我已经拥有了它,所以如果它不起作用,请root它。
您可以从 Android 源版本或 USB-HOST API 实际运行的平板电脑获取文件。我从 linaro 为 pandaboard 构建的 ICS 中获得了它们。
现在,您应该尝试查找这些文件,但对于 Gingerbread,您还需要推送
将
future.usb.accesory.jar
放入system/framework
(如果尚不存在)。I had the same problem but my tablet has ICS. If try what I did, might work. I've been searching a lot for this, and couldn't find it, but thanks to all the people who have spent a few moments answering our questions, and the wiki resources on the web, I figured it out.
There are some missing files in some android builds, like the AINOL NOVO 7 PALADIN. I think this would work in any ICS tablet with the same issue. I understand that the Galaxy Tab has the same issue but as I don't have one I can't confirm this. If you do, let me know if it works for you.
So lets push the files in and see how it goes.
Copy some missing files into system drive:
NOTE: If an error appears displaying Out of Memory, it's right and you must delete some files from system drive. I recommend some live wallpapers from system/app.
I understand that for writing system drive you should have root access, I already had it when I did this so if it doesn't work, root it.
You can get the files from android source builds or from a tablet where USB-HOST API is actually working. I got them from linaro's ICS build for pandaboard.
Now, you should try finding those files but for Gingerbread and you'll also need to push the
future.usb.accesory.jar
intosystem/framework
, if it isn't there already.引用自 http://developer.android.com/guide/topics/usb/ accessory.html:“如果您使用附加库,则将应用程序的最低 SDK 设置为 API 级别 10;如果您使用 android.hardware.usb 包,则将应用程序的最低 SDK 设置为 API 级别 12。”
您正在使用 android.hardware.usb 包。所以,你必须把版本设置为12。
发现错误:-)
并且不要忘记真正将SDK版本更改为12,而不仅仅是声明它!
根据文档,请改用 com.android.future.usb 包。
A cite from http://developer.android.com/guide/topics/usb/accessory.html: "Set the minimum SDK of the application to API Level 10 if you are using the add-on library or 12 if you are using the android.hardware.usb package."
You are using the android.hardware.usb package. So, you have to put the version to 12.
An error is found :-)
And don't forget to really change SDK version to 12, not only declare it!
Use com.android.future.usb package instead, according to docs.