Skip to content

Instantly share code, notes, and snippets.

@GatCode
Created February 18, 2022 11:27
Show Gist options
  • Save GatCode/ab1513545154fb53e1dd390a5ac2d30c to your computer and use it in GitHub Desktop.
Save GatCode/ab1513545154fb53e1dd390a5ac2d30c to your computer and use it in GitHub Desktop.
EXT ADV Sender/Receiver Problem
#include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h>
#include <bluetooth/bluetooth.h>
#define TIMEOUT_SYNC_CREATE K_SECONDS(10)
#define NAME_LEN 50
static bool per_adv_found;
static bt_addr_le_t per_addr;
static uint8_t per_sid;
static K_SEM_DEFINE(sem_per_adv, 0, 1);
static K_SEM_DEFINE(sem_per_sync, 0, 1);
static K_SEM_DEFINE(sem_per_sync_lost, 0, 1);
static void scan_recv(const struct bt_le_scan_recv_info *info,
struct net_buf_simple *buf)
{
if (!per_adv_found && info->interval) {
per_adv_found = true;
per_sid = info->sid;
bt_addr_le_copy(&per_addr, info->addr);
k_sem_give(&sem_per_adv);
}
}
static struct bt_le_scan_cb scan_callbacks = {
.recv = scan_recv,
};
static void sync_cb(struct bt_le_per_adv_sync *sync,
struct bt_le_per_adv_sync_synced_info *info)
{
k_sem_give(&sem_per_sync);
}
static void term_cb(struct bt_le_per_adv_sync *sync,
const struct bt_le_per_adv_sync_term_info *info)
{
k_sem_give(&sem_per_sync_lost);
}
static void recv_cb(struct bt_le_per_adv_sync *sync,
const struct bt_le_per_adv_sync_recv_info *info,
struct net_buf_simple *buf)
{
printk("Hello From recv_cb\n");
}
static struct bt_le_per_adv_sync_cb sync_callbacks = {
.synced = sync_cb,
.term = term_cb,
.recv = recv_cb
};
void main(void)
{
struct bt_le_per_adv_sync_param sync_create_param;
struct bt_le_per_adv_sync *sync;
int err;
/* Initialize the Bluetooth Subsystem */
err = bt_enable(NULL);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return;
}
bt_le_scan_cb_register(&scan_callbacks);
bt_le_per_adv_sync_cb_register(&sync_callbacks);
err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, NULL);
if (err) {
printk("failed (err %d)\n", err);
return;
}
do {
per_adv_found = false;
err = k_sem_take(&sem_per_adv, K_FOREVER);
if (err) {
printk("failed (err %d)\n", err);
return;
}
printk("Found periodic advertising.\n");
bt_addr_le_copy(&sync_create_param.addr, &per_addr);
sync_create_param.options = 0;
sync_create_param.sid = per_sid;
sync_create_param.skip = 0;
sync_create_param.timeout = 0xa;
err = bt_le_per_adv_sync_create(&sync_create_param, &sync);
if (err) {
printk("failed (err %d)\n", err);
return;
}
printk("Successfully found periodic advertising sync.\n");
printk("Waiting for periodic sync...\n");
err = k_sem_take(&sem_per_sync, TIMEOUT_SYNC_CREATE);
if (err) {
printk("failed (err %d)\n", err);
printk("Deleting Periodic Advertising Sync...");
err = bt_le_per_adv_sync_delete(sync);
if (err) {
printk("failed (err %d)\n", err);
return;
}
continue;
}
printk("Periodic sync established.\n");
printk("Waiting for periodic sync lost...\n");
err = k_sem_take(&sem_per_sync_lost, K_FOREVER);
if (err) {
printk("failed (err %d)\n", err);
return;
}
printk("Periodic sync lost.\n");
} while (true);
}
#include <bluetooth/bluetooth.h>
static uint8_t adv_data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
#define ADV_DATA_LEN 20
static const struct bt_data ad[] = {
BT_DATA(BT_DATA_MANUFACTURER_DATA, adv_data, ADV_DATA_LEN),
};
void main(void)
{
struct bt_le_ext_adv *adv;
int err;
/* Initialize the Bluetooth Subsystem */
err = bt_enable(NULL);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return;
}
/* Create a non-connectable non-scannable advertising set */
err = bt_le_ext_adv_create(BT_LE_EXT_ADV_NCONN_NAME, NULL, &adv);
if (err) {
printk("Failed to create advertising set (err %d)\n", err);
return;
}
/* Set periodic advertising parameters */
err = bt_le_per_adv_set_param(adv, BT_LE_PER_ADV_DEFAULT);
if (err) {
printk("Failed to set periodic advertising parameters"
" (err %d)\n", err);
return;
}
/* Set periodic advertising data */
printk("Set Periodic Advertising Data...");
err = bt_le_per_adv_set_data(adv, ad, ARRAY_SIZE(ad));
if (err) {
printk("Failed (err %d)\n", err);
return;
}
printk("done.\n");
/* Enable Periodic Advertising */
err = bt_le_per_adv_start(adv);
if (err) {
printk("Failed to enable periodic advertising (err %d)\n", err);
return;
}
printk("Start Extended Advertising...");
err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
if (err) {
printk("Failed to start extended advertising "
"(err %d)\n", err);
return;
}
printk("done.\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment