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

When to call sd_ble_gatts_hvx

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 added a characteristic based on "ble_app_blinky".
The following program was created for that characteristic.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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,////////////
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

By calling this "send_value" function
It turned out that a value is set by "sd_ble_gatts_value_set" and a value is notified by "sd_ble_gatts_hvx".

There is something I do not understand here, but I want to know when to call this function.

I found out that I have to be called when notification is enabled.

How should I program when notification is valid?

Please give me an answer

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

    You can do that using the Nordic UART Service (NUS) - have you tried that, before trying to create your own Service & Characteristics from the ground up?

    https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.3.0/ble_sdk_app_nus_eval.html?cp=5_0_4_2_2_24

    If you do want to create your own Service & Characteristics from the ground up, study these tutorials:

    https://devzone.nordicsemi.com/tutorials/b/bluetooth-low-energy/posts/ble-services-a-beginners-tutorial

    https://devzone.nordicsemi.com/tutorials/b/bluetooth-low-energy/posts/ble-characteristics-a-beginners-tutorial

  • Thank you for reply!

    The sensor connected to the nRF52832 is I2C communication. Therefore, UART communication can not be performed.

    Therefore, it creates its own characteristics so that the values received by I2C communication can be sent to Central by BLE.

  • I am also wondering.
    In ble_app_uart example,
    1) Set ble_nus_data_send () as timeout_handler
    2) timer_create in timer_init ()
    3) timer_start in main ()
    I just did it.
    Is there anything else you should do or do wrong?
    Attach the code of main.c.

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    /**
    * Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
    *
    * All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without modification,
    * are permitted provided that the following conditions are met:
    *
    * 1. Redistributions of source code must retain the above copyright notice, this
    * list of conditions and the following disclaimer.
    *
    * 2. Redistributions in binary form, except as embedded into a Nordic
    * Semiconductor ASA integrated circuit in a product or a software update for
    * such product, must reproduce the above copyright notice, this list of
    * conditions and the following disclaimer in the documentation and/or other
    * materials provided with the distribution.
    *
    * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
    * contributors may be used to endorse or promote products derived from this
    * software without specific prior written permission.
    *
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • It’s hard for me to see anything by looking at the entire code. Perhaps you can put some debug output after you call app_timer_start()? Are you sure that everything is still running after that?

  • With "BLENano2 (nRF52832)" that I use, I can not see the log by debugging because there is no J-Link.

    Previously, when operating the BLENano 2, it was advertised while connected to a PC, but it was possible to scan when the breadboard was powered.
    However, the connection is disconnected immediately after connecting.
    If you try to connect again later, you will be disconnected in less than a second.

    Since the debugging method can only turn on the LED before and after the code, I tried that method. (Nrf_gpio_pin_set (5);)
    The LED was lit normally before app_timer_start.
    If I tried to turn on the LED immediately after app_timer_start, the LED blinked even though I didn't write the stop code.
    From these things, it was found that app_timer_start has disconnected the BLE connection.
    But I do not know how to improve it.

    Is it caused by my BLENano2?
    Or is it caused by the program code?

  • Difficult to say honestly, I can only say that you should figure out a better way to debug your code, otherwise you won't get far.

  • you should figure out a better way to debug your code, otherwise you won't get far.

    Absolutely!

    Trying to do this stuff without a debugger is folly, IMO!

    get yourself an nRF52 DK - it has an onboard JLink which can be used to debug external targets.

Reply
  • you should figure out a better way to debug your code, otherwise you won't get far.

    Absolutely!

    Trying to do this stuff without a debugger is folly, IMO!

    get yourself an nRF52 DK - it has an onboard JLink which can be used to debug external targets.

Children
  • Consider purchasing the nRF52DK to debug. . .

    Originally, using the example of "ble_app_uart",
    1) Set ble_nus_data_send to timeout_handler.
    2) Create a timer
    3) Call app_timer_start immediately after advertising_start ().
    Can this procedure send data to Central?

  • It should, yes. But if it doesn't work, without a proper way to debug your program it is very hard for us to help you.

  • I see
    I will consider a little more.

  • Are these arguments correct when calling ble_nus_data_send?

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    static void notification_timeout_handler(void * p_context)
    {
    UNUSED_PARAMETER(p_context);
    ret_code_t err_code;
    data[0] = 1;
    err_code = ble_nus_data_send(&m_nus, data, &length, m_conn_handle);
    APP_ERROR_CHECK(err_code);
    }
    /**@brief Function for initializing the timer module.
    */
    static void timers_init(void)
    {
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);
    err_code = app_timer_create(&m_notification_timer_id, APP_TIMER_MODE_REPEATED, notification_timeout_handler);
    APP_ERROR_CHECK(err_code);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    There is no doubt that "m_nus" is defined for calling a structure, right?

  • I don't know where you're getting the length from, but at least the parameters look alright.

    Did you figure out your issue with the scanning?