This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Sending data

Hi all,

I have a small question. If a GATT server needs to send data to a GATT client(about 65 bytes), does it have to create a service first and then adds characteristic, or it can use directly sd_gap_gatts_hvx function()?

Regards,

mohBOSS.

  • Hi there,

    You will need to add a service and a characteristic to send arbitrary data. Once you have the handle to the characteristic you can use sd_ble_gatts_hvx() to send data. Alternatively, if it's easier for you, you can add the service and characteristic on the peer device and use sd_ble_gattc_write() to send data, acting as a GATT client.

    Carles

  • Hi Carles,

    In fact I need to be a server, in order to send data to the client. I can not understand yet how to make a service that can be used by the client to read data. So far I have adapted this code for my needs.

    static uint32_t characteristic_add(ble_lbs_t * p_lbs) { ble_gatts_char_md_t char_md; ble_gatts_attr_t attr_char_value; ble_uuid_t ble_uuid; ble_gatts_attr_md_t attr_md;

    memset(&char_md, 0, sizeof(char_md));
    
    char_md.char_props.read   = 1;
    char_md.char_props.write  = 1;
    char_md.char_ext_props.reliable_wr = 1;
    char_md.p_char_user_desc  = NULL;
    char_md.p_char_pf         = NULL;
    char_md.p_user_desc_md    = NULL;
    char_md.p_cccd_md         = NULL;
    char_md.p_sccd_md         = NULL;
    
    ble_uuid.type = p_lbs->uuid_type;
    ble_uuid.uuid = LBS_UUID_LED_CHAR;
    
    memset(&attr_md, 0, sizeof(attr_md));
    
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
    
    attr_md.vloc       = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth    = 0;
    attr_md.wr_auth    = 0;
    attr_md.vlen       = 1;
    
    memset(&attr_char_value, 0, sizeof(attr_char_value));
    
    attr_char_value.p_uuid       = &ble_uuid;
    attr_char_value.p_attr_md    = &attr_md;
    attr_char_value.init_len     = sizeof(uint8_t);
    attr_char_value.init_offs    = 0;
    attr_char_value.max_len      = 30;
    attr_char_value.p_value      = NULL;
    
    return sd_ble_gatts_characteristic_add(p_lbs->service_handle, &char_md,
                                               &attr_char_value,
                                               &p_lbs->led_char_handles);
    

    }

    But I can't replace the ble_lbs_t type with what I need.

    I hope You are understanding me Dear Carles.

  • Hi there,

    Not sure about ble_lbs_t, it's not something that's part of the SoftDevice. What I would recommend for simple data transmission as a service is to take a look at:

    Service: components/ble/ble_services/ble_nus

    Example using the service: examples/ble_peripheral/ble_app_uart

    The function call to: ble_nus_string_send()

    is what you need. It sends a notification with arbitrary data.

    Carles

  • Hi, please I have some questions.

    1. When adding a service then a characteristic, this enables the SoftDevice to send BLE events to the application whenever a client wants to use the characteristic. Is that right ?
    2. If a client wants to read data (without authorization) from the server, then how can the server use sd_ble_gatts_hvx() if there is no event generated for a Read Request without Authorization to notify the application?

    I hope that you understand me. Thanks a lot.

    mohBOSS.

  • Hi there,

    1. When you add a service and then a characteristic, you are just populating your own attribute table, and adding a placeholder to then be able to store data (inside the characteristic) that can then be filled in by your application, read by the peer client and updated (via hvx()) by you.
    2. I think you are confusing terms here. The client can read your characteristic value at any time and it will get the current value that is in memory. You can change that value locally by calling sd_ble_gatts_value_set(). Independently of that you can notify (i.e. push an update) the client whenever you want by calling sd_ble_gatts_hvx().

    Carles

Related