<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Central ble, how to relate UUID with handlers?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/79896/central-ble-how-to-relate-uuid-with-handlers</link><description>Hello, 
 I am developing a device with a nrf52 using s140 with the last sdk. The objective is to interact through ble with other devices that have different custom services. I have already implemented the central role, scan the devices and connect to</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 23 Sep 2021 10:10:18 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/79896/central-ble-how-to-relate-uuid-with-handlers" /><item><title>RE: Central ble, how to relate UUID with handlers?</title><link>https://devzone.nordicsemi.com/thread/330792?ContentTypeID=1</link><pubDate>Thu, 23 Sep 2021 10:10:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8c1921aa-3247-42bf-9b03-a145d1900472</guid><dc:creator>Karl Ylvisaker</dc:creator><description>[quote user="Angel"]Thanks for your fast answer.[/quote]
&lt;p&gt;No problem at all, I am happy to help! :)&amp;nbsp;&lt;/p&gt;
[quote user="Angel"]As far I understand with the documentation and using the UART example, I should be able to do something like this:[/quote]
&lt;p&gt;No, I think there might be a couple of possible things going wrong here.&lt;br /&gt;For example, you&amp;#39;re registering the &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.s140.api.v7.2.0%2Fgroup___b_l_e___u_u_i_d___t_y_p_e_s.html"&gt;UUID&amp;#39;s for service discovery and base UUID with the type UNKNOWN&lt;/a&gt;, since its 0. Do you get any runtime errors?&lt;br /&gt;From the&amp;nbsp;services list I assume that you are going to accept notifications from some of these services, is this correct?&lt;br /&gt;I recommend that you create a separate source file for the services you will be accessing, akin to ble_lbs_c.c, ble_hrs_c.c, ble_bas_c.c, etc. You could take any one of the existing services source and header files to use as a template for this. This is best practice, rather than having it all be part of your main.c&lt;br /&gt;&lt;br /&gt;You might also want to make use of &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/lib_ble_gatt_queue.html"&gt;the GATT Queue library&lt;/a&gt;&amp;nbsp;when working with multiple services and characteristics. The use of the GATT Queue library is also demonstrated in the BLE Central examples.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Central ble, how to relate UUID with handlers?</title><link>https://devzone.nordicsemi.com/thread/330706?ContentTypeID=1</link><pubDate>Wed, 22 Sep 2021 14:13:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9e6bf893-0d9a-4b28-ae62-3b712633d736</guid><dc:creator>Angel</dc:creator><description>&lt;p&gt;Thanks for your fast answer.&lt;/p&gt;
&lt;p&gt;As far I understand with the documentation and using the UART example, I should be able to do something like this:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// Custom Service
#define BLE_UUID_BASE_UUID {{0x91, 0x6B, 0xB6, 0xB9, 0x58, 0x9E, 0x75, 0x9B, \
                             0xBF, 0x4D, 0x09, 0x8D, 0x00, 0xE0, 0x5A, 0xFA}} // 128-bit base UUID

#define BLE_UUID_SERVICE_UUID                0xE000  

// Defining 16-bit characteristic UUID
#define BLE_UUID_COMMAND_CHARACTERISTC_UUID  0xE002 
#define BLE_UUID_STATUS_CHARACTERISTC_UUID   0xE004
#define BLE_UUID_DATA_CHARACTERISTC_UUID     0xE006 
#define BLE_UUID_FOTA_CHARACTERISTC_UUID     0xE008 
// Handler to perform operations over characteristics
typedef struct 
{
  uint16_t cmdHandler;    // BLE_UUID_COMMAND_CHARACTERISTC_UUID     
  uint16_t statusHandler; // BLE_UUID_STATUS_CHARACTERISTC_UUID     
  uint16_t dataHandler;   // BLE_UUID_DATA_CHARACTERISTC_UUID     
  uint16_t fotaHandler;   // BLE_UUID_FOTA_CHARACTERISTC_UUID     
}tBLECharacteristicsHandler;
static tBLECharacteristicsHandler charHnd;
static uint16_t connHandler;

//******************************************************************************
tRetCode BLE_ReadCharacteristicStatus(void)
//******************************************************************************
// Description: 
// Parameters: None 
// Returns: error check 
//******************************************************************************
{
  uint32_t err = 0;
  err = sd_ble_gattc_read(connHandler, charHnd.statusHandler, 0);
  if (err  != NRF_SUCCESS)
  { return RES_ERROR;}
  return RES_OK;
}

//******************************************************************************
static void DataBaseDiscoveryHandler(ble_db_discovery_evt_t * pEvt)
//******************************************************************************
// Description: 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.
// Parameters: 
//  ble_db_discovery_evt_t * pEvt: Pointer to the database discovery event
// Returns: error check 
//******************************************************************************
{
  uint32_t i = 0;
  ble_gatt_db_char_t *pChars = pEvt-&amp;gt;params.discovered_db.charateristics;
  ble_db_discovery_t const *pDb;
  pDb = (ble_db_discovery_t *)pEvt-&amp;gt;params.p_db_instance;

  ble_hrs_on_db_disc_evt(BLE_HeartRateClientHandler(), pEvt);

  if (pEvt-&amp;gt;evt_type == BLE_DB_DISCOVERY_AVAILABLE) 
  {
    NRF_LOG_INFO(&amp;quot;DB Discovery instance %p available on conn handle: %d&amp;quot;, pDb,
                  pEvt-&amp;gt;conn_handle);
    NRF_LOG_INFO(&amp;quot;Found %d services on conn_handle: %d&amp;quot;, pDb-&amp;gt;srv_count,
                 pEvt-&amp;gt;conn_handle); 
  }

  if (pEvt-&amp;gt;evt_type != BLE_DB_DISCOVERY_COMPLETE) return;
  connHandler = pEvt-&amp;gt;conn_handle;
  for (i = 0; i &amp;lt; pEvt-&amp;gt;params.discovered_db.char_count; i++)
  {
    switch (pChars[i].characteristic.uuid.uuid)
    {
    case BLE_UUID_COMMAND_CHARACTERISTC_UUID:
      charHnd.cmdHandler = pChars[i].characteristic.handle_value;
      break;
    case BLE_UUID_STATUS_CHARACTERISTC_UUID:
      charHnd.statusHandler = pChars[i].characteristic.handle_value;
      break;
    case BLE_UUID_DATA_CHARACTERISTC_UUID:
      charHnd.dataHandler = pChars[i].characteristic.handle_value;
      break;    
    case BLE_UUID_FOTA_CHARACTERISTC_UUID:
      charHnd.fotaHandler = pChars[i].characteristic.handle_value;
      break;    
    default:
      break;
    }
  }
}

