<?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>Interface nRF5340dk with external BLE sensors</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/113044/interface-nrf5340dk-with-external-ble-sensors</link><description>I&amp;#39;m developing a system where I want to connect simultaneously to different BLE and ANT+ sensors. I&amp;#39;m using the nRF5340 DK and I would like to be able to select which sensors to connect to once I detect nearby devices, so I can receive only the information</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 17 Jul 2024 12:36:45 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/113044/interface-nrf5340dk-with-external-ble-sensors" /><item><title>RE: Interface nRF5340dk with external BLE sensors</title><link>https://devzone.nordicsemi.com/thread/494358?ContentTypeID=1</link><pubDate>Wed, 17 Jul 2024 12:36:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:304c203a-f51d-4f90-9e8a-48216957f755</guid><dc:creator>Priyanka</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Please make sure that your console is initialized properly, say, the console_init function. You could also try adding a printk statement in your get_user_input function to make sure that it handles the console input correctly:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;void get_user_input(char *input, size_t size) &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt; printk(&amp;quot;Input: &amp;quot;);&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt; char *line = console_getline();&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt; if (line != NULL) {&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt; strncpy(input, line, size - 1);&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt; input[size - 1] = &amp;#39;\0&amp;#39;; // Ensure null-termination&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt; }&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Also, could you attach your prj.conf?&lt;/p&gt;
&lt;p&gt;-Priyanka&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Interface nRF5340dk with external BLE sensors</title><link>https://devzone.nordicsemi.com/thread/494169?ContentTypeID=1</link><pubDate>Tue, 16 Jul 2024 13:39:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4fe9b1da-8f9e-43a1-8f13-6d214ff390fd</guid><dc:creator>inigo.ls</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stddef.h&amp;gt;
#include &amp;lt;errno.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/sys/printk.h&amp;gt;
#include &amp;lt;zephyr/sys/byteorder.h&amp;gt;
#include &amp;lt;zephyr/console/console.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/hci.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/conn.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/uuid.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/gatt.h&amp;gt;

#define MAX_DEVICES 20  // Define MAX_DEVICES

static struct bt_conn *default_conn;
static struct bt_uuid_16 discover_uuid = BT_UUID_INIT_16(0);
static struct bt_gatt_discover_params discover_params;
static struct bt_gatt_subscribe_params subscribe_params;
static bt_addr_le_t discovered_devices[MAX_DEVICES];
static uint8_t device_count = 0;

static int scan_start(void);

static uint8_t notify_func(struct bt_conn *conn,
                           struct bt_gatt_subscribe_params *params,
                           const void *data, uint16_t length)
{
    if (!data) {
        printk(&amp;quot;[UNSUBSCRIBED]\n&amp;quot;);
        params-&amp;gt;value_handle = 0U;
        return BT_GATT_ITER_STOP;
    }

    uint8_t flags = ((uint8_t *)data)[0];
    uint8_t heart_rate;
    uint16_t heart_rate_16;

    if (flags &amp;amp; 0x01) {
        heart_rate_16 = sys_get_le16(&amp;amp;((uint8_t *)data)[1]);
        printk(&amp;quot;Heart Rate: %u bpm\n&amp;quot;, heart_rate_16);
    } else {
        heart_rate = ((uint8_t *)data)[1];
        printk(&amp;quot;Heart Rate: %u bpm\n&amp;quot;, heart_rate);
    }

    return BT_GATT_ITER_CONTINUE;
}

static uint8_t discover_func(struct bt_conn *conn,
                             const struct bt_gatt_attr *attr,
                             struct bt_gatt_discover_params *params)
{
    int err;

    if (!attr) {
        printk(&amp;quot;Discover complete\n&amp;quot;);
        (void)memset(params, 0, sizeof(*params));
        return BT_GATT_ITER_STOP;
    }

    printk(&amp;quot;[ATTRIBUTE] handle %u\n&amp;quot;, attr-&amp;gt;handle);

