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

How to change DEVICE_NAME by nRF Connect APP?

Device: nRF52832

SoftDevice: S132, 7.0.1

SDK: 16.0.0

I want change DEVICE_NAME by nRF Connect APP, then I add below code, but I found it die here "wait_for_flash_ready".

Can't call nrf_fstorage_write functions in ble_evt_handler function?

Or is there a better way to modify the Bluetooth broadcast name?

Many thanks and best regards!

John

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    ret_code_t err_code;

    switch (p_ble_evt->header.evt_id)
    {
        //Above omitted
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO("Disconnected");
            bsp_board_led_off(CONNECTED_LED);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            //err_code = app_button_disable();
            //APP_ERROR_CHECK(err_code);
            if (name_change_flag == 1)
            {
                advertising_stop(&m_advertising);
                gap_params_init();
                advertising_init();
                name_change_flag = 0;
            }
            advertising_start();
            break;
        //Add
        case BLE_GATTS_EVT_WRITE:
            NRF_LOG_INFO("Enter BLE_GATTS_EVT_WRITE.");
            if (p_ble_evt->evt.gatts_evt.params.write.uuid.uuid == BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME)
            {
                for(uint32_t counter=0;counter<p_ble_evt->evt.gatts_evt.params.write.len;counter++)
                {
                    NRF_LOG_INFO("Get Server BLE_GATTS_EVT_WRITE Data.");
                    CUSTOM_DEVICE_NAME[counter] = p_ble_evt->evt.gatts_evt.params.write.data[counter];
                }
                err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                                     BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                //store data to flash
                name_store(CUSTOM_DEVICE_NAME);
                NRF_LOG_WARNING("System reset");
                NVIC_SystemReset();
            }
            break;
        default:
            // No implementation needed.
            break;
    }
}

void name_store(uint8_t *data)
{
    ret_code_t rc;
    uint8_t *pDataRx;
    uint32_t str_data;
    uint8_t i;

    NRF_LOG_INFO("BLE Name Store Start!");
    rc = nrf_fstorage_write(&fstorage, 0x3e100, &CUSTOM_DEVICE_NAME, 8, NULL);
    APP_ERROR_CHECK(rc);
    name_change_flag = 1;
    NRF_LOG_INFO("BLE Name Writing!");
    //wait write flash finish
    wait_for_flash_ready(&fstorage);//Error while loop
    NRF_LOG_INFO("BLE Name Store End!");
}

Related