Datasheet

if(device_attached == FALSE)
{
//Continue to the top of the while loop to start the check over again.
continue;
}
//If the accessory is ready, then this is where we run all of the demo code
if(readInProgress == FALSE)
{
//This example shows how the handle is required for the transfer functions
errorCode = AndroidAppRead(device_handle,
(BYTE*)&command_packet,
(DWORD)sizeof(ACCESSORY_APP_PACKET));
//If the device is attached, then lets wait for a command from the application
if( errorCode != USB_SUCCESS)
{
//Error
DEBUG_ERROR("Error trying to start read");
}
else
{
readInProgress = TRUE;
}
}
}
}
BOOL USB_ApplicationEventHandler( BYTE address, USB_EVENT event, void *data, DWORD size )
{
switch( event )
{
//Android device has been removed.
case EVENT_ANDROID_DETACH:
device_attached = FALSE;
device_handle = NULL;
return TRUE;
break;
//Android device has been added. Must record the device handle
case EVENT_ANDROID_ATTACH:
device_attached = TRUE;
device_handle = data;
return TRUE;
//Handle other events here that are required...
//...
4.2.5 Sending Data
There are two functions that are associated with sending data from the Accessory to the device:
AndroidAppIsWriteComplete ( see page 23)()
and
AndroidAppWrite ( see page 26)()
. The
AndroidAppWrite ( see page 26)() function is used to send data from the Accessory to the Android device. The
AndroidAppIsWriteComplete ( see page 23)() function is used to determine if a previous transfer has completed.
Remember from the Keeping the Stack Running ( see page 18) section that the
USBTasks()
function needs to be called
in order to keep the stack running. This means that you shouldn't loop on the
AndroidAppIsWriteComplete ( see
page 23)()
function. Instead use either a state machine or booleans to indicate what you need to do.
while(1)
{
USBTasks();
//Do some extra stuff here to see if the buttons have updated
if( writeInProgress == TRUE )
{
Microchip's Accessory Framework for Android(tm) 19
19
4