<?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>Support multiple subscription for single service with multiple characteristics</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/116345/support-multiple-subscription-for-single-service-with-multiple-characteristics</link><description>Hi, 
 I am working on project where the peripheral has 2 services, and each has multiple characteristics. 
 The central needs to subscribe to all the characteristics and when doing so I am seeing error: -120 (Socket already connected) 
 Below is the device</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 16 Nov 2024 05:31:00 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/116345/support-multiple-subscription-for-single-service-with-multiple-characteristics" /><item><title>RE: Support multiple subscription for single service with multiple characteristics</title><link>https://devzone.nordicsemi.com/thread/510785?ContentTypeID=1</link><pubDate>Sat, 16 Nov 2024 05:31:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e468f0e7-184d-41f9-91c3-c1f29cf16ac3</guid><dc:creator>Jithin A</dc:creator><description>&lt;p&gt;Hi Einor,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thank you for the response, that make sense.&lt;/p&gt;
&lt;p&gt;It is working for me.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Support multiple subscription for single service with multiple characteristics</title><link>https://devzone.nordicsemi.com/thread/510646?ContentTypeID=1</link><pubDate>Fri, 15 Nov 2024 10:23:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cbfde58d-2c16-4543-ba6a-16766574cbc4</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I see one problem with your implementation. You are re-using the same variable for both calls to&amp;nbsp;&lt;code&gt;bt_gatt_subscribe()&lt;/code&gt;, and that will cause problems. Moreover these vairables need to stay valid for as long as subscribed. One way to improve this is to make two separate variables that are static.&lt;/p&gt;
&lt;p&gt;For reference, this is the relevant section from the &lt;a href="https://docs.nordicsemi.com/bundle/zephyr-apis-latest/page/group_bt_gatt_client.html#ga7d4a8e18f51ba6476886a15f81f48e5c"&gt;&lt;code&gt;bt_gatt_subscribe()&lt;/code&gt; API documentation&lt;/a&gt;:&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;Notifications are asynchronous therefore the &lt;code&gt;params&lt;/code&gt; must remain valid while subscribed and cannot be reused for additional subscriptions whilst active.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Support multiple subscription for single service with multiple characteristics</title><link>https://devzone.nordicsemi.com/thread/510528?ContentTypeID=1</link><pubDate>Thu, 14 Nov 2024 14:30:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cf457c65-6cfc-4eff-b0f9-53bbd7d7cffc</guid><dc:creator>Jithin A</dc:creator><description>&lt;p&gt;Hi Einar,&lt;/p&gt;
&lt;p&gt;Thank you for the response, let me put my comments here&lt;/p&gt;
[quote userid="7377" url="~/f/nordic-q-a/116345/support-multiple-subscription-for-single-service-with-multiple-characteristics/510512"]How are you testing?[/quote]
&lt;p&gt;--&amp;gt; I have here a NRF52840DK and a custom PCB with NRF52840 in it.&lt;/p&gt;
&lt;p&gt;So, I have configured DK as central device for logging and custom PCB as peripheral.&lt;/p&gt;
&lt;p&gt;When I try to subscribe using my DK, I am seeing the above error.&lt;/p&gt;
[quote userid="7377" url="~/f/nordic-q-a/116345/support-multiple-subscription-for-single-service-with-multiple-characteristics/510512"]Are you abel to&amp;nbsp;subscribe to any of the two characteristics, just not both at the same time?[/quote]
&lt;p&gt;--&amp;gt; Yes, in the above log you can see, the CMD characteristics is successfully subscribed and from the characteristics the peripheral can send the indication and central receive correctly.&lt;/p&gt;
&lt;p&gt;If I subscribe to IND characteristics without subscribing to the CMD characteristics, it works.&lt;/p&gt;
&lt;p&gt;What I see is I am not able to subscribe to both at a time.&lt;/p&gt;
[quote userid="7377" url="~/f/nordic-q-a/116345/support-multiple-subscription-for-single-service-with-multiple-characteristics/510512"]Have you tested with a different central implementation (for instance nRF Connect for Mobile) to narrow down the issue to understand if the issue is on the central or peripheral side?[/quote]
&lt;p&gt;--&amp;gt; Not yet, I just got to know about the mobile app (NRF Connect), will give a try with that and update to you here.&lt;/p&gt;
[quote userid="7377" url="~/f/nordic-q-a/116345/support-multiple-subscription-for-single-service-with-multiple-characteristics/510512"]I suspect you are referencing the same CCCD for some reason.[/quote]
&lt;p&gt;--&amp;gt; Yes, I am also thinking same.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Let me share you my service discovery code snippet, so you can get to know any mistake I am doing.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;
static uint8_t discover_cb (struct bt_conn *conn, const struct bt_gatt_attr *attr,
			                struct bt_gatt_discover_params *params)
{
	int err;
	static app_task_msg_t msg;
	static struct bt_uuid_128 uuid = BT_UUID_INIT_128(0);

	if (!attr) {
		// The control should not come here. Just in case, the attribute handles are not handled
        // properly it comes, disconnect the peer and wait for reconnection
        //
		(void)memset(params, 0, sizeof(*params));
		bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);

