nRF51822 白名单

1. 先定义,要使用白名单的设备为peripheral,连接peripheral的设备为central

2. 白名单,就是只有在peripheral的flash中保持了信息的central才能和peripheral进行连接。

central的设备信息需要怎么才能保存到peripheral的flash中:首先central在 peripheral的BLE_FAST_ADV或者BLE_SLOW_ADV广播过程中连接到peripheral,然后centralperipheral进行绑定,peripheral发现central需要绑定,绑定成功后就把central的信息保存到flash中。

3. 使用了白名单,一般会把广播分成几种模式,常见是分成 如下几种模式:

/**@brief Advertisement states. */
typedef enum
{
    BLE_NO_ADV,                                                        /**< No advertising running. */   // 没有广播运行模式
    BLE_FAST_ADV_WHITELIST,                             /**< Advertising with whitelist. */  // 白名单运行模式,一般使用的参数同快速广播模式
    BLE_FAST_ADV,                                                    /**< Fast advertising running. */ // 快速广播模式
    BLE_SLOW_ADV,                                                   /**< Slow advertising running. */ // 慢速广播模式
    BLE_SLEEP,                                                            /**< Go to system-off. */ // 系统睡眠模式

} ble_advertising_mode_t;

其中每个模式多有个TIMEOUT的时间,该时间到了,会有一个BLE_GAP_EVT_TIMEOUT 事件,在on_ble_evt()对该事件进行处理,从而实现模式的转换。

case BLE_GAP_EVT_TIMEOUT:
if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
{
if (m_advertising_mode == BLE_SLEEP)
{
m_advertising_mode = BLE_NO_ADV;

// Configure buttons with sense level low as wakeup source.
nrf_gpio_cfg_sense_input( SIGNAL_ALERT_BUTTON,
        BUTTON_PULL,
        NRF_GPIO_PIN_SENSE_LOW);

nrf_gpio_cfg_sense_input( BOND_DELETE_ALL_BUTTON_ID,
        BUTTON_PULL,
        NRF_GPIO_PIN_SENSE_LOW);

// Go to system-off mode.
// (this function will not return; wakeup will cause a reset).
err_code = sd_power_system_off();
APP_ERROR_CHECK(err_code);
}
else
{
advertising_start();
}
}
break;


4. 实现白名单,是在advertising_start() 中:

/**@brief Function for starting advertising.
*/
static void advertising_start(void)
{
uint32_t             err_code;
ble_gap_adv_params_t adv_params;
ble_gap_whitelist_t  whitelist;
uint32_t             count;

// Verify if there is any flash access pending, if yes delay starting advertising until 
// it's complete.
err_code = pstorage_access_status_get(&count);
APP_ERROR_CHECK(err_code);

if (count != 0)
{
m_memory_access_in_progress = true;
return;
}

// Initialize advertising parameters with defaults values
memset(&adv_params, 0, sizeof(adv_params));

adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND;
adv_params.p_peer_addr = NULL;
adv_params.fp = BLE_GAP_ADV_FP_ANY;
adv_params.p_whitelist = NULL;

// Configure advertisement according to current advertising state.
switch (m_advertising_mode)
{
case BLE_NO_ADV:
m_advertising_mode = BLE_FAST_ADV_WHITELIST;
// fall through.

case BLE_FAST_ADV_WHITELIST:
{
ble_gap_addr_t * p_whitelist_addr[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
ble_gap_irk_t * p_whitelist_irk[BLE_GAP_WHITELIST_IRK_MAX_COUNT];

whitelist.addr_count = BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
whitelist.irk_count   = BLE_GAP_WHITELIST_IRK_MAX_COUNT;
whitelist.pp_addrs   = p_whitelist_addr;
whitelist.pp_irks     = p_whitelist_irk;

err_code = dm_whitelist_create(&m_app_handle, &whitelist);  // 获取flash中保存的白名单
APP_ERROR_CHECK(err_code);

if ((whitelist.addr_count != 0) || (whitelist.irk_count != 0))  // 白名单不为空,就使用白名单
{
adv_params.fp           = BLE_GAP_ADV_FP_FILTER_CONNREQ;
adv_params.p_whitelist = &whitelist;

advertising_init(BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED);
m_advertising_mode = BLE_FAST_ADV;
nrf_gpio_pin_set(ADV_WHITELIST_LED_PIN_NO);
}
else // 没有白名单就直接进入BLE_FAST_ADV 模式,下个模式为BLE_SLOW_ADV
{
m_advertising_mode = BLE_SLOW_ADV;
}

adv_params.interval = APP_ADV_INTERVAL_FAST;
adv_params.timeout   = APP_FAST_ADV_TIMEOUT;
break;
}

case BLE_FAST_ADV:
advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);

adv_params.interval = APP_ADV_INTERVAL_FAST;
adv_params.timeout   = APP_FAST_ADV_TIMEOUT;
m_advertising_mode   = BLE_SLOW_ADV;
break;


case BLE_SLOW_ADV:
advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);

adv_params.interval = APP_ADV_INTERVAL_SLOW;
adv_params.timeout   = APP_SLOW_ADV_TIMEOUT;
m_advertising_mode   = BLE_SLEEP;
break;


default:
// No implementation needed.
break;
}


// Start advertising. // 开始广播
err_code = sd_ble_gap_adv_start(&adv_params);
APP_ERROR_CHECK(err_code);
}

你可能感兴趣的:(ARM)