Hello, I really need some help to connect a central nRF52 DK board to a peripheral that was provided to me. This peripheral can connect to a smartphone using nRF Connect and a passkey. I have the following setup:
#include <zephyr/kernel.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <bluetooth/services/nus.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/settings/settings.h>
#include <string.h>
#define DEVICE_NAME "SB-3100043"
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
#define PASSKEY 290156 // Predefined passkey
static struct bt_conn *default_conn;
/* Callback for data reception */
static void received(struct bt_conn *conn, const uint8_t *const data, uint16_t len)
{
char message[CONFIG_BT_L2CAP_TX_MTU + 1] = "";
memcpy(message, data, MIN(len, sizeof(message) - 1));
printk("Received: %s\n", message);
}
/* Callback structure for NUS */
static struct bt_nus_cb nus_listener = {
.received = received,
};
/* Passkey display callback */
static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printk("Passkey for %s: %06u\n", addr, passkey);
}
/* Passkey entry callback */
static void auth_passkey_entry(struct bt_conn *conn, unsigned int passkey)
{
printk("Passkey entry required. Entering predefined passkey.\n");
int err = bt_conn_auth_passkey_entry(conn, PASSKEY);
if (err) {
printk("Failed to enter passkey (err %d)\n", err);
} else {
printk("Passkey entry successful\n");
}
}
/* Cancel pairing callback */
static void auth_cancel(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printk("Pairing cancelled: %s\n", addr);
}
/* Pairing confirm callback */
static void auth_pairing_confirm(struct bt_conn *conn)
{
bt_conn_auth_pairing_confirm(conn);
printk("Pairing confirmed\n");
}
/* Security change callback */
static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
if (!err) {
printk("Security changed: %s level %u\n", addr, level);
} else {
printk("Security failed: %s level %u err %d\n", addr, level, err);
}
}
static struct bt_conn_auth_cb auth_cb_display = {
.passkey_display = auth_passkey_display,
.passkey_entry = auth_passkey_entry,
.cancel = auth_cancel,
.pairing_confirm = auth_pairing_confirm,
};
/* Connection callback */
static void connected(struct bt_conn *conn, uint8_t err)
{
if (err) {
printk("Connection failed (err %u)\n", err);
return;
}
printk("Connected\n");
default_conn = bt_conn_ref(conn);
// Request security level 3 (legacy pairing with encryption)
int sec_err = bt_conn_set_security(conn, BT_SECURITY_L3);
if (sec_err) {
printk("Failed to set security level 3 (err %d)\n", sec_err);
} else {
printk("Security level 3 requested successfully\n");
}
}
/* Disconnection callback */
static void disconnected(struct bt_conn *conn, uint8_t reason)
{
printk("Disconnected (reason %u)\n", reason);
if (default_conn) {
bt_conn_unref(default_conn);
default_conn = NULL;
}
}
static struct bt_conn_cb conn_callbacks = {
.connected = connected,
.disconnected = disconnected,
.security_changed = security_changed,
};
/* Scan callback to match a specific device */
static void scan_device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type, struct net_buf_simple *ad)
{
char addr_str[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
printk("Device found: %s, RSSI: %d\n", addr_str, rssi);
if (adv_type != BT_GAP_ADV_TYPE_ADV_IND) {
return;
}
for (int i = 0; i < ad->len;) {
uint8_t len = ad->data[i++];
if (len == 0 || i + len - 1 > ad->len) {
printk("Invalid advertisement data length\n");
break;
}
uint8_t type = ad->data[i++];
if (type == BT_DATA_NAME_COMPLETE) {
char found_name[DEVICE_NAME_LEN + 1];
memcpy(found_name, &ad->data[i], len - 1);
found_name[len - 1] = '\0';
printk("Found device name: %s\n", found_name);
if (len - 1 == DEVICE_NAME_LEN &&
memcmp(&ad->data[i], DEVICE_NAME, DEVICE_NAME_LEN) == 0) {
printk("Matching device found: %s. Attempting to connect...\n", addr_str);
if (default_conn) {
printk("Connection already in progress or established\n");
return;
}
struct bt_le_conn_param *conn_param = BT_LE_CONN_PARAM_DEFAULT;
// Stop scanning before attempting to connect
bt_le_scan_stop();
int err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, conn_param, &default_conn);
if (err) {
printk("Failed to create connection (err %d)\n", err);
} else {
printk("Connection successfully created\n");
}
return;
}
}
i += len - 1;
}
}
int main(void)
{
int err;
printk("Starting Nordic UART Service with Bonding\n");
// Initialize Bluetooth
err = bt_enable(NULL);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return err;
}
// Clear any stale bonding information
bt_unpair(BT_ID_DEFAULT, NULL);
printk("Cleared all bonding information\n");
// Load settings
err = settings_load();
if (err) {
printk("Settings load failed (err %d)\n", err);
return err;
}
// Register connection and authentication callbacks
bt_conn_cb_register(&conn_callbacks);
bt_conn_auth_cb_register(&auth_cb_display);
// Register NUS service
err = bt_nus_init(&nus_listener);
if (err) {
printk("Failed to init NUS (err %d)\n", err);
return err;
}
printk("Bluetooth initialized\n");
// Start scanning
err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, scan_device_found);
if (err) {
printk("Scanning failed to start (err %d)\n", err);
return err;
}
printk("Scanning started\n");
while (true) {
k_sleep(K_SECONDS(3));
}
}
And the error I keep getting is: Device found: EB:E1:BD:6F:9F:82 (random), RSSI: -56Found device name: SB-3100043
Matching device found: EB:E1:BD:6F:9F:82 (random). Attempting to connect...
Connection successfully created
Connected
Security level 3 requested successfully
[00:00:05.337,890] [1;31m<err> bt_smp: pairing failed (peer reason 0x8)[0m
Security failed: EB:E1:BD:6F:9F:82 (random) level 1 err 9