<?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>Service discovery failed in the BLE connection with Zephyr</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/107765/service-discovery-failed-in-the-ble-connection-with-zephyr</link><description>Hello, 
 Issue summary: I have my custom hardware that has nRF52840 and I am working on a multi-connection BLE central solution that connects with two sensors and collects their data. There is a thread running that reads the sensors&amp;#39; data through the</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 01 Feb 2024 17:20:28 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/107765/service-discovery-failed-in-the-ble-connection-with-zephyr" /><item><title>RE: Service discovery failed in the BLE connection with Zephyr</title><link>https://devzone.nordicsemi.com/thread/467236?ContentTypeID=1</link><pubDate>Thu, 01 Feb 2024 17:20:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:be33352a-13cf-415b-8975-50eadefe75ca</guid><dc:creator>dejans</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;Could you share complete log which shows the error?&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Service discovery failed in the BLE connection with Zephyr</title><link>https://devzone.nordicsemi.com/thread/466872?ContentTypeID=1</link><pubDate>Wed, 31 Jan 2024 07:43:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:01bb1931-275b-4e1a-a192-06caf2be4843</guid><dc:creator>Ankit_chauhan</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Thanks for your reply.&lt;/p&gt;
&lt;p&gt;I am using&amp;nbsp;v2.4.0 and the Bluetooth Central Multilink&amp;nbsp;sample code from the Zephyr sample to implement the solution.&lt;/p&gt;
&lt;p&gt;I have updated the handling functions as per my requirement but maintain the operation flow similar to the sample.&lt;/p&gt;
&lt;p&gt;Here are the functions that I am currently using from the sample code.&lt;/p&gt;
&lt;p&gt;1. The BLE connection handling&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/* The scan function to connect the BLE peripherals */
static void start_scan(void)
{
	int err;

	/* Use active scanning and disable duplicate filtering to handle any
	 * devices that might update their advertising data at runtime. */
	struct bt_le_scan_param scan_param = {
		.type       = BT_LE_SCAN_TYPE_ACTIVE,
		.options    = BT_LE_SCAN_OPT_NONE,
		.interval   = BT_GAP_SCAN_FAST_INTERVAL,
		.window     = BT_GAP_SCAN_FAST_WINDOW,
	};

	err = bt_le_scan_start(&amp;amp;scan_param, device_found);
	if (err) {
		LOG_ERR(&amp;quot;Scanning failed to start (err %d)\n&amp;quot;, err);
		return;
	}

	printk(&amp;quot;Scanning successfully started\n&amp;quot;);
}


/* The scan function Callback handler */
static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
			 struct net_buf_simple *ad)
{
	if(!strcmp(name, sensor_tag_details.name)) {
		printk(&amp;quot;Your Device Found.\n&amp;quot;);
		memset(Connection_device_detail, 0x00, sizeof(Connection_device_detail));
		if(sensor_tag_details.type == SENSOR_1) {
			strcpy(Connection_device_detail,&amp;quot;sensor_1&amp;quot;);
		} else {
			strcpy(Connection_device_detail,&amp;quot;sensor_2&amp;quot;);
		}

		err = bt_le_scan_stop();
		if (err) {
			printk(&amp;quot;%s: Stop LE scan failed (err %d)\n&amp;quot;, __func__, err);
			return;
		}

		static struct bt_conn *tempconn;
		err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
				BT_LE_CONN_PARAM_DEFAULT, &amp;amp;tempconn);
		if (err) {
			printk(&amp;quot;%s: Create conn failed (err %d)\n&amp;quot;, __func__, err);
			start_scan();
		} else {
			uint8_t conn_index = bt_conn_index(tempconn);
			connecting_index = conn_index;
			printk(&amp;quot;Connection index= %d.\n&amp;quot;, conn_index);
			connections[conn_index] = tempconn;
			tempconn = NULL;
			is_conn_cmd_rcvd = false;
		}
	}
}


