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

nRF52 change device name

Hello everyone,

I would like to change the name of my device with sd_ble_gap_device_name_set (). But the resulted name in the nRF Connect app is always "N/A".

Here's my code :

/**@brief Function for initializing the Advertising functionality.
 *
 * @details Encodes the required advertising data and passes it to the stack.
 *          Also builds a structure to be passed to the stack when starting advertising.
 */
static void advertising_init(void)
{
    uint32_t      err_code;
    int8_t tx_power = 8;
    ble_gap_conn_sec_mode_t sec_mode;
    
    err_code = sd_ble_gap_tx_power_set(tx_power);//Set the radio's transmit power in dBm.
        
    sd_power_dcdc_mode_set(1);

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
    err_code = sd_ble_gap_device_name_set(&sec_mode,
                                          (const uint8_t *) DEVICE_NAME,
                                          strlen(DEVICE_NAME));
    APP_ERROR_CHECK(err_code);

    BLE_data[0] = 0x1E;
    BLE_data[1] = BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA; //able to have 29 bytes for custom data
    
    err_code = sd_ble_gap_adv_data_set(BLE_data,sizeof(BLE_data),NULL,0);
    
    // Initialize advertising parameters (used when starting advertising).
    memset(&m_adv_params, 0, sizeof(m_adv_params));

    m_adv_params.type        = BLE_GAP_ADV_TYPE_ADV_NONCONN_IND;
    m_adv_params.p_peer_addr = NULL;    // Undirected advertisement.
    m_adv_params.fp          = BLE_GAP_ADV_FP_ANY;
    m_adv_params.interval    = NON_CONNECTABLE_ADV_INTERVAL;
    m_adv_params.timeout     = 0;       // Never time out.

}

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

    err_code = sd_ble_gap_adv_start(&m_adv_params, APP_BLE_CONN_CFG_TAG);
    APP_ERROR_CHECK(err_code);

}

Edit :

If I configure the BLE packet by myself, it's working fine. The code looks like this :

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

    BLE_data[0] = 0x0B; // Short Local Name length
    BLE_data[1] = BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME;// Short Local Name type
    BLE_data[2] = 'H';
    BLE_data[3] = 'E';
    BLE_data[4] = 'R';
    BLE_data[5] = 'E';
    BLE_data[6] = ''';
    BLE_data[7] = 'S';
    BLE_data[8] = 'N';
    BLE_data[9] = 'A';
    BLE_data[10] ='M';
    BLE_data[11] ='E';
    BLE_data[12] = 0x04; //Specific data length
    BLE_data[13] = BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA; //Specific data type
    BLE_data[14] = 0xAA;
    BLE_data[15] = 0xBB;
    BLE_data[16] = 0xCC;
    
    err_code = sd_ble_gap_adv_data_set(BLE_data,sizeof(BLE_data),NULL,0);

Indeed, I don't know what does the sd_ble_gap_device_name_set function. I don't found its source code anywhere. Anyone know ?

Sincerely,

Sylvain.

Related