This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

regarding the one unit with advertisement only on channel 37 and the other unit scan on only channel 37

I am using the SDK 17.0.2 to make the advertisement and scanning for the two nrf52840 boards. One unit with extended advertisement only on channel 37 and the other unit scan on only channel 37. I am not sure whether that is correct or not, could you please help to check? If I want to do both on channel 38 only instead of channel 37, how should I change my code? 

advertisement code as below 

static void advertising_init(void)
{
    uint32_t      err_code;
    ble_advdata_t advdata;
    uint8_t       flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;

    ble_advdata_manuf_data_t manuf_specific_data;

    manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;

#if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
    // If USE_UICR_FOR_MAJ_MIN_VALUES is defined, the major and minor values will be read from the
    // UICR instead of using the default values. The major and minor values obtained from the UICR
    // are encoded into advertising data in big endian order (MSB First).
    // To set the UICR used by this example to a desired value, write to the address 0x10001080
    // using the nrfjprog tool. The command to be used is as follows.
    // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val <your major/minor value>
    // For example, for a major value and minor value of 0xabcd and 0x0102 respectively, the
    // the following command should be used.
    // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val 0xabcd0102
    uint16_t major_value = ((*(uint32_t *)UICR_ADDRESS) & 0xFFFF0000) >> 16;
    uint16_t minor_value = ((*(uint32_t *)UICR_ADDRESS) & 0x0000FFFF);

    uint8_t index = MAJ_VAL_OFFSET_IN_BEACON_INFO;

    m_beacon_info[index++] = MSB_16(major_value);
    m_beacon_info[index++] = LSB_16(major_value);

    m_beacon_info[index++] = MSB_16(minor_value);
    m_beacon_info[index++] = LSB_16(minor_value);
#endif
    manuf_specific_data.data.p_data = (uint8_t *) m_beacon_info;
    manuf_specific_data.data.size   = APP_BEACON_INFO_LENGTH;

    // Build and set advertising data.
    memset(&advdata, 0, sizeof(advdata));

    advdata.name_type             = BLE_ADVDATA_FULL_NAME;
    advdata.flags                 = flags;
    advdata.p_manuf_specific_data = &manuf_specific_data;

    // Initialize advertising parameters (used when starting advertising).
    memset(&m_adv_params, 0, sizeof(m_adv_params));

    m_adv_params.properties.type = BLE_GAP_ADV_TYPE_EXTENDED_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
    m_adv_params.p_peer_addr     = NULL;    // Undirected advertisement.
    m_adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
    m_adv_params.interval        = NON_CONNECTABLE_ADV_INTERVAL;
    m_adv_params.duration        = 0;       // Never time out.
    //m_adv_params.channel_mask.ch_7_off = 0;
    //m_adv_params.channel_mask.ch_7_off = 0;
    //m_adv_params.channel_mask.ch_37_off = 1;
    m_adv_params.channel_mask[4] = 0x20;

    err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
    APP_ERROR_CHECK(err_code);

    err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
    APP_ERROR_CHECK(err_code);

    err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV,m_adv_handle,8);//ÉèÖ÷¢É书ÂÊ
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for starting advertising.
 */
static void advertising_start(void)
{
    ret_code_t err_code;

    err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
    APP_ERROR_CHECK(err_code);

    err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
    APP_ERROR_CHECK(err_code);
}

scanning code as below 

static void scan_init(void)
{
    ret_code_t          err_code;
    nrf_ble_scan_init_t init_scan;
    ble_gap_scan_params_t params;

    memset(&params, 0, sizeof(params));

    params.active  = 1;
    params.interval = NRF_BLE_SCAN_SCAN_INTERVAL;
    params.window = NRF_BLE_SCAN_SCAN_WINDOW;
    params.timeout = NRF_BLE_SCAN_SCAN_DURATION;
    params.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL;
    params.scan_phys = NRF_BLE_SCAN_SCAN_PHY;
    params.channel_mask[4] = 0x20; //try to lock on channel 37 niklas
    params.extended = true;

    memset(&init_scan, 0, sizeof(init_scan));

    init_scan.conn_cfg_tag     = 1;
    init_scan.p_scan_param     = &params;
    

    err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
    APP_ERROR_CHECK(err_code);

}



/**@brief Function for handling the idle state (main loop).
 *
 * @details Handle any pending log operation(s), then sleep until the next event occurs.
 */
static void idle_state_handle(void)
{
    NRF_LOG_FLUSH();
    nrf_pwr_mgmt_run();
}


int main(void)
{
    // Initialize.
    log_init();
    timer_init();
    leds_init();
    power_management_init();
    ble_stack_init();
    scan_init();

    // Start execution.
    NRF_LOG_INFO("Beacon scanner example started.");
    scan_start();

    // Turn on the LED to signal scanning.
    bsp_board_led_on(CENTRAL_SCANNING_LED);

    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}

  • Hi Luan, 

    When starting scanning or advertisement you have the possibility to control a parameter called: ble_gap_ch_mask_t ble_gap_scan_params_t::channel_mask 

    Take note of what the documentation says about the ble_gap_ch_mask_t structure:

    • Channel mask (40 bits).
    • Every channel is represented with a bit positioned as per channel index defined in Bluetooth Core Specification v5.0, Vol 6, Part B, Section 1.4.1.
    • The LSB contained in array element 0 represents channel index 0, and bit 39 represents channel index 39. If a bit is set to 1, the channel is not used.

    Also, see my colleague Jared's explanation in this post

    -Amanda H.

Related