nRF51822白名单实现

nRF51822白名单实现

static void _advertising_init(uint8_t adv_flags) {
    uint32_t err_code;
    ble_advdata_t advdata;
    ble_uuid_t adv_uuids[] = { { BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE, BLE_UUID_TYPE_BLE } };
    // Build and set advertising data
    memset(&advdata, 0, sizeof(advdata));
    advdata.name_type = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance = true;
    advdata.flags.size = sizeof(adv_flags);
    advdata.flags.p_data = &adv_flags;
    advdata.uuids_complete.uuid_cnt = sizeof(adv_uuids)  sizeof(adv_uuids[0]);
    advdata.uuids_complete.p_uuids = adv_uuids;
    err_code = ble_advdata_set(&advdata, NULL );
    APP_ERROR_CHECK(err_code);
    // Initialise advertising parameters (used when starting advertising)
    memset(&adv_params, 0, sizeof(adv_params));
    adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND;
    adv_params.p_peer_addr = NULL;                           // Undirected advertisement
    adv_params.fp = BLE_GAP_ADV_FP_ANY;
    adv_params.p_whitelist = NULL;
    adv_params.interval = APP_ADV_INTERVAL;
    adv_params.timeout = APP_ADV_TIMEOUT_IN_SECONDS;
    advertising_mode = BLE_NO_ADV;
}


static void _advertising_start(void) {
    uint32_t err_code;
    ble_gap_whitelist_t whitelist;
    ble_gap_addr_t peer_address;
    char str[6];
    err_code = ble_bondmngr_whitelist_get(&whitelist);
    APP_ERROR_CHECK(err_code);
    if (ble_bondmngr_master_addr_get(last_connected_master, &peer_address) == NRF_SUCCESS) {
        adv_params.p_peer_addr = &peer_address;
        debug_print_array_hex(peer_address.addr, BLE_GAP_ADDR_LEN);
        debug_uart_putstring("");
        advertising_mode = BLE_DIRECTED_ADV;
    }
    else if ((whitelist.addr_count != 0) || (whitelist.irk_count != 0)) {
        _advertising_init(BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED);
        adv_params.fp = BLE_GAP_ADV_FP_FILTER_BOTH;
        adv_params.p_whitelist = &whitelist;
        advertising_mode = BLE_ADV_WHITELIST;
    }
    else {
        advertising_mode = BLE_ADV;
    }
    debug_fmt_u32(str, advertising_mode);
    debug_uart_putstring(str);
    debug_uart_new_line();
    err_code = sd_ble_gap_adv_start(&adv_params);
    APP_ERROR_CHECK(err_code);
}  

你可能感兴趣的:(nRF51822白名单实现)