    if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_HRS)) {
        memcpy(&amp;amp;discover_uuid, BT_UUID_HRS_MEASUREMENT, sizeof(discover_uuid));
        discover_params.uuid = &amp;amp;discover_uuid.uuid;
        discover_params.start_handle = attr-&amp;gt;handle + 1;
        discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;

        err = bt_gatt_discover(conn, &amp;amp;discover_params);
        if (err) {
            printk(&amp;quot;Discover failed (err %d)\n&amp;quot;, err);
        }
    } else if (!bt_uuid_cmp(discover_params.uuid,
                            BT_UUID_HRS_MEASUREMENT)) {
        memcpy(&amp;amp;discover_uuid, BT_UUID_GATT_CCC, sizeof(discover_uuid));
        discover_params.uuid = &amp;amp;discover_uuid.uuid;
        discover_params.start_handle = attr-&amp;gt;handle + 2;
        discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR;
        subscribe_params.value_handle = bt_gatt_attr_value_handle(attr);

        err = bt_gatt_discover(conn, &amp;amp;discover_params);
        if (err) {
            printk(&amp;quot;Discover failed (err %d)\n&amp;quot;, err);
        }
    } else {
        subscribe_params.notify = notify_func;
        subscribe_params.value = BT_GATT_CCC_NOTIFY;
        subscribe_params.ccc_handle = attr-&amp;gt;handle;

        err = bt_gatt_subscribe(conn, &amp;amp;subscribe_params);
        if (err &amp;amp;&amp;amp; err != -EALREADY) {
            printk(&amp;quot;Subscribe failed (err %d)\n&amp;quot;, err);
        } else {
            printk(&amp;quot;[SUBSCRIBED]\n&amp;quot;);
        }

        return BT_GATT_ITER_STOP;
    }

    return BT_GATT_ITER_STOP;
}

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

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

    if (conn_err) {
        printk(&amp;quot;Failed to connect to %s (%u)\n&amp;quot;, addr, conn_err);

        bt_conn_unref(default_conn);
        default_conn = NULL;

        scan_start();
        return;
    }

    printk(&amp;quot;Connected: %s\n&amp;quot;, addr);

    if (conn == default_conn) {
        memcpy(&amp;amp;discover_uuid, BT_UUID_HRS, sizeof(discover_uuid));
        discover_params.uuid = &amp;amp;discover_uuid.uuid;
        discover_params.func = 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(default_conn, &amp;amp;discover_params);
        if (err) {
            printk(&amp;quot;Discover failed(err %d)\n&amp;quot;, err);
            return;
        }
    }
}

static bool eir_found(struct bt_data *data, void *user_data)
{
    printk(&amp;quot;[AD]: %u data_len %u\n&amp;quot;, data-&amp;gt;type, data-&amp;gt;data_len);
    return true;
}

static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
                         struct net_buf_simple *ad)
{
    char dev[BT_ADDR_LE_STR_LEN];

    bt_addr_le_to_str(addr, dev, sizeof(dev));
    printk(&amp;quot;[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i\n&amp;quot;,
           dev, type, ad-&amp;gt;len, rssi);

    if (type == BT_HCI_ADV_IND || type == BT_HCI_ADV_DIRECT_IND) {
        bt_data_parse(ad, eir_found, (void *)addr);

        // Check if the device is already in the list
        for (int i = 0; i &amp;lt; device_count; i++) {
            if (bt_addr_le_cmp(&amp;amp;discovered_devices[i], addr) == 0) {
                return; // Device already in the list
            }
        }

        // Store the discovered device
        if (device_count &amp;lt; MAX_DEVICES) {
            bt_addr_le_copy(&amp;amp;discovered_devices[device_count++], addr);
        }
    }
}

static int scan_start(void)
{
    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,
    };

    return bt_le_scan_start(&amp;amp;scan_param, device_found);
}

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
    char addr[BT_ADDR_LE_STR_LEN];
    int err;

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

    printk(&amp;quot;Disconnected: %s (reason 0x%02x)\n&amp;quot;, addr, reason);

    if (default_conn != conn) {
        return;
    }

    bt_conn_unref(default_conn);
    default_conn = NULL;

    err = scan_start();
    if (err) {
        printk(&amp;quot;Scanning failed to start (err %d)\n&amp;quot;, err);
    }
}

BT_CONN_CB_DEFINE(conn_callbacks) = {
    .connected = connected,
    .disconnected = disconnected,
};

void get_user_input(char *input, size_t size) {
    char *line = console_getline();
    if (line != NULL) {
        strncpy(input, line, size - 1);
        input[size - 1] = &amp;#39;\0&amp;#39;; // Ensure null-termination
    }
}

