1 of 39

Android ADK

and Beyond!

GDG Detroit 2013

2 of 39

Terry May

Nerd

3 of 39

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)

4 of 39

What's an ADK?

ADK from 2011

  • Arduino Mega 2560
  • USB Host Shield

ADK From 2012

  • ARM 32-bit Cortex M3
  • USB & Bluetooth
  • Alarm Clock
  • Broken

5 of 39

ADK 2011

  • Backported to Android 2.3.4
  • Phone oem USB driver needs to support accessory mode
  • USB host should supply 500 mA @ 5v�to charge Android device.

6 of 39

7 of 39

8 of 39

ADK 2011

Demo

9 of 39

Networking

AndroidAccessory acc("DetroitLabs",

"ThermoDroid",

"Non contact 64 zone temp sensor",

"1.0",

"http://www.detroitlabs.com",

"0000000000000001");

10 of 39

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>

11 of 39

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);

}

}

}

...

}

12 of 39

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;

...

}

...

}

13 of 39

ADK 2011

http://jeffreysambells.com/2011/05/17/understanding-the-demokit-pde-arduino-sketch

14 of 39

15 of 39

16 of 39

ADK 2012

Demo

17 of 39

18 of 39

USB-Serial

Demo

19 of 39

20 of 39

21 of 39

22 of 39

23 of 39

24 of 39

USB-SERIAL for Android

  • Compatible with arduino serial chips
  • Compatible with only certain Android devices.
  • Nexus 4 is not Supported!

https://code.google.com/p/usb-serial-for-android/

25 of 39

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>

...

26 of 39

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>

27 of 39

28 of 39

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

29 of 39

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);

}

30 of 39

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; }

31 of 39

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);

}

};

32 of 39

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;

33 of 39

Questions?

34 of 39

THANK YOU!

35 of 39

ADK 2011

http://developer.android.com/tools/adk/adk.html

Communication Protocol

  1. The accessory is in wait state and tries to detect any connected devices.
  2. The Accessory checks for accessory mode support of the device.
  3. The Accessory tries to set the device in accessory mode if it is necessary.
  4. If the device supports the protocol, the accessory establishes the communication

36 of 39

Title

37 of 39

Doing a Thing

Code example

38 of 39

Title with paragraph

Content

39 of 39

Networking

arduino code example