| /* |
| * Copyright (c) 2024 Croxel, Inc. |
| * |
| * SPDX-License-Identifier: Apache-2.0 |
| */ |
| |
| #include <zephyr/kernel.h> |
| #include <zephyr/bluetooth/bluetooth.h> |
| #include <zephyr/bluetooth/services/nus.h> |
| |
| #define DEVICE_NAME CONFIG_BT_DEVICE_NAME |
| #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) |
| |
| 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), |
| }; |
| |
| static const struct bt_data sd[] = { |
| BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_SRV_VAL), |
| }; |
| |
| static void notif_enabled(bool enabled, void *ctx) |
| { |
| ARG_UNUSED(ctx); |
| |
| printk("%s() - %s\n", __func__, (enabled ? "Enabled" : "Disabled")); |
| } |
| |
| static void received(struct bt_conn *conn, const void *data, uint16_t len, void *ctx) |
| { |
| char message[CONFIG_BT_L2CAP_TX_MTU + 1] = ""; |
| |
| ARG_UNUSED(conn); |
| ARG_UNUSED(ctx); |
| |
| memcpy(message, data, MIN(sizeof(message) - 1, len)); |
| printk("%s() - Len: %d, Message: %s\n", __func__, len, message); |
| } |
| |
| struct bt_nus_cb nus_listener = { |
| .notif_enabled = notif_enabled, |
| .received = received, |
| }; |
| |
| int main(void) |
| { |
| int err; |
| |
| printk("Sample - Bluetooth Peripheral NUS\n"); |
| |
| err = bt_nus_cb_register(&nus_listener, NULL); |
| if (err) { |
| printk("Failed to register NUS callback: %d\n", err); |
| return err; |
| } |
| |
| err = bt_enable(NULL); |
| if (err) { |
| printk("Failed to enable bluetooth: %d\n", err); |
| return err; |
| } |
| |
| err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd)); |
| if (err) { |
| printk("Failed to start advertising: %d\n", err); |
| return err; |
| } |
| |
| printk("Initialization complete\n"); |
| |
| while (true) { |
| const char *hello_world = "Hello World!\n"; |
| |
| k_sleep(K_SECONDS(3)); |
| |
| err = bt_nus_send(NULL, hello_world, strlen(hello_world)); |
| printk("Data send - Result: %d\n", err); |
| |
| if (err < 0 && (err != -EAGAIN) && (err != -ENOTCONN)) { |
| return err; |
| } |
| } |
| |
| return 0; |
| } |