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

Call Disconnect Function In NUS

Hello,

I'm using nRF52 SDK12.2 "ble_app_uart_c" project as a "Central", and the target device is the "Peripheral."

Test Steps:

  1. Using putty sending message(with wrong header) to the ""Central"
  2. Central will detect the wrong header and disconnect to the target device and also showing "Disconnect" message on putty.
  3. Using putty send message to Central again

-----> BUT the Central can NOT get any message from UART until reset it.

The question is how to disconnect to the target inside the "BLE_NUS_C_EVT_NUS_RX_EVT"???

Also attached some codes as below:

static void ble_nus_c_evt_handler(ble_nus_c_t * p_ble_nus_c, const ble_nus_c_evt_t * p_ble_nus_evt)
{
    uint32_t err_code;

    switch (p_ble_nus_evt-

>evt_type)
    {
        case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
            err_code = ble_nus_c_handles_assign(p_ble_nus_c, p_ble_nus_evt->conn_handle, &p_ble_nus_evt->handles);
            APP_ERROR_CHECK(err_code);
            
		err_code = ble_nus_c_rx_notif_enable(p_ble_nus_c);
            APP_ERROR_CHECK(err_code);
				
		//printf("The device has the NUS\r\n");
				
            break;
							
        case BLE_NUS_C_EVT_NUS_RX_EVT:
					
		if(	p_ble_nus_evt->p_data[0] != CMD_STATUS_HEADER)
		{

                    // disconnect to the target device
			sd_ble_gap_disconnect(m_pending_db_disc_conn, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
		}


            break;

        case BLE_NUS_C_EVT_DISCONNECTED:
            break;
    }
}



static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t              err_code;
    const ble_gap_evt_t * p_gap_evt = &p_ble_evt->evt.gap_evt;

    switch (p_ble_evt->header.evt_id)
    {
        ....
         switch (p_ble_evt->header.evt_id)
        {
             case BLE_GAP_EVT_DISCONNECTED:
             {
                    printf("Disconnected \r\n");
                    .........
             }
        }
    }
}
Related