(一)蓝牙之广播:
1 广播参数之广播名称
typedef enum
{
BLE_ADVDATA_NO_NAME,/**< Include no device name in advertising data. */
BLE_ADVDATA_SHORT_NAME,/**< Include short device name in advertising data. */
BLE_ADVDATA_FULL_NAME/**< Include full device name in advertising data. */
} ble_advdata_name_type_t;
没有名称 ,短名称,全名,如果广播的name为short的话还需将name的长度一并指出:
ble_advdata_name_type_tname_type;
/**< Type of device name. */
uint8_tshort_name_len;
/**< Length of short device name (if short type is specified). */
【BLE蓝牙广播和扫描主要数据设置解析与总结】2 广播参数之广播flag设置
#define BLE_GAP_ADV_FLAG_LE_LIMITED_DISC_MODE(0x01)/**< LE Limited Discoverable Mode. */
#define BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE(0x02)/**< LE General Discoverable Mode. */
#define BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED(0x04)/**< BR/EDR not supported. */
#define BLE_GAP_ADV_FLAG_LE_BR_EDR_CONTROLLER(0x08)/**< Simultaneous LE and BR/EDR, Controller. */
#define BLE_GAP_ADV_FLAG_LE_BR_EDR_HOST(0x10)/**< Simultaneous LE and BR/EDR, Host. */
#define BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE(BLE_GAP_ADV_FLAG_LE_LIMITED_DISC_MODE | BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED)/**< LE Limited Discoverable Mode, BR/EDR not supported. */
#define BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE(BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE | BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED)/**< LE General Discoverable Mode, BR/EDR not supported. */
/**@} */
BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE:有限可发现模式,不支持BR/EDR
LE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE:一般可发现模式,不支持BR/EDR
BR/EDR:蓝牙基本数率/增强数据率()
有限可发现模式广播的间隔比一般可发现模式小。 从时间的限制上,我们可以看出有限可发现模式对连接的迫切性和目的性比一般可发现模式高,一个处于有限可发现模式的设备正在广播,那么他一定是刚被用户操作过并且极希望被连接。 一般情况下,设备首次开机、按下连接按钮,设备进入有限可发现模式比较合适。如果在有限可发现模式时间内没有被连接,可以转入一般可发现模式。 如果我们希望设备在没有被连接时一直保持广播,那么应该使用一般可发现模式,因为一般可发现模式是没有时间限制的。
若想设置时间为无穷大 #define APP_ADV_DURATION 0
3 广播参数之广播类型设置
定义枚举
typedef enum
{
BLE_ADV_MODE_IDLE,/**< Idle;
no connectable advertising is ongoing. */
BLE_ADV_MODE_DIRECTED_HIGH_DUTY, /**< Directed advertising (high duty cycle) attempts to connect to the most recently disconnected peer. */
BLE_ADV_MODE_DIRECTED,/**< Directed advertising (low duty cycle) attempts to connect to the most recently disconnected peer. */
BLE_ADV_MODE_FAST,/**< Fast advertising will connect to any peer device, or filter with a whitelist if one exists. */
BLE_ADV_MODE_SLOW,/**< Slow advertising is similar to fast advertising. By default, it uses a longer advertising interval and time-out than fast advertising. However, these options are defined by the user. */
} ble_adv_mode_t;
5种广播类型,定向直连为两种
使能结构体
typedef struct{
boolble_adv_on_disconnect_disabled;
/**< Enable or disable automatic return to advertising upon disconnecting.*/
boolble_adv_whitelist_enabled;
/**< Enable or disable use of the whitelist. */
boolble_adv_directed_high_duty_enabled;
/**< Enable or disable direct advertising mode. can only be used if ble_adv_legacy_enabled is true. */
boolble_adv_directed_enabled;
/**< Enable or disable direct advertising mode. */
boolble_adv_fast_enabled;
/**< Enable or disable fast advertising mode. */
boolble_adv_slow_enabled;
/**< Enable or disable slow advertising mode. */
uint32_t ble_adv_directed_interval;
/**< Advertising interval for directed advertising. */
uint32_t ble_adv_directed_timeout;
/**< Time-out (number of tries) for direct advertising. */
uint32_t ble_adv_fast_interval;
/**< Advertising interval for fast advertising. */
uint32_t ble_adv_fast_timeout;
/**< Time-out (in seconds) for fast advertising. */
uint32_t ble_adv_slow_interval;
/**< Advertising interval for slow advertising. */
uint32_t ble_adv_slow_timeout;
/**< Time-out (in seconds) for slow advertising. */
boolble_adv_extended_enabled;
/**< Enable or disable extended advertising. */
uint32_t ble_adv_secondary_phy;
/**< PHY for the secondary (extended) advertising @ref BLE_GAP_PHYS (BLE_GAP_PHY_1MBPS, BLE_GAP_PHY_2MBPS or BLE_GAP_PHY_CODED). */
uint32_t ble_adv_primary_phy;
/**< PHY for the primary advertising. @ref BLE_GAP_PHYS (BLE_GAP_PHY_1MBPS, BLE_GAP_PHY_2MBPS or BLE_GAP_PHY_CODED). */
} ble_adv_modes_config_t;
设置广播模式时未配置为递进状态:
如图:
1.在超时处理时由Direct 到Fast到slow到Idle,
2.若设置了FAST广播模式会由于超时而进入slow再到idle
3.若设置不想进入IDLE即保持一种广播模式不变可以注销超时时间
init.config.ble_adv_fast_timeout = APP_ADV_DURATION; 使APP_ADV_DURATION为0
APP_ADV_DURATION广播持续时间,广播超时时间
4 广播参数之广播数据设置
manuf_specific_data.data.p_data = https://www.it610.com/article/my_adv_manuf_data;
//my_adv_manuf_data中为自定义数据
manuf_specific_data.data.size= 23;
init.advdata.p_service_data_array = &service_data;
init.advdata.p_manuf_specific_data= &manuf_specific_data
5 广播参数之广播回包数据设置
//名字
init.srdata.name_type= BLE_ADVDATA_FULL_NAME;
//UUID
manuf_specific_data1.data.p_data = https://www.it610.com/article/&my_adv_manuf_data[23];
manuf_specific_data1.data.size= 23;
//数据
init.srdata.p_manuf_specific_data= &manuf_specific_data1;
6 广播参数之广播间隔
广播间隔指的是每次蓝牙广播之间的间隔,间隔大,功耗低,间隔越小,功耗越大
间隔时间=advinterval+advdelay
advInterval 必须是“0.625ms”的整数倍,范围是“20ms ~ 10.24s”之间。对于可扫描非定向广播和不可连接非定向广播这两种广播类型,该值最好不小于100ms,即(160个0.625ms)。advDelay是 Link Layer(链接层)分配的一个伪随机数,它的范围为“0 ~ 10ms”。
7 广播参数修改
#define APP_ADV_INTERVAL32/**< The advertising interval (in units of 0.625 ms). This value corresponds to 187.5 ms. *///#define APP_ADV_DURATION18000/**< The advertising duration (180 seconds) in units of 10 milliseconds. */
//#define APP_ADV_INTERVAL300
#define APP_ADV_DURATION30
(二)蓝牙之扫描
扫描是一个在一定范围内用来寻找其他低功耗蓝牙设备广播的过程。扫描设备在扫描过程中会使用广播信道,于广播过程不相同的是扫描没有严格的时间定义和信道规则
1 扫描参数设置
扫描开始函数
static void scan_start(void)
{
ret_code_t err_code;
(void) sd_ble_gap_scan_stop();
//协议栈封装函数利用 m_scan_params来进行扫描
err_code = sd_ble_gap_scan_start(&m_scan_params, &m_scan_buffer);
// It is okay to ignore this error since we are stopping the scan anyway.
if (err_code != NRF_ERROR_INVALID_STATE)
{
APP_ERROR_CHECK(err_code);
}
}
static ble_gap_scan_params_t const m_scan_params =
{
.extended= 1,
.active= 1,
.interval= SCAN_INTERVAL,
.window= SCAN_WINDOW,
.timeout= SCAN_DURATION,
.scan_phys= BLE_GAP_PHY_1MBPS,
.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
};
.active 参数表示选择扫描模式,扫描模式分为主动扫描和被动扫描
.timeout 扫描超时时间 超过指定时间过后没有扫描到设备后将停止扫描
.interval 参数表示扫描间隔,控制器多久扫描一次 也就是两个连续的扫描窗口的开始时间的时间 间隔
.window 扫描窗口,每次扫描所持续的时间,在持续时间内扫描设备一直在广播信道上运行
.filter_policy 拦截政策
文章图片
.timeout 的范围 0X0001到0xffff 单位为s间隔时间为1s到65535s,设置为0x0000则无超时时间
.interval 的范围 0x0004到0x4000 单位为0.625ms间隔为2.5ms到10.24s
.window 的范围 0x0004到0x4000 单位为0.625ms间隔为2.5ms到10.24s
#define SCAN_INTERVAL0x00A0//16*10*0.625=100ms/**< Determines scan interval in units of 0.625 millisecond. */
#define SCAN_WINDOW0x0050//5*16*0.625=50ms/**< Determines scan window in units of 0.625 millisecond. */#define SCAN_DURATION0x0000/**< Duration of the scanning in units of 10 milliseconds. If set to 0x0000, scanning will continue until it is explicitly disabled. */