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

Changing device name dynamically

Hi there,

I am using the nRF connect app on Android to see my dongle's name. I want the name to change every 5 seconds from one name to another. This is just for evaluation.

How can I do this? I am using SDK 15 with SD S140. I am using the LED button example.

A snippet of my code:

          BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
          sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_2, strlen(DEVICE_NAME_2));
          ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
          sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params);
          sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
          bsp_board_led_on(CONNECTED_LED);
          nrf_delay_ms(5000);
          BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
          sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME));
          ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
          sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params);
          sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
          bsp_board_led_off(CONNECTED_LED);
          nrf_delay_ms(5000);

Parents
  • I was able to get this working by doing something like this:

    static void setDeviceName(deviceStatus_t status)
    {
        ble_gap_conn_sec_mode_t sec_mode;
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
        
        // Set the device name
        switch(status)
        {
            case STATUS_LOW:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_LOW, strlen(DEVICE_NAME_LOW));
                bsp_board_led_on(BSP_BOARD_LED_1);
                break;
            case STATUS_MED:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_MED, strlen(DEVICE_NAME_MED));
                bsp_board_led_on(BSP_BOARD_LED_3);
                break;
            case STATUS_HIGH:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_HIGH, strlen(DEVICE_NAME_HIGH));
                bsp_board_led_on(BSP_BOARD_LED_2);
                break;
            case STATUS_ERR:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_ERR, strlen(DEVICE_NAME_ERR));
                bsp_board_led_on(BSP_BOARD_LED_0);
                break;   
        }
    
        // Encode the advertising packet and start advertising
        ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params);
        sd_ble_gap_adv_stop(m_adv_handle);
        sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
    }

Reply
  • I was able to get this working by doing something like this:

    static void setDeviceName(deviceStatus_t status)
    {
        ble_gap_conn_sec_mode_t sec_mode;
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
        
        // Set the device name
        switch(status)
        {
            case STATUS_LOW:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_LOW, strlen(DEVICE_NAME_LOW));
                bsp_board_led_on(BSP_BOARD_LED_1);
                break;
            case STATUS_MED:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_MED, strlen(DEVICE_NAME_MED));
                bsp_board_led_on(BSP_BOARD_LED_3);
                break;
            case STATUS_HIGH:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_HIGH, strlen(DEVICE_NAME_HIGH));
                bsp_board_led_on(BSP_BOARD_LED_2);
                break;
            case STATUS_ERR:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_ERR, strlen(DEVICE_NAME_ERR));
                bsp_board_led_on(BSP_BOARD_LED_0);
                break;   
        }
    
        // Encode the advertising packet and start advertising
        ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params);
        sd_ble_gap_adv_stop(m_adv_handle);
        sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
    }

Children
No Data
Related