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

Reading the central device name (smart phone) in nRF52DK(Peripheral) and printing on the terminal

I am working on nRF52DK. This will act as peripheral device and smart phone device will act as central. 
From my observations i could see 48-bit mac address and GATT local db components are getting stored in the flash during pairing sequence.
It doesn't store the central device name.
Please let me know how to read the central device name during the connection.

Parents Reply
  • Tried to get the device name using  sd_ble_gatts_attr_get but it doent work.

    static void on_ble_evt(ble_evt_t * p_ble_evt)
    {
      case BLE_GAP_EVT_CONNECTED:
      m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
      ble_uuid_t temp;
      temp.type = BLE_UUID_TYPE_BLE;
      temp.uuid = BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME;
      err_code = sd_ble_gatts_attr_get(m_conn_handle, &temp, NULL);
      APP_ERROR_CHECK(err_code); // Fails here

Children
  • Hi, 

    sd_ble_gatts_attr_get will only return information about local attributes. You have to use GATT client read procedure to get the device name from your connected phone, see GATTC Characteristic or Descriptor Value Read. But before you attempt to a GATT read you need to do a service discovery to find the attribute handles of the characteristics you want to access. You can use the database discovery module for this.

    Below is a code snippet showing how you can configure the discovery module to search for the device name characteristic. Please refer to the GAP central examples in the SDK if you run into problems with adding the module to your project.   

    /**@brief Function for handling database discovery events.
     *
     * @details This function is a callback function to handle events from the database discovery module.
     *          Depending on the UUIDs that are discovered, this function forwards the events
     *          to their respective services.
     *
     * @param[in] p_event  Pointer to the database discovery event.
     */
    static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
    {
        
        uint32_t err_code; 
        uint16_t att_handle;
    
        ble_gatt_db_char_t * p_chars = p_evt->params.discovered_db.charateristics;
        
        if (    (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE)
            &&  (p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_GAP)
            &&  (p_evt->params.discovered_db.srv_uuid.type == BLE_UUID_TYPE_BLE))
        {
            for (uint32_t i = 0; i < p_evt->params.discovered_db.char_count; i++)
            {
                  if (p_chars[i].characteristic.uuid.uuid == BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME)
                  {
                      att_handle = p_chars[i].characteristic.handle_value;
                      /*Found Device name characteristic - Start read procedure*/
                      err_code = sd_ble_gattc_read(p_evt->conn_handle, att_handle, 0);
                      APP_ERROR_CHECK(err_code);
                  }
            }
        }
        
    }
    
    
    /** @brief Function for initializing the database discovery module. */
    static void db_discovery_init(void)
    {
        static ble_uuid_t ble_uuid;
    
        ble_uuid.type = BLE_UUID_TYPE_BLE;
        ble_uuid.uuid = BLE_UUID_GAP;
        
        ret_code_t err_code = ble_db_discovery_init(db_disc_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = ble_db_discovery_evt_register(&ble_uuid);
        APP_ERROR_CHECK(err_code);
    }

  • Thank you Vidar for the detailed explanation. Now I am am able to read the device name. 

Related