HOW THE CONNECT THE PERIPHERAL USING THAT DEVICE MAC ADDRESS THROUGH THE CENTRAL DEVICE(WHICH IS MY BOARD NRF52833DK)

Hi,

toolchain:-v3.1.0,sdk:v3.1.0. i am using the vscode software for the development

Basically i am trying to connect the peripheral using the address (mac:-7C:D9:F4:14:05:A8).in this my board NRF52833DK  is working as the central. I am using the sample code which is available (NAME IS:-central)  in that code i have added the mac address logic ,

static const bt_addr_le_t target_addr = {
    .type = BT_ADDR_LE_PUBLIC,  // or BT_ADDR_LE_PUBLIC depending on your device
    .a = { 0xA8, 0x05, 0x14, 0xF4, 0xD9, 0x7C }  // LSB first: 7C:D9:F4:14:05:A8
};
also i have little bit done the changes into the device_found and start_scan logic so below i will past the whole code logic so it will help you to understand.in prj.config file same as it is 
/* main.c - Application main entry point */

/*
 * Copyright (c) 2015-2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/types.h>
#include <stddef.h>
#include <errno.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>

#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/sys/byteorder.h>



#define MIN_INTERVAL 6   /* 7.5 ms */
#define MAX_INTERVAL 320 /* 400 ms */
#define LATENCY 0
#define TIMEOUT 400

static const struct bt_le_scan_param scan_param = {
    .type       = BT_LE_SCAN_TYPE_ACTIVE,
    .options    = BT_LE_SCAN_OPT_NONE,
    .interval   = 0x0060,  /* 60 ms */
    .window     = 0x0030,  /* 30 ms */
};

/* Connection parameters */
static const struct bt_le_conn_param conn_param = {
    .interval_min = 0x0018,  /* 30 ms */
    .interval_max = 0x0028,  /* 50 ms */
    .latency      = 0,
    .timeout      = 400,
};

static const bt_addr_le_t target_addr = {
    .type = BT_ADDR_LE_PUBLIC,  // or BT_ADDR_LE_PUBLIC depending on your device
    .a = { 0xA8, 0x05, 0x14, 0xF4, 0xD9, 0x7C }  // LSB first: 7C:D9:F4:14:05:A8
};


static void start_scan(void);

static struct bt_conn *default_conn;


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

    bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
    /*printk("[DEVICE] %s (RSSI %d, adv_type %u, addr_type %u)\n",
           addr_str, rssi, type, addr->type);*/

    if (default_conn) {
        return;
    }

     if (bt_addr_le_cmp(addr, &target_addr) != 0) {
        return;
    }

    printk(" Target device found: %s (RSSI %d, adv_type %u)\n",
           addr_str, rssi, type);


    /* We're only interested in connectable events */
    if (type != BT_GAP_ADV_TYPE_ADV_IND &&
        type != BT_GAP_ADV_TYPE_ADV_DIRECT_IND) {
        return;
    }

   
    if (bt_addr_le_cmp(addr, &target_addr) != 0) {
        /* Not the target device */
        return;
    }

    bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
    printk("Device found: %s (RSSI %d)\n", addr_str, rssi);

    if (bt_le_scan_stop()) {
        return;
    }


    /* connect only to devices in close proximity */
    //if (rssi < -50) {
        //return;
    //}

    if (bt_le_scan_stop());
     if (err && err != -EALREADY) {
        printk("Failed to stop scanning (err %d)\n", err);
        return;
    }

    err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
                       &conn_param, &default_conn);
    if (err) {
        printk("Create conn to %s failed (%d)\n", addr_str, err);
        start_scan();
    }
}

