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

Get connected device name in Generic Access Service from ble central

Hi all,

I would like to get the complete name from the connected peripheral by reading the device name characteristic (0x2A00) in the Generic Access Service (0x1800). I am using SDK 15.2.0 with soft device s132 v6.1.1.

I have tried to call sd_ble_gattc_char_value_by_uuid_read in BLE_GAP_EVT_CONNECTED event, and then get the device name through p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp.handle_value in BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP event. But it never runs inside the event BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP.  I have tested another way by using p_ble_evt->evt.gatts_evt.params.write.data but it didn't get the device name.

So I would like to know how I can do that.

Many thanks in advance.

Parents Reply Children
  • On your discovery event handler you do this:

    if ((p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE) &&
    	(p_evt->params.discovered_db.srv_uuid.uuid == gap_profile_s_uuid.uuid) &&
    	(p_evt->params.discovered_db.srv_uuid.type == gap_profile_s_uuid.type)) {
    
    	NRF_LOG_INFO("Found GAP profile service");
    
    	// Find the CCCD Handles of the device name characteristic
    	for (uint8_t i = 0; i < p_evt->params.discovered_db.char_count; i++) {
    		if ((p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid == device_name_c_uuid.uuid) &&
    			(p_evt->params.discovered_db.charateristics[i].characteristic.uuid.type == device_name_c_uuid.type)) {
    
    			NRF_LOG_INFO("Found device name characteristic");
    			device_name_handle = p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
    
    			// Break out of for loop
    			break;
    		}
    	}
    }

    Then you can use the device_name_handle in sd_ble_gattc_read(). In the offset just write 0. Where you call sd_ble_gattc_read() is up to you, and it doesn't really matter.

    To get the device name data, you need to do this in you BLE event handler:

    case BLE_GATTC_EVT_READ_RSP:;
    	ble_gattc_evt_read_rsp_t const * p_read_rsp = &p_ble_evt->evt.gattc_evt.params.read_rsp;
    
    	NRF_LOG_INFO("Read length: %d, Read offset: %d", p_read_rsp->len, p_read_rsp->offset);
    
    	// We are reading the device name
    	if (p_read_rsp->handle == device_name_handle) {
    		for (uint8_t i = 0; i < p_read_rsp->len; i++) {
    			device_name[i + p_read_rsp->offset] = p_read_rsp->data[i];
    		}
    		device_name_length = p_read_rsp->len + p_read_rsp->offset;
    	}
    	break;

  • Hi Andy,
    I have put the code in "db_disc_handler" and BLE_GATTC_EVT_READ_RSP event; sd_ble_gattc_read() is also called inside "db_disc_handler". But I see only the log line "Found GAP profile service". "Found device name characteristic" and "Read length: Read offset: " do not appear.

    static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
    {
        ble_nus_c_on_db_disc_evt(&m_ble_nus_c, p_evt);
    
        if ((p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE) &&
                (p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_NUS_SERVICE) &&
                (p_evt->params.discovered_db.srv_uuid.type == NUS_SERVICE_UUID_TYPE))
                {
                    NRF_LOG_INFO("Found GAP profile service");
                    // Find the CCCD Handles of the device name characteristic
                    for (uint8_t i = 0; i < p_evt->params.discovered_db.char_count; i++)
                    {
                        if ((p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid == BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME) &&
    			(p_evt->params.discovered_db.charateristics[i].characteristic.uuid.type == BLE_UUID_TYPE_BLE))
                        {
    			NRF_LOG_INFO("Found device name characteristic");
    			device_name_handle = p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
    			// Break out of for loop
    			break;
                        }
                    }
                }
    
         ret_code_t err_code;
         err_code = sd_ble_gattc_read(p_evt->conn_handle, device_name_handle, 0);
         if (err_code != NRF_ERROR_BUSY)
         {
            APP_ERROR_CHECK(err_code);
         }
    }
    case BLE_GATTC_EVT_READ_RSP:
            {
                uint8_t device_name[50];
                uint8_t device_name_length;
                ble_gattc_evt_read_rsp_t const * p_read_rsp = &p_ble_evt->evt.gattc_evt.params.read_rsp;
                NRF_LOG_INFO("Read length: %d, Read offset: %d", p_read_rsp->len, p_read_rsp->offset);
                // We are reading the device name
                if (p_read_rsp->handle == device_name_handle)
                {
    		for (uint8_t i = 0; i < p_read_rsp->len; i++)
                    {
    			device_name[i + p_read_rsp->offset] = p_read_rsp->data[i];
    		}
    		device_name_length = p_read_rsp->len + p_read_rsp->offset;
                }
    	} break;

  • The reason this won't work is because you are looking for the device name characteristic inside the NUS service. The device name characteristics lies inside the generic access service. I tried to check for p_evt->params.discovered_db.srv_uuid.uuid == 0x1800 but never got any hit, and I am not sure why.

    I tried to get sd_ble_gattc_char_value_by_uuid_read() to work, and have partly succeeded. I called it inside case BLE_GATTC_EVT_WRITE_RSP (Then the call to ble_nus_c_tx_notif_enable--> cccd_configure()-->sd_ble_gattc_write() has completed and I won't receive NRF_ERROR_BUSY) and I successfully received the event BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP. However, I haven't been able to get the data yet. But I think you have to use the function sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter().

    If you don't figure it out, please ask, and I will look into it.

    Best regards,

    Simon

  • Hi Simon,

    Thank you for your reply. I will try later and give feedback.
    I think I must discover all services, then discover service characteristics inside 1800 uuid, and finally read the characteristic value of 2A00 uuid.

Related