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

About the characteristic "read" of BLE communication

Hello

I am using Android and BLE communication with nRF52832 (BLENano2) now.

I am using SDK 15.0.0 with keil uversion.

Central is Android and peripheral is nRF52832.

Use the SDK "ble_app_blinky.

I now want to send arbitrary data from peripheral to central.

So I created the following function.

uint32_t send_value(uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t sensor_value)
{
	
	if (p_lbs == NULL)
    {
        return NRF_ERROR_NULL;
    }
		
	
	uint32_t err_code = NRF_SUCCESS;
	ble_gatts_value_t gatts_value;
	
	// Initialize value struct.
  memset(&gatts_value, 0, sizeof(gatts_value));

	gatts_value.len     = sizeof(uint8_t);
  gatts_value.offset  = 0;
  gatts_value.p_value = &sensor_value;

	// Update database.
  err_code = sd_ble_gatts_value_set(BLE_CONN_HANDLE_ALL,////////////
                                      p_lbs->sensor_char_handles.value_handle,
                                      &gatts_value);

	
		
		if (err_code != NRF_SUCCESS)
		{
				return err_code;
		}


	
	// Send value if connected and notifying.
    if ((conn_handle != BLE_CONN_HANDLE_INVALID)) 
    {
        ble_gatts_hvx_params_t hvx_params;

        memset(&hvx_params, 0, sizeof(hvx_params));

        hvx_params.handle = p_lbs->sensor_char_handles.value_handle;
        hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
        hvx_params.offset = gatts_value.offset;
        hvx_params.p_len  = &gatts_value.len;
        hvx_params.p_data = gatts_value.p_value;

        err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params);
    }
    else
    {
        err_code = NRF_ERROR_INVALID_STATE;
    }
		return err_code;
}

I want to process an event to call this function.

According to this page, when "ble_srv_is_notification_enabled" in on_write
evt.evt_type = BLE_CUS_EVT_NOTIFICATION_ENABLED
It is written to register the event.

I have a question here,
When sending data from peripheral to central, it is "read", right?
And when sending data from central to peripherals, it is "write", right?

I think this is "read" because data is sent from the peripheral to the central. Do you register an event in on_write?

Does that mean that if I want "read" but do not "write" I can not call "read"?
I'm sorry for asking questions like this because I'm a beginner.

If I use "ble_app_blinky", can I register the read event in on_write of "ble_conn_params.c"?

Thank you

Parents
  • There are two types of data transfers: transfers initiated by the central and transfers initiated by the peripheral.

    Reads and writes are both central-initiated transfers. This means that the central either wants to update a value in the peripheral, or it wants to get a value from the peripheral.

    There are two kinds of peripheral-initiated transfers: notifications and indications. These are used when the peripheral wants to let the central know that the value of a characteristic changed. At this point, just always use notifications. In my experience indications are quite useless (see this thread for more on this).

  • Thank you for reply!

    The relationship between central and peripheral seems to be understood.

    This time, because you want to get the value from the peripheral, it is "read", right?

    I want to send data from peripheral nRF52832 to Central Android.
    This is the process of "read", right?

    I want to know how to do this "read" process.
    It will be helpful if there is an example that can be used as it is.

  • There are two ways to send data from the peripheral to the central. Using notifications/indications or reads. If you want the phone to initiate the transfer, then you do a read. If you want the phone to always have the latest value, you "subscribe" to the characteristic and receive notifications every time the value changes.

    It's totally up to you how you want to do this.

  • Thank you for the very easy-to-understand explanation.

    I realized that I need to "subscribe" the value and receive notifications every second.

    Which SDKsample would be the best reference for running this?

Reply Children
Related