Skip to content

Writing android apps in accessory mode

rajeshsola edited this page Nov 10, 2014 · 9 revisions

Here is an easiest way to develop android applications in accessory mode,i.e. establishing communication between an app and external accessory.

1.Add the skeleton class ADKControl responsible for communicating to ADK through USB connection All USB related communication is kept in this class with suitable methods for simple communication

2.As there is only one USBConenction which is required in many places ADKControl class is designed with singleton pattern,to use this skeleton class

Create an instance of ADKControl in OnCreate method of MainActivity by passing the reference of activity and obtain the reference of the ADKControl class in other places like additional threads,activities,fragments by passing null as follows

ADKControl adk=ADKControl.getInstance(this); //In MainActivity

ADKControl adk=ADKControl.getInstance(null); //In other places

3.For sending data from app to ADK,may be in appropriate listener code adk.sendCommand(byte_array,length);

(or) adk.sendCommand(byte1,byte2)

(or) adk.sendCommand

4.To receive data from ADK to app, may be in additional threads len=adk.readData(byte_array,maxlen); which returns actual no.of bytes received

5.Use the following methods of ADKControl class in appropriate overridden methods of MainActivity as per the activity lifecycle

adk.pauseAccessory(); //in OnPause method

adk.resumeAccessory(); //in OnResume method

adk.closeAccessory(); //in OnDestroy method

The skeleton code of ADKControl class is under hevey developments, needs lot of bug fixing,improvements and fine tuning,stay tuned at this wiki page and code repository for further updates

Clone this wiki locally