BLE学习(5)
介绍
上一篇介绍了BLE应用的工程,这一篇将针对实际项目应用实现部分功能。
1.动态广播数据更新
动态广播数据更新主要是利用GAP_UpdateAdvertisingData 函数实现,在前面的文章中介绍过GAP_UpdateAdvertisingData 的参数与作用
/*******************************************************************************
* @fn GAP_UpdateAdvertisingData
*
* @brief Setup or change advertising and scan response data.
*
* NOTE: if the return status from this function is SUCCESS,
* the task isn't complete until the GAP_ADV_DATA_UPDATE_DONE_EVENT
* is sent to the calling application task.
*
* input parameters
*
* @param taskID - task ID of the app requesting the change
* @param adType - TRUE - advertisement data, FALSE - scan response data
* @param dataLen - Octet length of advertData
* @param pAdvertData - advertising or scan response data
*
* output parameters
*
* @param None.
*
* @return SUCCESS: data accepted,<BR>
* bleIncorrectMode: invalid profile role,<BR>
*/
extern bStatus_t GAP_UpdateAdvertisingData( u8 taskID,u8 adType,u8 dataLen,u8 *pAdvertData );
taskID - 为任务在TMOS中注册的ID
adType - TRUE对广播数据修改,FALSE对扫描回应数据修改
dataLen - 为广播数据或扫描回应数据数组长度
pAdvertData - 为将要修改成的广播数据(格式与advertData[] 相同,需要数据长度,数据类型,数据值)
1)首先在uint16 Broadcaster_ProcessEvent( uint8 task_id, uint16 events ) 回调函数中添加一个“动态广播更新”事件
if ( events & SBP_UPDATE_ADV_EVT ){
// Updata the advertising data
}
2)然后在主函数broadcaster_main.c中添加“动态广播更新”事件宏定义
#define SBP_UPDATE_ADV_EVT 0x0005
3)在应用程序broadcaster.c中添加一个广播周期定义
static uint32 sBp_adv_Broadcaster_process_evt_period = 1000;
4)在刚才添加的“动态广播更新”事件中完成广播数据的更新
在看相关资料时有看到使用GAP_UpdateAdvertisingData更新广播数据时需要先将广播关闭,修改完成后再打开广播使能,我这里也进行了这样的操作,没有实际测试,不知道是否必要
if ( events & SBP_UPDATE_ADV_EVT ){
// Updata the advertising data
uint8 initial_advertising_enable = FALSE;
GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );
uint8 advertData_UpData[]=
{
0x05,
GAP_ADTYPE_LOCAL_NAME_SHORT,
'y',
'a',
'n',
0x3A,
0x03,
GAP_ADTYPE_MANUFACTURER_SPECIFIC,
0x00,
0x00
};
abcBuff = ADC_ExcutSingleConver() + RoughCalib_Value;
PRINT("%d ", ADC_GetCurrentTS(abcBuff));
sTemperature_L = (abcBuff&0xff);
sTemperature_H = (abcBuff&0xff00)>>8;
advertData_UpData[8] = sTemperature_H;
advertData_UpData[9]