When you create your first Android USB Accessory with the Android ADK, one of the first things you’ll need to do is identify your accessory when you connect it to your Android device. Within the Arduino sketch for your accessory, you identify your accessory in the
AndroidAccessory(const char *manufacturer, const char *model, const char *description, const char *version, const char *uri, const char *serial);
For example, the DemoKit app that comes with the ADK identifies itself as version 1.0 of the Google DemoKit accessory:
AndroidAccessory acc("Google, Inc.", "DemoKit", "DemoKit Arduino Board", "1.0", "http://www.android.com", "0000000012345678");
You’re own app might looks something like this:
AndroidAccessory acc("My App Company", "CoolAccessory", "CoolAccessory Arduino Board", "1.0", "http://www.example.com/CoolAccessory", "0000000012345678");
The three key one’s you’ll need to remember are
version as you’ll need to define these in you’re Android app so that the device can notify your app when your accessory is connected.
When you create your Android app, there’s a few steps you need to take. First, not every Android-powered device supports the USB accessory APIs. In fact, the Android Open Accessory ADK is only supported by Android OS 3.1 and 2.3.4 platforms. To identify that your app has USB features, you need to include a
<uses-feature> element in your manifest to declare that your application uses the
<uses-feature android:name="android.hardware.usb.accessory" />
Next, if you’re using the add-on library, add a
<uses-library> element with
com.android.future.usb.accessory for the USB accessory library.
<uses-library android:name="com.android.future.usb.accessory" />
and set the minimum SDK of the application to API Level 10. If you’re using
theandroid.hardware.usb package then the
<uses-library> isn’t necessary and you only need to set the minimum SDK to 12 .
Lastly, you’ll want your application to be notified when your accessory is attached. Specify an
<meta-data> element pair for the
android.hardware.usb.action.USB_ACCESSORY_ATTACHED intent in your main activity. Overall your manifest should look something like this:
<manifest ...> <uses-feature android:name="android.hardware.usb.accessory" /> <uses-sdk android:minSdkVersion="<version>" /> ... <application> <uses-library android:name="com.android.future.usb.accessory" /> <activity ...> ... <intent-filter> <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" android:resource="@xml/accessory_filter" /> </activity> </application> </manifest>
<meta-data> element references an XML resource file that includes the
version you defined earlier in your Arduino sketch. Create an
accessory_filter.xml resource file in the
res/xml/ directory with a
<usb-accessory> element to identify your accessory.
<resources> <usb-accessory model="CoolAccessory" manufacturer="My App Company" version="1.0"/> </resources>
Now, when you connect your accessory to the device android will send an intent to open an appropriate application. The best part is that more than one application can respond to a given intent so multiple apps could optionally respond for the same accessory.