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

Sending 32 bit of data over BLE onto nrf52832

Hi,

I have used "write_handler(p_ble_evt->evt.gap_evt.conn_handle, p_rtc_service, p_evt_write->data[0]);" to send data to nrf52832 from mobile but iam able to send  1 byte data as it is meant to be predefined in the gatts structure i.e

/**@brief Event structure for @ref BLE_GATTS_EVT_WRITE. */
typedef struct
{
uint16_t handle;                  /**< Attribute Handle. */
ble_uuid_t uuid;                  /**< Attribute UUID. */
uint8_t op;                       /**< Type of write operation, see @ref BLE_GATTS_OPS. */
uint8_t auth_required;            /**< Writing operation deferred due to authorization requirement. Application may use @ref sd_ble_gatts_value_set to finalize the writing operation. */
uint16_t offset;                   /**< Offset for the write operation. */
uint16_t len;                      /**< Length of the received data. */
uint8_t data[1];                   /**< Received data. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
uint32_t data1[1];
} ble_gatts_evt_write_t;

If i want to send 32 bit data can i try making change to uint8_t data[0] as uint32_t data[0]??If this is not the right way please let me know with what changes will i be able to send 32 bit data to nrf52832 from mobile?

Thank you.

  • Hi,

    Just no,

    Split your 32 bit data into an array of uint8_t(s) and and set that array equal to ble_gatts_evt_write_t ->data. ble_gatts_evt_write_t ->len should be set as the length of data sent in bytes.

    example code

    ble_gatts_evt_write_t params;
    
    uint32_t data = 0x12345678
    
    uint8_t buffer[sizeof(uint32_t)];
    
    
    void set_data(uint32_t *data, uint8_t *buffer,uint16_t len){
        memcpy(buffer,data,len);
    }
    
    
    void main(void){
        set_data(&data,buffer,sizeof(buffer));
        // buffer = {78,56,34,12}
        
        params.data = data;
        params.len = sizeof(uint32_t)
        
    }
    


    data[1] is a pointer to the start of an array of uint8_t(s).

    Example of sending more than one byte of data as a notification.

    uint32_t ble_packet_send(
    		ble_service_t * p_service, // service handle
    		packet_u * p_packet){ // data to send
    
    		uint32_t err_code = NRF_SUCCESS;
    
    		if (p_service->conn_handle != BLE_CONN_HANDLE_INVALID) {
    			ble_gatts_hvx_params_t hvx_params;
    			uint8_t encoded_value[sizeof(packet_u)]; // array to split data
    			uint16_t hvx_len;
    
    			// Initialize value struct.
    			memset(&hvx_params, 0, sizeof(hvx_params));
    
    			hvx_len = ble_encode(p_respons_packet, encoded_value); // lenght in bytes 
    			hvx_params.handle =p_service->ble_handles.value_handle;
    			hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
    			hvx_params.p_len = &hvx_len;
    			hvx_params.offset = 0;
    			hvx_params.p_data = encoded_value; //split data to send
    
    			err_code = sd_ble_gatts_hvx(p_service->conn_handle,&hvx_params);
    		} else {
    			err_code = NRF_ERROR_INVALID_STATE;
    		}
    
    		return err_code;
    }

    You should also set ble_add_char_params_t.max_len to the size of the data in bytes you intend to send in you service init.

    Regards

Related