Hello,
i asked this quastion once in this thread: https://devzone.nordicsemi.com/f/nordic-q-a/32444/reset-on_ble_connect-when-button-event
I accepted the given answer but later i found out that the solution does not work. I rejected the answer, but i am afraid that nobody can see it anymore.
This is not intended as spamming and i apologize for the duplicate. I sum up the results of thread:
My problem is reproducable with this setup:
- nRF5-DK with ble_blinky_example (central)
- nRF5-DK with ble_blinky_example (peripheral)
- Press Button1 immediately on the peripheral after connecting (LED2 lights up).
- watch how the peripheral goes back to Advertising (resets)
- Press Button1 after the Connection is established a while, everything works fine.
- SDK 12.3 and Softdevice S130 2.0.1/ Softdevice S132 3.0.0
The problem is that pressing a button on the peripheral after connection setup causes a reset. It is shown in the video attached:
- Peripheral left, central right.
- First: in connection, everything fine. (Button press is indicated with a led).
- Second: Switch off and on the peripheral (same with resetting via IF BOOT/RESET). Press Button1 directly after connecting (LED2 lights up). Board goes back to advertising/resets.
- Third: Wait after connection. Everything works fine. (Button press is indicated with a led)
If the button_event_handler() is empty, the devices stay connected. APP_ERROR_CHECK(err_code) causes the reset. NRF_LOG_INFO shows err_code = 13313 --> ERROR: fatal. If i comment the APP_ERROR_CHECK out, the connection stays (at least the led is still on). The first button presses are not detected, but the later ones.
In my application, i want to show that the device is in a connection so that the customer can start interacting. Therefore, i want to be sure that there is a valid connection when a led lights up.
Tried Solutions:
I tried to check if there is a valid connection-handle but that does not help. I dont know how to check for the other err_codes (13313).
static void button_event_handler(uint8_t pin_no, uint8_t button_action) { uint32_t err_code; if(m_conn_handle != BLE_CONN_HANDLE_INVALID) { switch (pin_no) { case LEDBUTTON_BUTTON_PIN: NRF_LOG_INFO("Send button state change.\r\n"); err_code = ble_lbs_on_button_change(&m_lbs, button_action); NRF_LOG_INFO("Error Code: %d \r\n", err_code); if (err_code != NRF_SUCCESS && err_code != BLE_ERROR_INVALID_CONN_HANDLE && err_code != NRF_ERROR_INVALID_STATE ) { APP_ERROR_CHECK(err_code); } break; default: APP_ERROR_HANDLER(pin_no); break; } } }
I used a timer to delay the indication of the connection state. But that is kind of sloppy.
In on_ble_evt() a timer is started:
case BLE_GAP_EVT_CONNECTED: NRF_LOG_INFO("Connected\r\n"); err_code = app_timer_stop(m_adv_con_led_timer_id); APP_ERROR_CHECK(err_code); //bsp_board_led_on(CONNECTED_LED_PIN); m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; err_code = app_timer_start(m_off_timer_id, APP_TIMER_TICKS(400, APP_TIMER_PRESCALER), NULL); APP_ERROR_CHECK(err_code); break; // BLE_GAP_EVT_CONNECTED
And in the timer_handler, the state is indicated and the app_button module enabled:
uint8_t err_code = app_button_enable(); APP_ERROR_CHECK(err_code); bsp_board_led_on(CONNECTED_LED_PIN);
Thank you very much in advance and kind regards
DanKa