CH573 CH582 CH579 peripheral通用外设例子讲解三 修改广播包

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 );  //   扫描应答包

posted @ 2022-02-18 16:20  debugdabiaoge  阅读(2417)  评论(0编辑  收藏  举报