动态广播有两种实现方式:

1.关闭广播,更改广播包数据,等待关闭上报状态后,开启广播。

uint8_t initial_advertising_enable = FALSE;

GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);//关闭广播

GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);//修改扫描应答包数据
GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);//修改广播包数据

uint8_t initial_advertising_enable = TRUE;

GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);//开启广播

可以用tmos写个1s的定时任务,在任务中关闭广播,填写新数据,数据要符合广播包格式:

if(events & SBP_PERIODIC_EVT)
{

uint8_t initial_advertising_enable = FALSE;

uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;

GAP_SetParamValue(TGAP_DISC_ADV_INT_MIN, advInt);
GAP_SetParamValue(TGAP_DISC_ADV_INT_MAX, advInt);

GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);//关闭广播

 advertData[2]=advertData[2]+1;

GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);//修改扫描应答包数据
GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);//修改广播包数据

tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
return (events ^ SBP_PERIODIC_EVT);
}

在等待广播处开启广播:

if(pEvent->gap.opcode == GAP_END_DISCOVERABLE_DONE_EVENT)
{
PRINT("Waiting for advertising..\n");

uint8_t initial_advertising_enable = TRUE;

GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);开启广播

}

PS:一定不要修改完数据就开启广播,需要在报等待广播后开启广播,使用方式一也可以修改广播间隔。

2.调用更新广播包函数进行更新。

extern bStatus_t GAP_UpdateAdvertisingData( uint8_t taskID, uint8_t adType, uint16_t dataLen, uint8_t *pAdvertData );

GAP_UpdateAdvertisingData( Peripheral_TaskID,TRUE ,sizeof( advertData ),advertData );         //  广播包

GAP_UpdateAdvertisingData( Peripheral_TaskID,FALSE ,sizeof( scanRspData ),scanRspData );  //   扫描应答包

写个周期性的任务调用就可以了:

if(events & SBP_PERIODIC_EVT)
{

advertData[2]=advertData[2]+1;

GAP_UpdateAdvertisingData( Peripheral_TaskID,TRUE ,sizeof( advertData ),advertData );         //  广播包

GAP_UpdateAdvertisingData( Peripheral_TaskID,FALSE ,sizeof( scanRspData ),scanRspData );  //   扫描应答包

tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
return (events ^ SBP_PERIODIC_EVT);
}

posted on 2022-09-15 15:22  WCH蓝牙应用分享  阅读(1033)  评论(0编辑  收藏  举报