int main(void) {
    char input[100];
    int selected_index;
    int err;

    err = bt_enable(NULL);
    if (err) {
        printk(&amp;quot;Bluetooth init failed (err %d)\n&amp;quot;, err);
        return 0;
    }

    printk(&amp;quot;Bluetooth initialized\n&amp;quot;);

    err = scan_start();
    if (err) {
        printk(&amp;quot;Scanning failed to start (err %d)\n&amp;quot;, err);
        return 0;
    }

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

    k_sleep(K_SECONDS(10));  // Adjust the scan duration as needed

    bt_le_scan_stop();
    printk(&amp;quot;Scanning stopped\n&amp;quot;);

    // List discovered devices
    printk(&amp;quot;Discovered devices:\n&amp;quot;);
    for (int i = 0; i &amp;lt; device_count; i++) {
        char addr_str[BT_ADDR_LE_STR_LEN];
        bt_addr_le_to_str(&amp;amp;discovered_devices[i], addr_str, sizeof(addr_str));
        printk(&amp;quot;[%d] %s\n&amp;quot;, i, addr_str);
    }

    // Select device
    selected_index = -1;
    while (selected_index &amp;lt; 0 || selected_index &amp;gt;= device_count) {
        printk(&amp;quot;Enter the number of the device to connect to: &amp;quot;);
        get_user_input(input, sizeof(input));
        selected_index = atoi(input);
    }

    // Connect to the selected device
    bt_addr_le_t *selected_device = &amp;amp;discovered_devices[selected_index];
    err = bt_conn_le_create(selected_device, BT_CONN_LE_CREATE_CONN,
                            BT_LE_CONN_PARAM_DEFAULT, &amp;amp;default_conn);
    if (err) {
        printk(&amp;quot;Create connection failed (err %d)\n&amp;quot;, err);
        return 0;
    }

    printk(&amp;quot;Connecting to selected device...\n&amp;quot;);

    return 0;
}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;What I would like is to be able to enter the number of the device I want to connect to via the keyboard, so that I can integrate it into a user interface with a TFT LCD display in the future. The attached code does not give me an error, but when I run it and it lists all the found devices, it does not allow me to make the selection. I don&amp;#39;t know if I need to modify something in the &lt;code&gt;prj.config&lt;/code&gt; or &lt;code&gt;CMakeLists.txt&lt;/code&gt; or if I need to add something else.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Interface nRF5340dk with external BLE sensors</title><link>https://devzone.nordicsemi.com/thread/494165?ContentTypeID=1</link><pubDate>Tue, 16 Jul 2024 13:30:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7b8b3652-0363-4939-94b4-fa5846ebe941</guid><dc:creator>Priyanka</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Is it possible to share the code snippet where you try this? Do you get any errors when trying this?&lt;/p&gt;
&lt;p&gt;-Priyanka&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Interface nRF5340dk with external BLE sensors</title><link>https://devzone.nordicsemi.com/thread/494061?ContentTypeID=1</link><pubDate>Tue, 16 Jul 2024 07:28:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6e2d0f37-84a7-48f6-a583-939ff6c757eb</guid><dc:creator>inigo.ls</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I have tried with BLE examples where I receive information from different devices around me.&lt;/p&gt;
&lt;p&gt;I have also tried setting the MAC address of the device I&amp;#39;m interested in, which is a HR sensor, and that worked too.&lt;/p&gt;
&lt;p&gt;Now, what I would like to do is first detect the nearby devices and then, after that, be able to select the device(s) I want to connect to, in order to receive information only from them.&lt;/p&gt;
&lt;p&gt;At the moment, I&amp;#39;m trying to select the sensor I&amp;#39;m interested in by entering the MAC address via keyboard, but I am not getting the correct result.&lt;br /&gt;&lt;br /&gt;Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Interface nRF5340dk with external BLE sensors</title><link>https://devzone.nordicsemi.com/thread/494057?ContentTypeID=1</link><pubDate>Tue, 16 Jul 2024 07:20:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:463c61c4-6188-4d27-998f-b50f27384ca5</guid><dc:creator>Priyanka</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;For this you would to first scan in order to detect nearby BLE devices and then connect based on any criteria, say,&amp;nbsp;device name, advertised services, signal strength or any other parameter of your choice.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Priyanka&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>