Hello
I use nrf52832 and sdk v17.0.
I'm using a battery and I can check whether the battery is charging or not. And I'm going to turn off the Bluetooth function while the battery is charging. So I'm going to stop the Bluetooth advertising with 'sd_ble_gap_adv_stop()' or use 'sd_ble_gap_disconnect()' to turn off Bluetooth.
However, when I attempt to invoke these two functions, the error 'NRF_ERROR_INVALID_STATE' is output. And all operations will be stopped.
I couldn't show all the codes, so I only put some related codes in. (Based on the example of ble_app_uart.)
/**@brief Function for starting advertising.
*/
static void advertising_start(bool erase_bonds)
{
if (erase_bonds == true)
{
delete_bonds();
// Advertising is started by PM_EVT_PEERS_DELETE_SUCCEEDED event.
}
else
{
ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
}
}
static void advertising_stop()
{
ret_code_t err_code;
err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle);
APP_ERROR_CHECK(err_code);
}
int main(void)
{
uint32_t err_code;
bool erase_bonds = false;
// Initialize.
gpio_init();
uart_init();
log_init();
timers_init();
app_buttons_init();
power_management_init();
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
peer_manager_init();
err_code = app_button_enable();
APP_ERROR_CHECK(err_code);
adc_configure(); //baterry value & LED
pwm_init();
mpu_init();
bsp_board_init(BSP_INIT_LEDS); //use bsp
nrf_gpio_pin_set(POWER_SW); //HIGH : stay power on
// Start execution.
printf("\r\nHelmet Device started.\r\n");
NRF_LOG_INFO("Debug logging for UART over RTT started.");
advertising_start(erase_bonds); //false
pwm_change_frequency(1500);
do_play_buzzer(); //When start play buzzer
// Enter main loop.
for (;;)
{
uint32_t err_code;
charge_state = nrf_gpio_pin_read(bat_state); //read CHG pin
if(charge_state == 0) //charging
{
if(m_conn_handle == BLE_CONN_HANDLE_INVALID) //in scanning
{
//printf("Not yet pairing!\n");
advertising_stop(); //not scanning bluetooth //error
}
else //in connect pairing
{
//m_conn_handle = BLE_CONN_HANDLE_INVALID; //Not working
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); //disconnect pairing //error
APP_ERROR_CHECK(err_code);
}
charging_state = 1;
PairLED_timers_stop();
nrf_gpio_pin_clear(Pairing_LED); //pairing led off
}
else //no charging
{
//doing
}
}
}
V 17:37:53.705 Connecting to FA:21:92:8E:86:C8... D 17:37:53.705 gatt = device.connectGatt(autoConnect = false, TRANSPORT_LE, preferred PHY = LE 1M) D 17:37:53.813 [Broadcast] Action received: android.bluetooth.device.action.ACL_CONNECTED D 17:37:53.840 [Callback] Connection state changed with status: 0 and new state: CONNECTED (2) I 17:37:53.840 Connected to FA:21:92:8E:86:C8 V 17:37:53.864 Discovering services... D 17:37:53.864 gatt.discoverServices() I 17:37:54.504 Connection parameters updated (interval: 7.5ms, latency: 0, timeout: 5000ms) D 17:37:59.538 [Callback] Connection state changed with status: 8 and new state: DISCONNECTED (0) E 17:37:59.539 Error 8 (0x8): GATT CONN TIMEOUT I 17:37:59.539 Disconnected D 17:37:59.597 [Broadcast] Action received: android.bluetooth.device.action.ACL_DISCONNECTED
Thank you.