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

Little problem with send data to BLE

Good afternoon.

I´m using nrf51-dk with SDK 9.0.0, s110 softdevice and uart nordic app on mobile. I have a program in my nrf51-dk that it sends and receives data between nrf51-dk and mobile device.

I want to send a data when mobile device establishes connection with my nrf51-dk, the problem is that the first time that mobile device establishes the connection doesn´t receive "Pulse c para calibra" but when I disconnect and connect again the mobile device with my nrf51-dk, it receives the data correctly.

I have modified "on_ble_evt" function.

static void on_ble_evt(ble_evt_t * p_ble_evt){
    uint32_t                         err_code;
	uint8_t instrucciones[20] =     "Pulse c para calibra";

switch (p_ble_evt->header.evt_id){
    case BLE_GAP_EVT_CONNECTED:
                                conectado = true;
                   				nrf_delay_ms(1500);
        						ble_nus_string_send(&m_nus, instrucciones, 20);
            					app_timer_start(timer_id,  APP_TIMER_TICKS(500, APP_TIMER_PRESCALER), NULL);   //10 segundos para configurar
                                err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
                                APP_ERROR_CHECK(err_code);
                                m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
                                break;
        
    case BLE_GAP_EVT_DISCONNECTED:
					            conectado = false;
         						temporizador_menu = tiempo_espera_menu;
	            				limpiar_datos_recibidos_bluetooth();
        						app_timer_stop(timer_id);
                                err_code = bsp_indication_set(BSP_INDICATE_IDLE);
                                APP_ERROR_CHECK(err_code);
                                m_conn_handle = BLE_CONN_HANDLE_INVALID;
                                break;

    case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
                                // Pairing not supported
                                err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
                                APP_ERROR_CHECK(err_code);
                                break;

    case BLE_GATTS_EVT_SYS_ATTR_MISSING:
                                // No system attributes have been stored.
                                err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
                                APP_ERROR_CHECK(err_code);
                                break;

    default:
        // No implementation needed.
        break;
}
}

Screen of data received by mobile device:

image description

Thank you.

Parents
  • You should not send data on the connect event, (unless you are already bonded and HVX is enabled) as the peer hasn't had the chance to subscribe to your indications/notifications by writing to the CCCD. Instead you should wait for HVX to be enabled. Look at e.g. the HRS example. There you can see that HRs has it's own handler that calls on_write when a ble_gatts_evt_write is received. In on write there is a check to see if the cccd was written to, and if it is it's starts to send notifications. If you try to send a notification or indication when it's not enabled sd_ble_gatts_hvx will return NRF_ERROR_INVALID_STATE.

Reply
  • You should not send data on the connect event, (unless you are already bonded and HVX is enabled) as the peer hasn't had the chance to subscribe to your indications/notifications by writing to the CCCD. Instead you should wait for HVX to be enabled. Look at e.g. the HRS example. There you can see that HRs has it's own handler that calls on_write when a ble_gatts_evt_write is received. In on write there is a check to see if the cccd was written to, and if it is it's starts to send notifications. If you try to send a notification or indication when it's not enabled sd_ble_gatts_hvx will return NRF_ERROR_INVALID_STATE.

Children
Related