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

How do I get my DK to Read character values

Hi, i am having some trouble getting my nRF52 DK (S132) to read a value provided by the nRF51 dongle(S130). First of all, i am very new to this whole Bluetooth thing so i've done all the tutorials, tried some things and had fun - so far. But now im stuck: I used the mulitlink_peripheral example on the dongle, set up the characteristics and values which i can read from the smartphone MCP. So all UUIDs and values und stuff are known and appear in MCP. This is where i figured out, i have to make changes to costumize the service i copied from multilink peripheral

static void services_init(void)   {
uint32_t            err_code;
ble_uuid_t          uuid;
ble_gatts_char_md_t char_md;
ble_gatts_attr_t    attr;
ble_gatts_attr_md_t attr_md;
ble_gatts_attr_md_t cccd_md;
ble_gatts_attr_md_t char_ud_md;
uint16_t            svc_test;


ble_uuid128_t base_uuid = MULTILINK_PERIPHERAL_BASE_UUID;

err_code = sd_ble_uuid_vs_add(&base_uuid, &m_base_uuid_type);
APP_ERROR_CHECK(err_code);

uuid.type = m_base_uuid_type;
uuid.uuid = MULTILINK_PERIPHERAL_SERVICE_UUID;

err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &uuid, &svc_test);  /**< Service will be added */
APP_ERROR_CHECK(err_code);

//////////

static uint8_t multilink_peripheral_data = 101;	/**< Actual Value of Characteristic, range 0-255, changing to uint16_t seems to stop functionality */
static uint8_t multilink_peripheral_ud[] = "Keycode Hex 1";			/**< Descriptor of Characteristic Value */

uuid.uuid = MULTILINK_PERIPHERAL_CHAR1_UUID;								/**< Set Characteristic UUID from #define */

memset(&attr, 0, sizeof(ble_gatts_attr_t));									/**< write 0 to ble_gatts_attr_t attr to make sure attr is not on random values */
attr.p_uuid    = &uuid;
attr.p_attr_md = &attr_md;
attr.max_len   = 1;
attr.p_value   = &multilink_peripheral_data;
attr.init_len  = sizeof(multilink_peripheral_data);

memset(&attr_md, 0, sizeof(ble_gatts_attr_md_t));
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.write_perm);
attr_md.vloc = BLE_GATTS_VLOC_STACK;
attr_md.vlen = 0;

memset(&cccd_md, 0, sizeof(ble_gatts_attr_md_t));
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&cccd_md.write_perm);
cccd_md.vloc = BLE_GATTS_VLOC_STACK;

memset(&char_md, 0, sizeof(ble_gatts_char_md_t));
char_md.p_cccd_md               = &cccd_md;
char_md.char_props.notify       = 1;									/**< comment / uncomment to set permissions */
char_md.char_props.indicate     = 1;
char_md.char_props.read         = 1;
char_md.char_props.write        = 1;
char_md.char_ext_props.wr_aux   = 1;										/**< needs to stay 1 */
char_md.p_user_desc_md          = &char_ud_md;
char_md.p_char_user_desc        = multilink_peripheral_ud;
char_md.char_user_desc_size     = (uint8_t)strlen((char *)multilink_peripheral_ud);
char_md.char_user_desc_max_size = (uint8_t)strlen((char *)multilink_peripheral_ud);

memset(&char_ud_md, 0, sizeof(ble_gatts_attr_md_t));
char_ud_md.vloc = BLE_GATTS_VLOC_STACK;
char_ud_md.vlen = 1;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&char_ud_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&char_ud_md.write_perm);

err_code = sd_ble_gatts_characteristic_add(BLE_GATT_HANDLE_INVALID,
                                           &char_md,
                                           &attr,
                                           &m_char_handles);
APP_ERROR_CHECK(err_code);

next thing i want to do is to read the value stored in

static uint8_t multilink_peripheral_data = 101;

from my nrf52 DK which then lights up a LED when compared the 101 to another value stored on the nrf52 DK. In the infocenter I found this flowchart: GATTC Read Characteristic Value by UUID

but somehow i cannot figure out how to make this function work. e.g. i don't know where the conn_handle comes from. Infocenter just tells me: this is the connection handle, which i already figured out from the name...

do i have to call some other functions first, or set up some structs to make this work? thanks for the help so far. I'm trying to be precise, but as i am new to this, it's a bit difficult. And thanks for recommending the app_hrs and battery service, but since i set up my peripheral device and it appears in MCP as I wish, i'd rather like to stick with this code, than getting to know a new one which wasn't very familiar looking.

best regards selle

  • you had to change what type in what variable where? It's really not very clear what didn't compile, but if it didn't compile,it's probably a mistake and changing it is probably another mistake.

    BLE_UUID_CHARACTERISTIC is definitely wrong, look at the documentation for that structure, it takes a few things, none of them that. If it's a standard UUID it's BLE_UUID_TYPE_BLE else it's something returned by the function which registered the custom uuid. In your case it's most likely the latter as you're using a custom UUID.

    So call the function, with the correct parameters, and wait for the reply to come back, then process the reply. If the reply says nothing, you sent the wrong question.

Related