Bluetooth advertise

This commit is contained in:
2023-11-04 02:28:32 +01:00
parent 981e949cf1
commit 953ea2a25a
5 changed files with 67 additions and 19 deletions

View File

@@ -3,4 +3,4 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(hellobt)
target_sources(app PRIVATE src/main.cpp)
target_sources(app PRIVATE src/main.c)

View File

@@ -4,6 +4,7 @@
/dts-v1/;
#include <nordic/nrf52840_qiaa.dtsi>
// https://wiki.makerdiary.com/nrf52840-mdk-usb-dongle/getting-started/
/ {
model = "nrf52840_test";
compatible = "nrf52840-test";

View File

@@ -11,3 +11,8 @@ CONFIG_BT=y
CONFIG_BT_HCI=y
CONFIG_BT_CTLR=y
CONFIG_BT_LL_SW_SPLIT=y
CONFIG_BT_DEVICE_NAME="HelloBT_Peripheral"
CONFIG_BT_PERIPHERAL=y
# Enable print for float
CONFIG_CBPRINTF_FP_SUPPORT=y

60
src/main.c Normal file
View File

@@ -0,0 +1,60 @@
#include "zephyr/kernel.h"
#include "zephyr/drivers/gpio.h"
#include "zephyr/random/rand32.h"
#include "zephyr/bluetooth/bluetooth.h"
#include "zephyr/bluetooth/uuid.h"
#include "zephyr/bluetooth/addr.h"
#include "zephyr/bluetooth/hci.h"
#include <stdio.h>
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
#define COMPANY_ID_CODE 0x0059
// static struct bt_le_adv_param* adv_param = BT_LE_ADV_PARAM(
// BT_LE_ADV_OPT_NONE,
// 800,
// 801,
// NULL
// );
static uint8_t url_data[] = {
0x17, '/', '/', 'n', 'r', 'z', '.', 's', 'e'
};
// Advertisment data
static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
};
// Scan response packet
static const struct bt_data sd[] = {
BT_DATA(BT_DATA_URI, url_data, sizeof(url_data)),
// BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_128_ENCODE(0x00001523, 0x1212, 0xefde, 0x1523, 0x785feabcd123)),
};
int main() {
int err;
err = bt_enable(NULL);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return 1;
}
printk("Bluetooth initialised!\n");
err = bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
if (err) {
printk("Advertising failed to start (err %d)\n", err);
return 1;
}
printk("Advertising successfully started!\n");
while (true) {
float value = (float)(sys_rand32_get() % 1000)/1000.0f;
printk("%u: Hello, World! %.3f\n", k_uptime_get_32(), value);
k_msleep(250);
}
return 0;
}

View File

@@ -1,18 +0,0 @@
#include "zephyr/kernel.h"
#include "zephyr/drivers/gpio.h"
#include "zephyr/random/rand32.h"
#define LED_NODE DT_ALIAS(led0)
static gpio_dt_spec led = GPIO_DT_SPEC_GET(LED_NODE, gpios);
auto main() -> int {
if (!gpio_is_ready_dt(&led)) return 1;
if (gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE) < 0) return 1;
gpio_pin_set_dt(&led, 0);
while (true) {
printk("Hello, World! %d - %u\n", 100'000, sys_rand32_get());
k_msleep(250);
}
return 0;
}