Hello everyone,
is there an example that demonstrates how to turn off and on the Bluetooth completely multiple times in the application?
I need really low consumption (tens of microamperes) and the only way I can do that is to invoke the sd_softdevice_disable() API. However, at a later time I cannot init the Bluetooth correctly.
Here is my code (based on the ble_peripheral_uart example from nRF5_SDK_17.0.2_d674dde, s132, nRF52832-CIAA(build code: E1) 512kB Flash, 64kB RAM):
/**@brief Function for the SoftDevice initialization.
*
* @details This function initializes the SoftDevice and the BLE event interrupt.
*/
static void ble_stack_init(uint32_t enable_request)
{
ret_code_t err_code;
if(enable_request){
err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);
}
// Configure the BLE stack using the default settings.
// Fetch the start address of the application RAM.
uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err_code);
// Enable BLE stack.
err_code = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err_code);
// Register a handler for BLE events.
NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
}
void user_bluetooth_init(void){
ret_code_t err;
const nrf_clock_lf_cfg_t sd_lf_config = {
.source = NRF_SDH_CLOCK_LF_SRC,
.rc_ctiv = NRF_SDH_CLOCK_LF_RC_CTIV,
.rc_temp_ctiv = NRF_SDH_CLOCK_LF_RC_TEMP_CTIV,
.accuracy = NRF_SDH_CLOCK_LF_ACCURACY
};
static uint32_t first_time = 1;
if(!ble_enabled){
ble_enabled = 1;
central_connected = 0;
if(first_time){
timers_init();
ble_stack_init(first_time);
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
first_time = 0;
}
else{
err = sd_softdevice_enable(&sd_lf_config, fault_handler);
APP_ERROR_CHECK(err);
// Configure the BLE stack using the default settings.
// Fetch the start address of the application RAM.
uint32_t ram_start = 0;
err = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err);
// Enable BLE stack.
err = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err);
}
user_bluetooth_advertising_start();
}
}
void user_bluetooth_deinit(void){
if(ble_enabled){
ble_enabled = 0;
sd_softdevice_disable();
nrf_delay_ms(1000);
}
}
int main(void){
DEBUGOUT("on\n");
user_bluetooth_init();
nrf_delay_ms(5000);
DEBUGOUT("off\n");
user_bluetooth_deinit();
nrf_delay_ms(5000);
DEBUGOUT("on\n");
user_bluetooth_init();
nrf_delay_ms(5000);
while(1){ }
}
If I try to connect with nRF Toolbox on the second turn on (I can see that the device is advertising correctly), the nRF Toolbox displays an error: "The device does not have required services."
Thank you in advance for your time!
Regards,
L. B.