/* Connected callback function */
static void connected(struct bt_conn *conn, uint8_t err)
{
	char addr[BT_ADDR_LE_STR_LEN];
	uint8_t conn_err;

	struct bt_conn_info info;
    bt_conn_get_info(conn, &amp;amp;info);

	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

	if(info.role == BT_CONN_ROLE_MASTER) {
		if (err) {
			printk(&amp;quot;Failed to connect to %s (%u)\n&amp;quot;, addr, err);
			start_scan();
			return;
		}
		
		conn_index = bt_conn_index(conn);
		printk(&amp;quot;Connected_conn_index: %s %d\n&amp;quot;, addr, conn_index);
		
		if (conn == connections[conn_index]) {
			conn_count++;
		#if defined(CONFIG_BT_SMP)
			conn_err = bt_conn_set_security(connections[conn_index], BT_SECURITY_L1);
			if (conn_err) {
				printk(&amp;quot;Failed to set security: %d\n&amp;quot;, conn_err);
				return;
			}
		#endif


		#if defined(CONFIG_BT_GATT_CLIENT)
			mtu_exchange(connections[conn_index]);
		#endif

			service_disc.tagtype = sensor_tag_details.type;
			sh_discover_primary_svc(conn, &amp;amp;sys_uuid.uuid);
		}
		if (conn_count &amp;lt; CONFIG_BT_MAX_CONN) {
			start_scan();
		}
	}

	if(info.role == BT_CONN_ROLE_SLAVE) {
		if(err) {
			printk(&amp;quot;Failed to connect to %s (%u)\n&amp;quot;, addr, err);
		} else {
			printk(&amp;quot;Connected: %s\n&amp;quot;, addr);
		}
	}

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;2. BLE service discovery handling&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;
/* Service discovery function to initiate all the services and characteristics of the sensors */
static uint8_t sh_discover_primary_svc(struct bt_conn *conn,
							const struct bt_uuid *uuid)
{
	int err;

	char *dev_type = get_sensor_type_by_index(ble_manager, bt_conn_index(conn));
	printk(&amp;quot;Origial device type = %s.\n&amp;quot;, dev_type);
	discover_params.uuid = uuid;
	if(!strcmp(&amp;quot;sensor_1&amp;quot;, dev_type)) {
		printk(&amp;quot;Received device type: sensor_1&amp;quot;);
		discover_params.func = sensor_1_discover_func;
	} else {
		printk(&amp;quot;Received device type: sensor_2&amp;quot;);
		discover_params.func = sensor_2_discover_func;
	}
	discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
	discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
	discover_params.type = BT_GATT_DISCOVER_PRIMARY;

	err = bt_gatt_discover(conn, &amp;amp;discover_params);
	if (err) {
		LOG_ERR(&amp;quot;Discover failed (err %d)\n&amp;quot;, err);
	}
	return err;
}


/* Service discovery hanlder function of the sensors */
static __attribute__((unused)) uint8_t sensor_1_discover_func(struct bt_conn *conn,
			     const struct bt_gatt_attr *attr,
			     struct bt_gatt_discover_params *params)
{
	//My descover function that handls the discovery of services, characteristics, and descriptors
}

static __attribute__((unused)) uint8_t sensor_2_discover_func(struct bt_conn *conn,
			     const struct bt_gatt_attr *attr,
			     struct bt_gatt_discover_params *params)
{
	//My descover function that handls the discovery of services, characteristics, and descriptors
}

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;3. This is how I am reading the sensor characteristics&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;
/* Function to read a characteristic given its UUID */
void read_characteristic(struct bt_conn *conn, char *device_type) {
    struct bt_conn *read_conn;
    // struct bt_gatt_read_params read_params;
	bt_addr_le_t addr;
	struct bt_conn_info info;
	int err;

	// Check if the connection pointer is not NULL
	if (conn != NULL) {
		printk(&amp;quot;Before bt_conn_lookup_addr_le\n&amp;quot;);
		bt_addr_le_copy(&amp;amp;addr, bt_conn_get_dst(conn));
		printk(&amp;quot;After bt_conn_lookup_addr_le\n&amp;quot;);

		// Now you can use &amp;#39;addr&amp;#39; for further processing
	} else {
		printk(&amp;quot;Error: Connection pointer is NULL\n&amp;quot;);
		return;
	}

    /* Get the default connection */
    read_conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &amp;amp;addr);
    if (!read_conn) {
        printk(&amp;quot;Not connected\n&amp;quot;);
        return;
    } else {
		printk(&amp;quot;Get the connection.\n&amp;quot;);
	}
	if(!strcmp(device_type, &amp;quot;sensor_1&amp;quot;)) {
		for (int i = 0; i &amp;lt; 4; i++) {
			if(!read_conn) {
			printk(&amp;quot;No connection found.\n&amp;quot;);
			return;
			} else {

				/* Start reading the characteristic */
				int err = bt_gatt_read(read_conn, &amp;amp;sensor_1_read_params_array[i]);
				printk(&amp;quot;starting read: %d\n&amp;quot;, err);
				if (err) {
					printk(&amp;quot;Error starting read: %d\n&amp;quot;, err);
				} {
					printk(&amp;quot;read sucessfully.\n&amp;quot;);
				}
				// if (read_conn) {
				// 	bt_conn_unref(read_conn);
				// }
			}
		}
	} else if (!strcmp(device_type, &amp;quot;sensor_2&amp;quot;)) {
		for (int i = 0; i &amp;lt; 6; i++) {
			if(!read_conn) {
			printk(&amp;quot;No connection found.\n&amp;quot;);
			return;
			} else {

				/* Start reading the characteristic */
				int err = bt_gatt_read(read_conn, &amp;amp;sensor_1_read_params_array[i]);
				printk(&amp;quot;starting read: %d\n&amp;quot;, err);
				if (err) {
					printk(&amp;quot;Error starting read: %d\n&amp;quot;, err);
				} {
					printk(&amp;quot;read sucessfully.\n&amp;quot;);
				}
				// if (read_conn) {
				// 	bt_conn_unref(read_conn);
				// }
			}
		}
	}

	if (read_conn) {
		bt_conn_unref(read_conn);
	}
}

