|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Sean Kyer |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/bluetooth/bluetooth.h> |
| 9 | +#include <zephyr/bluetooth/hci.h> |
| 10 | +#include <zephyr/bluetooth/conn.h> |
| 11 | +#include <zephyr/bluetooth/uuid.h> |
| 12 | +#include <zephyr/bluetooth/gatt.h> |
| 13 | +#include <zephyr/bluetooth/services/ans.h> |
| 14 | + |
| 15 | +static uint8_t num_unread; |
| 16 | + |
| 17 | +static const struct bt_data ad[] = { |
| 18 | + BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), |
| 19 | + BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_ANS_VAL))}; |
| 20 | + |
| 21 | +static const struct bt_data sd[] = { |
| 22 | + BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1), |
| 23 | +}; |
| 24 | + |
| 25 | +static void connected(struct bt_conn *conn, uint8_t err) |
| 26 | +{ |
| 27 | + if (err) { |
| 28 | + printk("Connection failed, err 0x%02x %s\n", err, bt_hci_err_to_str(err)); |
| 29 | + } else { |
| 30 | + printk("Connected\n"); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +static void disconnected(struct bt_conn *conn, uint8_t reason) |
| 35 | +{ |
| 36 | + printk("Disconnected, reason 0x%02x %s\n", reason, bt_hci_err_to_str(reason)); |
| 37 | +} |
| 38 | + |
| 39 | +BT_CONN_CB_DEFINE(conn_callbacks) = { |
| 40 | + .connected = connected, |
| 41 | + .disconnected = disconnected, |
| 42 | +}; |
| 43 | + |
| 44 | +int main(void) |
| 45 | +{ |
| 46 | + int err; |
| 47 | + |
| 48 | + printk("Sample - Bluetooth Peripheral ANS\n"); |
| 49 | + |
| 50 | + err = bt_enable(NULL); |
| 51 | + if (err) { |
| 52 | + printk("Failed to enable bluetooth: %d\n", err); |
| 53 | + return err; |
| 54 | + } |
| 55 | + |
| 56 | + err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd)); |
| 57 | + if (err) { |
| 58 | + printk("Failed to start advertising: %d\n", err); |
| 59 | + return err; |
| 60 | + } |
| 61 | + |
| 62 | + num_unread = 0; |
| 63 | + |
| 64 | + while (true) { |
| 65 | + int ret; |
| 66 | + char msg[] = "Test Alert!\0"; |
| 67 | + |
| 68 | + ret = bt_ans_notify_new_alert(bt_ans_cat_simple_alert, 1, msg, |
| 69 | + strnlen(msg, BT_ANS_MAX_TEXT_STR_SIZE)); |
| 70 | + if (ret) { |
| 71 | + printk("Failed to push new alert\n"); |
| 72 | + } |
| 73 | + |
| 74 | + ret = bt_ans_set_unread_count(bt_ans_cat_simple_alert, ++num_unread); |
| 75 | + if (ret) { |
| 76 | + printk("Failed to push new unread count\n"); |
| 77 | + } |
| 78 | + |
| 79 | + k_sleep(K_SECONDS(5)); |
| 80 | + } |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
0 commit comments