Android ADK
and Beyond!
GDG Detroit 2013
Terry May
Nerd
What's an ADK?
"The Accessory Development Kit is microcontroller development board that adheres to the simple Open Accessory Standard Protocol created by Google."
-Beginning Android ADK With Arduino
- M. Bohmer (Apress, 2012)
What's an ADK?
ADK from 2011
ADK From 2012
ADK 2011
ADK 2011
Demo
Networking
AndroidAccessory acc("DetroitLabs",
"ThermoDroid",
"Non contact 64 zone temp sensor",
"1.0",
"http://www.detroitlabs.com",
"0000000000000001");
Networking
Demo kit Manifest:
...
<activity android:name="UsbAccessoryActivity" android:label="DemoKit"
android:taskAffinity="" android:launchMode="singleInstance">
<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>
...
res/xml/accessory_filter.xml
<resources>
<usb-accessory manufacturer="Google, Inc." model="DemoKit" version="1.0" />
</resources>
Networking
void loop() {
...
if (acc.isConnected()) {
int len = acc.read(msg, sizeof(msg), 1);
if (len > 0) {
if(msg[0] == 0x3) {
if (msg[1] == 0x0)
digitalWrite(RELAY1, msg[2] ? HIGH : LOW);
if (msg[1] == 0x1)
digitalWrite(RELAY2, msg[2] ? HIGH : LOW);
}
}
}
...
}
Networking
void loop() {
...
if (acc.isConnected()) {
...
val = analogRead(TEMP_SENSOR);
msg[0] = 0x4;
msg[1] = val >> 8;
msg[2] = val & 0xff;
acc.write(msg, 3);
break;
...
}
...
}
ADK 2011
http://jeffreysambells.com/2011/05/17/understanding-the-demokit-pde-arduino-sketch
ADK 2012
Demo
USB-Serial
Demo
USB-SERIAL for Android
https://code.google.com/p/usb-serial-for-android/
Networking
Thermo Droid Manifest:
...
<activity android:name="UsbAccessoryActivity" android:label="DemoKit"
android:taskAffinity="" android:launchMode="singleInstance">
<intent-filter>
<action
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
...
Networking
res/xml/device_filter.xml
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" />
<!-- 0x2341 / Arduino -->
<usb-device vendor-id="9025" />
<!-- 0x16C0 / 0x0483: Teensyduino -->
<usb-device vendor-id="5824" product-id="1155" />
<!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
<usb-device vender-id="4292" product-id="60000" />
</resources>
Serial Framing Protocol
Start flag: 0×12
End flag: 0×13
Escape: 0x7D
Raw Data
DE AD 12 BE EF 7D FE ED
Framed Data
12 DE AD 7D 12 BE EF 7D 7D FE ED 13
Networking
void Send_Escaped_Data(byte b) {
if (b == START_FLAG ||
b == END_FLAG ||
b == ESCAPE) {
Serial.write(ESCAPE);
}
Serial.write(b);
}
void Temperatures_Serial_Transmit(){
Serial.write(START_FLAG);
for(int i=0;i<=63;i++){
//break float into 4 bytes
byte * b = (byte *) &temperatures[i];
for (int j=0; j<4; j++) {
Send_Escaped_Data(b[j]);
}
}
Serial.write(END_FLAG);
}
Networking
...
ByteBuffer inBuffer = ByteBuffer.wrap(data);
while(inBuffer.hasRemaining()) {
int b = inBuffer.get() & 0xFF;
if (escaped) {
ints[frameByteCount++] = b;
escaped = false;
continue;
}
switch (b) {
case ESCAPE:
escaped = true;
break;
case START_FLAG:
frameByteCount = 0;
ints = new int[512];
break;
case END_FLAG:
Message m = Message.obtain(mHandler, NEW_FRAME);
m.obj = ints;
m.arg1 = frameByteCount;
mHandler.sendMessage(m);
break;
default:
ints[frameByteCount++] = b; }
Networking
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
IntBuffer buff = IntBuffer.wrap((int[]) msg.obj, 0, msg.arg1);
ThermoFrame frame = new ThermoFrame(buff);
view.update(frame);
}
};
Networking
while(tempsToRead > 0) {
if (frame.limit() - frame.position() < 4) {
frame.clear();
break;
}
int[] f = new int[4];
frame.get(f, 0, 4);
int intbits = 0;
intbits = (f[3] << 24 | f[2] << 16 | f[1] << 8 | f[0]);
float temp = Float.intBitsToFloat(intbits);
tempSum += temp;
temps.put(temp);
tempsToRead--;
}
avgTemp = tempSum/64;
Questions?
THANK YOU!
ADK 2011
http://developer.android.com/tools/adk/adk.html
Communication Protocol
Title
Doing a Thing
Code example
Title with paragraph
Content
Networking
arduino code example