void Peripheral_Init() { Peripheral_TaskID = TMOS_ProcessEventRegister(Peripheral_ProcessEvent); // Setup the GAP Peripheral Role Profile { uint8_t initial_advertising_enable = TRUE; uint16_t desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; uint16_t desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; // Set the GAP Role Parameters 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);//設置掃廣播包 GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desired_min_interval); GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desired_max_interval);//設置預期的連接間隔 }
如果需要修改廣播包,需要先把廣播關閉
uint8_t initial_advertising_enable = FALSE; // Set the GAP Role Parameters GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);//關閉廣播包
修改廣播數據
advertData[5]=advertData[5]+1; //示例,修改廣播數據, GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);//重新設置掃描應答包 GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);//重新設置廣播包
重新使能廣播
uint8_t initial_advertising_enable = TRUE; // Set the GAP Role Parameters GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);//使能廣播
測試效果
程序運行,可以看廣播開啟廣播引起的狀態變化 static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEvent_t *pEvent)
抓包看廣播包信息,修改位置是變化的
直接動態修改廣播包和掃描應答包(不用關閉廣播,可以直接調用下面這個函數修改廣播和掃描應答包)
/******************************************************************************* * @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 );
直接修改,不用開關廣播,提供的接口
/**
* @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.
*
* @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
*
* @return SUCCESS: data accepted
* bleIncorrectMode: invalid profile role
*/
extern bStatus_t GAP_UpdateAdvertisingData( uint8_t taskID, uint8_t adType, uint16_t dataLen, uint8_t *pAdvertData );
例如:
GAP_UpdateAdvertisingData( 0,TRUE ,sizeof( advertData ),advertData ); // 廣播包
GAP_UpdateAdvertisingData( 0,FALSE ,sizeof( scanRspData ),scanRspData ); // 掃描應答包