//******************************************************************************
static tRetCode BLE_RegisterUUID(uint16_t uuid)
//******************************************************************************
// Description: This function register a UUId to the discovery database
// Parameters: uint16_t uuid: UUID to add 
// Returns: error check 
//******************************************************************************
{
  ble_uuid_t charUuid;
  uint8_t uuidType = 0;
  ble_uuid128_t baseUuid = BLE_UUID_BASE_UUID;

  if (sd_ble_uuid_vs_add(&amp;amp;baseUuid, &amp;amp;uuidType) != NRF_SUCCESS)
  { return RES_ERROR;}
  charUuid.uuid = uuid;
  charUuid.type = uuidType;
  if (ble_db_discovery_evt_register(&amp;amp;charUuid) != NRF_SUCCESS)
  { return RES_ERROR;}
  return RES_OK;
}

//******************************************************************************
tRetCode BLE_DataBaseDiscoveryInit(void)
//******************************************************************************
// Description: Database discovery initialization.
// Parameters: None 
// Returns: error check 
//******************************************************************************
{
  ble_db_discovery_init_t dbInit;

  memset(&amp;amp;dbInit, 0, sizeof(dbInit));

  dbInit.evt_handler  = DataBaseDiscoveryHandler;
  dbInit.p_gatt_queue = &amp;amp;gattQueue;

  if (ble_db_discovery_init(&amp;amp;dbInit) != NRF_SUCCESS) return RES_ERROR;

  if (BLE_RegisterUUID(BLE_UUID_COMMAND_CHARACTERISTC_UUID) != RES_OK)
  { return RES_ERROR;}
  if (BLE_RegisterUUID(BLE_UUID_STATUS_CHARACTERISTC_UUID) != RES_OK)
  { return RES_ERROR;}
  if (BLE_RegisterUUID(BLE_UUID_DATA_CHARACTERISTC_UUID) != RES_OK)
  { return RES_ERROR;}
  if (BLE_RegisterUUID(BLE_UUID_FOTA_CHARACTERISTC_UUID) != RES_OK)
  { return RES_ERROR;}

  return RES_OK;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Where ble_db_discovery_start() is called at BLE_GAP_EVT_CONNECTED event.&lt;/p&gt;
&lt;p&gt;And after all, I should be able to call BLE_ReadCharacteristicStatus() to read the data in the BLE_GATTC_EVT_READ_RSP event.&lt;/p&gt;
&lt;p&gt;Am I wrong with the aproximation?&lt;/p&gt;
&lt;p&gt;The problem is that I never get BLE_DB_DISCOVERY_COMPLETE in the discover event. And, at&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;BLE_DB_DISCOVERY_AVAILABLE I get this in the debugger:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1632376982587v1.png" /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;This are the peripheral services and characteristics, the peripheral is mature product of my company and works successfully&amp;nbsp;with other centrals.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1632377398878v2.png" /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What am I doing wrong?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks for your help,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Central ble, how to relate UUID with handlers?</title><link>https://devzone.nordicsemi.com/thread/330641?ContentTypeID=1</link><pubDate>Wed, 22 Sep 2021 11:48:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e78c1df9-e75d-4da8-a1dd-fcac73ce5136</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
[quote user=""]&lt;p&gt;But I do not how to retrieve the &amp;quot;handle&amp;quot; parameter. Again I guess I should use the discovery module but I do not know how to use it to relate the UUID of the characteristics with the &amp;quot;handle&amp;quot; parameter.&lt;/p&gt;
&lt;p&gt;What are the steps to get the &amp;quot;handle&amp;quot; parameter related with a UUID characteristic?&lt;/p&gt;[/quote]
&lt;p&gt;Yes, I would &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/lib_ble_db_discovery.html"&gt;recommend using the db discovery module for this&lt;/a&gt;. The usage of the module is detailed in the referenced documentation.&lt;br /&gt;You could also see this demonstrated in most of the central examples from the SDK. Take for example &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/ble_sdk_app_nus_c.html"&gt;the Nordic UART central example&lt;/a&gt; - it starts database discovery on the CONNECTED event looking for the Nordic UART service on the connected device. If it is found the events are forwarded to the nus client event handler, which then assigns it a handle and enables notifications for the TX characteristic.&lt;br /&gt;In case of multiple services you could take a look at &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/ble_sdk_app_hrc.html"&gt;the Heart rate collector example&lt;/a&gt;, which discovers and uses 2 separate services.&lt;br /&gt;&lt;br /&gt;Please do not hesitate to ask if any part of this should still be unclear, or if you have any other questions!&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>