static struct bt_gatt_read_params sensor_1_read_params_array[] = {
    {
        .func = temp_read_cb,
        .handle_count = 1,
		.single.handle = 45,
        .single.offset = 0,
    },
    {
        .func = hr_read_cb,
        .handle_count = 1,
		.single.handle = 64,
        .single.offset = 0,
    },
    {
        .func = bat_read_cb,
        .handle_count = 1,
		.single.handle = 48,
        .single.offset = 0,
    },
	{
        .func = sys_stat_cb,
        .handle_count = 1,
		.single.handle = 60,
        .single.offset = 0,
    },
	
};



/* Callback function for characteristic read */
static void temp_read_cb(struct bt_conn *conn, uint8_t err,
                    struct bt_gatt_read_params *params, const void *data,
                    uint16_t length, uint16_t handle) {

	// printk(&amp;quot;In temp_read_cb.\n&amp;quot;);
    if (err) {
        printk(&amp;quot;Error reading data: %u\n&amp;quot;, err);
        return;
    }

    if (!data) {
        printk(&amp;quot;No data received\n&amp;quot;);
        return;
    }

	uint8_t* notif_data = (uint8_t*) data;
	 
	
    // printk(&amp;quot;Read data \n: &amp;quot;);
    // for(int i = 0; i&amp;lt;length; i++)
	// {
	// 	printk(&amp;quot;%x &amp;quot;, notif_data[i]);
	// }

    // printk(&amp;quot;\n&amp;quot;);

	printk(&amp;quot;Temprature value = %d.\n&amp;quot;, notif_data[4]);

	// chest_tmp = notif_data[4];
	atomic_set(&amp;amp;chest_tmp, notif_data[4]);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I have identified that the connection, service discovery, and all other operations are working fine for individual sensors.&lt;/p&gt;
&lt;p&gt;But when I started reading the sensor_1 data continuously every second using the read_characteristic function, and then I tried to connect the sensor_2 it did not discover its services and reported the error as I mentioned in my first question.&lt;/p&gt;
&lt;p&gt;Regards...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Service discovery failed in the BLE connection with Zephyr</title><link>https://devzone.nordicsemi.com/thread/466194?ContentTypeID=1</link><pubDate>Fri, 26 Jan 2024 11:13:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bdde3412-b0c9-4d65-a00b-b890eef0353d</guid><dc:creator>dejans</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;Which NCS version do you use?&lt;br /&gt;&lt;br /&gt;Is your application based on any of existing NCS samples? If so, have you modified the sample in any way?&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>