static void start_scan(void)
{
    int err;

    /* This demo doesn't require active scan */
    //err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
    //if (err) {
    //  printk("Scanning failed to start (err %d)\n", err);
    //  return;
    //}
    //printk("Scanning successfully started\n");

    //struct bt_le_scan_param scan_param = {
  //  .type     = BT_HCI_LE_SCAN_ACTIVE,  // passive scan
  //  .interval = 0x0060,                  // scan interval
   // .window   = 0x0030,                  // scan window
  //  .options  = 0,                        // no special options
//};

static struct bt_le_conn_param custom_conn_param = {
    .interval_min = MIN_INTERVAL,
    .interval_max = MAX_INTERVAL,
    .latency = LATENCY,
    .timeout = TIMEOUT,
};




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

printk("Scanning successfully started\n");


}

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

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

    if (err) {
        printk("Failed to connect to %s %u %s\n", addr, err, bt_hci_err_to_str(err));

        bt_conn_unref(default_conn);
        default_conn = NULL;

        start_scan();
        return;
    }

    if (conn != default_conn) {
        return;
    }

    printk("Connected: %s\n", addr);

    //bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
}

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

    if (conn != default_conn) {
        return;
    }

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

    printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));

    bt_conn_unref(default_conn);
    default_conn = NULL;

    start_scan();
}

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

int main(void)
{
    int err;

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

    printk("Bluetooth initialized\n");

    start_scan();
    return 0;
}


after the build and flash i recevied the blow logs :-

Target device found: 7C:D9:F4:14:05:A8 (public) (RSSI -72, adv_type 0)
Device found: 7C:D9:F4:14:05:A8 (public) (RSSI -72)
Failed to connect to 7C:D9:F4:14:05:A8 (public) 2 BT_HCI_ERR_UNKNOWN_CONN_ID
Scanning successfully started


why the central is not connecting to my peripheral . when i am trying to connect with the nrf connect android app it will connect to that peripheral sucessfully but when i am tryaing to connect through central its failed to connect why?? its possiable that i amusing wrong sample code should i try to use the central_uart example ?? 
Parents
  • Hi,

    The error BT_HCI_ERR_UNKNOWN_CONN_ID means that the connection was aborted or timed out. I do not see any indication that you explicitly disconnect here, so a timeout is more likely. However, I see you filter on only connectable advertising. Could it be that the peripheral discregards the connection for some reason (perhaps it is using a whitelist / filter accept list)? Do you have a sniffer trace so that we can see what is happeing on air?

Reply
  • Hi,

    The error BT_HCI_ERR_UNKNOWN_CONN_ID means that the connection was aborted or timed out. I do not see any indication that you explicitly disconnect here, so a timeout is more likely. However, I see you filter on only connectable advertising. Could it be that the peripheral discregards the connection for some reason (perhaps it is using a whitelist / filter accept list)? Do you have a sniffer trace so that we can see what is happeing on air?

Children
  • Hi,

    I do not see any connection attempt in that trace. Can you test again and verify that you do not see any?

  • hi,

    ok let me expian you the steps which i followd and also below i will attache the screenstop so you can view that and tell me where i am missing  step 
    1)firstly i will open the nRF connect for desktop tool  -> programmer section -> i will connect to the board nRF52833dk board -> add file i have attached .hex file (blow attached) -> i clicked erase & write option

    2) in this step i will open the wireshark tool and in capture section i see nrf sniffer for bluetooth LE  com 7 i start capturing 

    3) in this step i will open vscode and build my code which is shard with you previously that was build and flashed to board 

    4)after that i will go to wireshak tool i added the below filter  btle.advertising_address == 7c:d9:f4:14:05:a8 and whati received that screen shot  attached blow 



    so  i am not understaing what should i do next and aslo where is the problem in code i think in central code is not sending any connection request can you check and tell me , in code which part what should i change??

  • Hi,

    I do not see any connection attemt in the screenshot but the time between advertising packets sticks out as quite long and variabe. It might not be related, but I wonder what the advertising interval is and what environment you are testing in? Can you upload the full sniffer trace?

    Normally I would ask to configure the sniffer to follow any connection established for that advertiser as described here. Howeer, as the error is BT_HCI_ERR_UNKNOWN_CONN_ID, it is likely that the nRF neve rattempts to connect.

    Given that advertising packets are received quite rarely by the sniffer, I wonder if the timeout when establishing the connectiong is a problem? I see you do not set a specific timeout, which meanst hat the default timeout from CONFIG_BT_CREATE_CONN_TIMEOUT is used. Does it make a difference if you set this to a high value in your prj.conf? For instance CONFIG_BT_CREATE_CONN_TIMEOUT=60 would give a 60 second timeout.

Related