demokit (adk_release_20120606.zip (https://dl-ssl.google.com/android/adk/adk_release_20120606.zip )) 在我的zte u970上跑不起来。一直以为是厂家把ADK部分删除了。仔细阅读了文档( http://developer.android.com/guide/topics/connectivity/usb/accessory.html )后发现,是我搞错了。
在Android 2.3.4,应当使用名字空间com.android.future.usb
,这里是作为android的add-on
在Android3.1以后,应当使用名字空间
android.hardware.usb
:这里是作为android 框架的一部分。
代码也要做相应修改
add-on library(android 2.3.4) | platform(android 3.1) |
---|---|
UsbManager manager = UsbManager.getInstance(this); | UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); |
UsbAccessory accessory = UsbManager.getAccessory(intent); | UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); |
Set the minimum SDK of the application to API Level 10 | Set the minimum SDK of the application to API Level 12 |
add the <uses-library> element specifyingcom.android.future.usb.accessory | include a<uses-feature> element that declares that your application uses theandroid.hardware.usb.accessory feature. |
常用代码
Android App USB Code
In the ADK 2012 Android app, the code for handling USB connections is encapsulated in a UsbConnection
class. This class sets up a BroadcastReceiver
to listen for USB events and then attempts to connect when a matching connection event is received. Here is a summary of the relevant code:
import com.android.future.usb.UsbAccessory; import com.android.future.usb.UsbManager; mUSBManager = UsbManager.getInstance(this); UsbAccessory acc = mUSBManager.getAccessoryList()[0]; if (!mUSBManager.hasPermission(acc)) return;
The ADK 2012 app uses the support library to implement the USB accessory connections, in order to support devices running Android 2.3.4 (API Level 10). If you only need to support Android 3.1 (API Level 12) and higher devices, you can replace the first 4 lines the following code:
import android.hardware.usb.UsbAccessory import android.hardware.usb.UsbManager mUSBManager = (UsbManager) getSystemService(Context.USB_SERVICE); UsbAccessory acc = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
Note that the app only receives events when the USB accessory identification information matches the information in theres/xml/usb_accessory_filter.xml
file, referenced by the application’s manifest statement:
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" android:resource="@xml/usb_accessory_filter" />
Connections from other USB devices are not received by the ADK 2012 accessory.
ref:
adk_release_20120606.zip (https://dl-ssl.google.com/android/adk/adk_release_20120606.zip )
http://developer.android.com/guide/topics/connectivity/usb/accessory.html
http://developer.android.com/tools/adk/adk2.html
http://developer.android.com/tools/adk/adk.html