TI BLE: Advertisement

 #define GAPROLE_ADVERT_ENABLED 0x305 //!< Enable/Disable Advertising. Read/Write. Size is uint8. Default is TRUE=Enabled.
#define GAPROLE_ADVERT_OFF_TIME 0x306 //!< Advertising Off Time for Limited advertisements (in milliseconds). Read/Write. Size is uint16. Default is 30 seconds.

    GAPROLE_ADVERT_OFF_TIME—表示外设关闭广播持续时间,该值为零表示无限期关闭广播直到下一次广播使能信号到来。

GAPROLE_ADVERT_OFF_TIME – This parameter is used when the device is put into limited discoverable mode. It sets how long the device should wait before becoming discoverable again at the end of the limited discovery period. By setting this value to 0, the device will not become discoverable again until the GAPROLE_ADVERT_ENABLED is set back to TRUE.

上面来自TI论坛

#if defined( CC2540_MINIDK )
// For the CC2540DK-MINI keyfob, device doesn't start advertising until button is pressed
uint8 initial_advertising_enable = FALSE;
#else
// For other hardware platforms, device starts advertising upon initialization
uint8 initial_advertising_enable = TRUE;
#endif

// By setting this to zero, the device will go into the waiting state after
// being discoverable for 30.72 second, and will not being advertising again
// until the enabler is set back to TRUE
uint16 gapRole_AdvertOffTime = 0;

1   // Set advertising interval
2   {
3     uint16 advInt = DEFAULT_ADVERTISING_INTERVAL;  //100ms
4 
5     GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt );
6     GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt );
7     GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt );
8     GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt );
9   }
Set advertising interval

// Set the GAP Role Parameters

GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );
GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );

设置扫描应答数据和广播数据

GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData );
GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData );

 1 // GAP - SCAN RSP data (max size = 31 bytes)
 2 static uint8 scanRspData[] =
 3 {
 4 // complete name
 5 0x14, // length of this data
 6 GAP_ADTYPE_LOCAL_NAME_COMPLETE,
 7 0x53, // 'S'
 8 0x69, // 'i'
 9 0x6d, // 'm'
10 0x70, // 'p'
11 0x6c, // 'l'
12 0x65, // 'e'
13 0x42, // 'B'
14 0x4c, // 'L'
15 0x45, // 'E'
16 0x50, // 'P'
17 0x65, // 'e'
18 0x72, // 'r'
19 0x69, // 'i'
20 0x70, // 'p'
21 0x68, // 'h'
22 0x65, // 'e'
23 0x72, // 'r'
24 0x61, // 'a'
25 0x6c, // 'l'
26 
27 // connection interval range
28 0x05, // length of this data
29 GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
30 LO_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ), // 100ms
31 HI_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ),
32 LO_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ), // 1s
33 HI_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ),
34 
35 // Tx power level
36 0x02, // length of this data
37 GAP_ADTYPE_POWER_LEVEL,
38 0 // 0dBm
39 };
SCAN RSP data
 1 // GAP - Advertisement data (max size = 31 bytes, though this is
 2 // best kept short to conserve power while advertisting)
 3 static uint8 advertData[] =
 4 {
 5 // Flags; this sets the device to use limited discoverable
 6 // mode (advertises for 30 seconds at a time) instead of general
 7 // discoverable mode (advertises indefinitely)
 8 0x02, // length of this data
 9 GAP_ADTYPE_FLAGS,
10 DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
11 
12 // service UUID, to notify central devices what services are included
13 // in this peripheral
14 0x03, // length of this data
15 GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all
16 LO_UINT16( SIMPLEPROFILE_SERV_UUID ),
17 HI_UINT16( SIMPLEPROFILE_SERV_UUID ),
18 
19 };
Advertisement data

广播模式在数组中定义

// Flags; this sets the device to use limited discoverable
// mode (advertises for 30 seconds at a time) instead of general
// discoverable mode (advertises indefinitely)

#if defined ( CC2540_MINIDK )
#define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_LIMITED
#else
#define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL
#endif // defined ( CC2540_MINIDK )

 除了模式外也声明是否支持标准蓝牙或者只是BLE

 DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,

实验:当 GAP_ADTYPE_FLAGS_GENERAL时候确实为一直广播

GAP_ADTYPE_FLAGS_LIMITED为一次性广播,。

修改模式

#if defined ( CC2540_MINIDK )
#define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_LIMITED
#else
//#define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL
#define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_LIMITED
#endif // defined ( CC2540_MINIDK )

 GAP_ADTYPE_FLAGS_LIMITED模式下广播间隙由GAPROLE_ADVERT_OFF_TIME来设置,ms单位,如果GAPROLE_ADVERT_OFF_TIME为0则不再广播直到收到

 GAP_SetParamValue(TGAP_LIM_ADV_TIMEOUT, 60);设置每次的广播时长,单位s ,这里广播60秒

当GAPROLE_ADVERT_OFF_TIME=0,广播一次60s停下,直到用下面的开关打开

 1   uint8 current_adv_enabled_status;
 2       uint8 new_adv_enabled_status;
 3 
 4       //Find the current GAP advertisement status
 5       GAPRole_GetParameter( GAPROLE_ADVERT_ENABLED, &current_adv_enabled_status );
 6 
 7       if( current_adv_enabled_status == FALSE )
 8       {
 9         new_adv_enabled_status = TRUE;
10       }
11       else
12       {
13         new_adv_enabled_status = FALSE;
14       }
ADV 开关

 当GAPROLE_ADVERT_OFF_TIME=30000,广播一次60s停下,停下30秒之后自动又再次广播见下图,60秒停下90秒又开始广播

原文地址:https://www.cnblogs.com/wwjdwy/p/3631425.html