/*Functions for data exchanging with beacon application*/
bool_t GetBleAppStarted(void);
bool_t GetmAdvertisingOn(void);
void SetmAdvertisingOn(bool_t value);
#include "fsl_component_timer_manager.h"
#define UPDATE_BEACON_TIMER (5) //in seconds
/*Create timer ID*/
static TIMER_MANAGER_HANDLE_DEFINE(BeaconUpdateDataTimerID);
/*Callback prototype*/
static void UpdateBeaconTimerCallback(void * pParam);
/*Define the variables*/
static bool_t BleAppStarted = FALSE;
static bool_t mAdvertisingOn = FALSE;
/*Declare variable as external*/
extern gapAdvertisingData_t gAppAdvertisingData;
/*Define functions for data echange*/
bool_t GetBleAppStarted(void)
{
return BleAppStarted;
}
bool_t GetmAdvertisingOn(void)
{
return mAdvertisingOn;
}
void SetmAdvertisingOn(bool_t value)
{
mAdvertisingOn = value;
}
/*define the timer callback*/
static void UpdateBeaconTimerCallback(void * pParam)
{
/*Value that will be advertised*/
static int32_t count = 1;
/* Stop ADV and handle the update on the callbacks*/
Gap_StopAdvertising();
mAdvertisingOn = !mAdvertisingOn;
/* On ADV data 0-1 = company ID, 2 = Beacon ID, 3 -18 = UUID,
/* 19-20: A Data, 21-22: B Data, 23-24: C Data */
gAppAdvertisingData.aAdStructures[1].aData[19] = (uint8_t)((count >> 8) & 0xFF);
gAppAdvertisingData.aAdStructures[1].aData[20] = (uint8_t)(count & 0xFF);
count++;
}
case gAdvertisingParametersSetupComplete_c:
{
(void)Gap_SetAdvertisingData(mpAdvParams->pGapAdvData,
mpAdvParams->pScanResponseData);
if (!BleAppStarted) {
BleAppStarted = TRUE;
/*Allocate timer*/
(void) TM_Open(BeaconUpdateDataTimerID);
/* Start data update timer */
(void) TM_InstallCallback((timer_handle_t) BeaconUpdateDataTimerID,
UpdateBeaconTimerCallback, NULL);
(void) TM_Start((timer_handle_t) BeaconUpdateDataTimerID,
(uint8_t) kTimerModeSingleShot
| (uint8_t) kTimerModeLowPowerTimer,
TmSecondsToMilliseconds(UPDATE_BEACON_TIMER));
}
}
break;
case gAdvertisingDataSetupComplete_c: {
(void) Gap_StartAdvertising(App_AdvertisingCallback,
App_ConnectionCallback);
/* Start data update timer */
(void) TM_InstallCallback((timer_handle_t) BeaconUpdateDataTimerID,
UpdateBeaconTimerCallback, NULL);
(void) TM_Start((timer_handle_t) BeaconUpdateDataTimerID,
(uint8_t) kTimerModeSingleShot
| (uint8_t) kTimerModeLowPowerTimer,
TmSecondsToMilliseconds(UPDATE_BEACON_TIMER));
}
break;
#if defined(gBeaconAE_c) && (gBeaconAE_c)
static appExtAdvertisingParams_t mAppExtAdvParams = {
&gExtAdvParams,
&gAppExtAdvertisingData,
NULL,
mBeaconExtHandleId_c,
gBleExtAdvNoDuration_c,
gBleExtAdvNoMaxEvents_c
};
#endif /*gBeaconAE_c */
case gAdvertisingStateChanged_c:
{
/* update ADV data when is disabled */
if((!GetmAdvertisingOn()) && GetBleAppStarted())
{
Gap_SetAdvertisingData(&gAppAdvertisingData, NULL);
SetmAdvertisingOn(true);
}
if(GetmAdvertisingOn())
{
Led1On();
}
else
{
Led1Off();
#if defined(gBeaconAE_c) && (gBeaconAE_c)
if(mAppTargetState == mAppState_ExtAdv_c)
{
if (gBleSuccess_c != BluetoothLEHost_StartExtAdvertising(&mAppExtAdvParams, BleApp_AdvertisingCallback, NULL))
{
panic(0, 0, 0, 0);
}
}
#endif
}
}
break;
The IoT Toolbox is an all-in-one application that demonstrates NXP’s BLE functionalities, the implementation of BLE and custom profiles and the compatibility with different smartphones. This mobile application can be downloaded from the App Store and Google Play Store.