This is an Android library for Accessory Programming. It sits on top of the basic Android AOA Libraries, and implements the MQTT protocol for communicating with Arduino based accessories.
This Library was written by Andreas Göransson and David Cuartielles for a book called Android Open Accessory Programming with Arduino.
- Clone this repository to your development machine
- From the Eclipse menu, select New -> Project
- In the dialog, expand Android and select Android Project From Existing code
- Browse to the cloned repository root folder on your machine
- Click Finish
- Right-click the WroxAccessories project in your Package Explorer and select Properties
- Set the Build Target to be Google APIs version 12
- Click OK
- Select your Android Project in the Package explorer and from the menu select File -> Properties
- Select Android from the list on the left side of the dialog
- In the Library TAB, click Add
- Select WroxAccessories and click OK
- Click OK again to close the Properties dialog
/**
* Adding the required Variables
*/
private WroxAccessory mAccessory;
private UsbManager mUsbManager;
private UsbConnection12 connection;
/*
* 1. Get a handle to the UsbManager service
* 2. Initialize the UsbConnection12 object, passing the UsbManager reference
* 3. Initialize WroxAccessory object
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsbManager = (UsbManager) getSystemService(USB_SERVICE);
connection = new UsbConnection12(this, mUsbManager);
mAccessory = new WroxAccessory(this);
}
@Override
protected void onResume() {
super.onResume();
try {
mAccessory.connect(WroxAccessory.USB_ACCESSORY_12, connection);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
mAccessory.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
- Add the uses-feature element to the manifest
- Add the action (USB_ACCESSORY_ATTACHED) element to the intent-filter
- Add the meta-data element to the activity
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-feature
android:name="android.hardware.usb.accessory"
android:required="true" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<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>
Edit manufacturer and model attributes to be the same as your accessory.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-accessory manufacturer="Your company" model="Accessory name" version="1.0" />
</resources>