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

How to receive array data sent from custom characteristic to application on Android Studio

Hello everyone,

I got a problem with receiving an array data on Android application sent from a custom characteristic.

Below is the code for sending an array data via BLE.

uint32_t ble_cus_custom_value_update(ble_cus_t * p_cus, uint8_t* custom_value, uint16_t length)
{
    NRF_LOG_INFO("In ble_cus_custom_value_update. \r\n"); 
    if (p_cus == 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 = custom_value;

    // Update database. // Need to understand about it
		// Set a new value of the custom_value which will be sent to client.
    err_code = sd_ble_gatts_value_set(p_cus->conn_handle,
                                      p_cus->custom_value_handles.value_handle,
                                      &gatts_value);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

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

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

        hvx_params.handle = p_cus->custom_value_handles.value_handle;
        hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
        hvx_params.offset = gatts_value.offset; // only used if it is larger than 20 bytes
        hvx_params.p_len  = &gatts_value.len;
        hvx_params.p_data = gatts_value.p_value;

        err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params); // Notify server
        NRF_LOG_INFO("sd_ble_gatts_hvx result: %x. \r\n", err_code); 
			for (uint8_t i = 0; i < 10; i++)
			{
				NRF_LOG_INFO("Custom value result: %d. \r\n", custom_value[i]);
			}
				 
    }
    else
    {
        err_code = NRF_ERROR_INVALID_STATE;
        NRF_LOG_INFO("sd_ble_gatts_hvx result: NRF_ERROR_INVALID_STATE. \r\n"); 
    }
    return err_code;
	}

I used a timer to call this function. The RTT log showed my array data supposed to send to the Android app. However, what I received in the Android app which I modified from Blink app is the first value of the array. I used nRF Connect app to log the data, it also got the first value.

  

For the Android application, the image below describes the way I get the data. I anticipated that either the update value function above or the way to get value in the Android app was wrong.

Could you please help me to check it?

Thanks!

  • Well, I fixed that problem!

    The issue stemmed from the size of characteristic. I changed from 1 byte to 10 bytes (image blow), and it worked. Hope it is helpful for someone struggling with this tho.

    Moreover, instead of using the function data.getIntValue() in the Android application, I used data.getValue() which returns a byte array that is array data I supposed to receive from BLE server.

    Last modification is to remove the portion of code which I supposed to use to update the value whenever some changes in my characteristic happen.

    I'm still confused about the function of sd_ble_gatts_value_set(). How should it be used properly? Dose that act like whenever updating value from client (our phone..) to server, this function will be called?

    Thanks,

  • Hi

    Glad you were able to figure out your initial problem. 

    The sd_ble_gatts_value_set function() takes in a ble_gatts_value_t pointer which has a pointer p_value to a uint8_t array. You can, therefore, typecast your struct to an uint8_t array and send it with the sd_ble_gatts_value_set() function. The attribute value offset parameter can be used to specify which parts of the attribute that you want to change. This can be useful if you only want to change a certain part of the characteristic. 

    Documentation can be found here

    Best regards,

    Simon

Related