		return BT_GATT_ITER_STOP;
	}

	if (!bt_uuid_cmp(g_discover_params.uuid, BT_UUID_NOTIFY_DEV))
    {
		// Found the NOTIF_DEV_SERVICE. Look for the CMD characteristic handle
        //
		memcpy(&amp;amp;uuid, BT_UUID_CMD, sizeof(uuid));
		g_discover_params.uuid = &amp;amp;uuid.uuid;
		g_discover_params.start_handle = attr-&amp;gt;handle + 1;
		g_discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
		bt_gatt_discover(conn, &amp;amp;g_discover_params);
	}
    else if (!bt_uuid_cmp(g_discover_params.uuid, BT_UUID_CMD)) {
		// Copy command characteristics handle for future reference
        //
		current_dev_info.cmd_chr_handle = (attr-&amp;gt;handle + 1);

        // As we have found command characteristics, look for indication characteristics
        //
        printk(&amp;quot;Found CMD CHR: %d\n&amp;quot;, current_dev_info.cmd_chr_handle);
		memcpy(&amp;amp;uuid, BT_UUID_IND, sizeof(uuid));
		g_discover_params.uuid = &amp;amp;uuid.uuid;
		g_discover_params.start_handle = attr-&amp;gt;handle;
		g_discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
		bt_gatt_discover(conn, &amp;amp;g_discover_params);
	}
    else if (!bt_uuid_cmp(g_discover_params.uuid, BT_UUID_IND)) {
		// Copy indication characteristics handle for future reference
        //
		current_dev_info.ind_chr_handle = (attr-&amp;gt;handle + 1);

		// Found the NOTIF_DEV_CMD_CHR handle. Find the DESCRIPTOR handle
		//
        printk(&amp;quot;Found IND CHR: %d\n&amp;quot;, current_dev_info.ind_chr_handle);
		memcpy(&amp;amp;uuid, BT_UUID_GATT_CCC, sizeof(struct bt_uuid_16));
		g_discover_params.uuid = &amp;amp;uuid.uuid;
		g_discover_params.start_handle = attr-&amp;gt;handle + 2;
		g_discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR;
		g_subscribe_params.value_handle = bt_gatt_attr_value_handle(attr);
		bt_gatt_discover(conn, &amp;amp;g_discover_params);
	}
    else {
		// Subscribe to the INDICATIONS, this will enable indications on the peripheral
		//
		g_subscribe_params.notify = indication_cb;
		g_subscribe_params.value = BT_GATT_CCC_INDICATE;
		g_subscribe_params.ccc_handle = current_dev_info.cmd_chr_handle + 1;

        // Subscribe for CMD 
        //
		err = bt_gatt_subscribe(conn, &amp;amp;g_subscribe_params);
        printk(&amp;quot;bt_gatt_subscribe: %d, ccc_handle: %d\n&amp;quot;, err, g_subscribe_params.ccc_handle);
		if (err == 0) {
            printk(&amp;quot;[SUBSCRIBED]\n&amp;quot;);
		}

        // Subscribe for IND
        //
        g_subscribe_params.ccc_handle = current_dev_info.ind_chr_handle + 1;
		err = bt_gatt_subscribe(conn, &amp;amp;g_subscribe_params);
        printk(&amp;quot;bt_gatt_subscribe: %d, ccc_handle: %d\n&amp;quot;, err, g_subscribe_params.ccc_handle);
		if (err == 0) {
            printk(&amp;quot;[SUBSCRIBED]\n&amp;quot;);
		}
	}

	return BT_GATT_ITER_STOP;
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Please review it once and let me know.&lt;/p&gt;
&lt;p&gt;Thank you in advance&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Support multiple subscription for single service with multiple characteristics</title><link>https://devzone.nordicsemi.com/thread/510512?ContentTypeID=1</link><pubDate>Thu, 14 Nov 2024 13:52:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2da21367-a119-42cb-95cb-2ffeefd0720f</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;There is no fundamental issue with having multiple characteristics with nofify or indicate in the same service. I have some qustions in order to understand more:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;How are you testing?&lt;/li&gt;
&lt;li&gt;Are you abel to&amp;nbsp;subscribe to any of the two characteristics, just not both at the same time?&lt;/li&gt;
&lt;li&gt;Have you tested with a different central implementation (for instance nRF Connect for Mobile) to narrow down the issue to understand if the issue is on the central or peripheral side?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That said, I assume we need to look more closely at your central implementation.&amp;nbsp;bt_gatt_subscribe() will return -EALREADY (-120) if allready subscribed, so I suspect you are referencing the same CCCD for some reason.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Support multiple subscription for single service with multiple characteristics</title><link>https://devzone.nordicsemi.com/thread/510399?ContentTypeID=1</link><pubDate>Thu, 14 Nov 2024 05:39:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:820d5910-80a1-4354-849a-566842a76790</guid><dc:creator>Jithin A</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;Anybody there looking into my issue. Please update if any.&lt;/p&gt;
&lt;p&gt;Thank you in advance&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>