I'm new to nRF
have done the "nRF Connect SDK Fundamentals", course and have create a custom characteristic to read and write but I cannot get notification to work.
I'm using nRF Connect SDK (vsCode) and nRFConnect to view the BLE output on my iPhone.
I'm based this off zephyr/samples/bluetooth/peripheral and just removed code that I don't need and added a bit. I thought that I was just following the heart rate code with slight changes but I must be missing something. In the nRfConnect app I press the two down arrows and nothing happens to the Value. I'm firing of a notification every second.
If anyone has an example for a custom notification that I can study that would be great, or with the code below can see what I am missing that would be great also.
Also how would I debug this?
/* main.c - Application main entry point */ /* * Copyright (c) 2015-2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/types.h> #include <stddef.h> #include <string.h> #include <errno.h> #include <zephyr/sys/printk.h> #include <zephyr/sys/byteorder.h> #include <zephyr/zephyr.h> #include <zephyr/settings/settings.h> #include <zephyr/bluetooth/bluetooth.h> #include <zephyr/bluetooth/hci.h> #include <zephyr/bluetooth/conn.h> #include <zephyr/bluetooth/uuid.h> #include <zephyr/bluetooth/gatt.h> // NOTIFICATION Service #define BT_UUID_NOTIFICATION_SERVICE_VAL BT_UUID_128_ENCODE(0x12343333, 0x1234, 0x5678, 0x1234, 0x56789abcdef0) static struct bt_uuid_128 notification_uuid = BT_UUID_INIT_128(BT_UUID_NOTIFICATION_SERVICE_VAL); // characteristic information static struct bt_uuid_128 adc_notify_uuid = BT_UUID_INIT_128( BT_UUID_128_ENCODE(0x12345611, 0x1234, 0x5678, 0x1234, 0x56789abcdef1)); #define VND_MAX_LEN 20 // define Notification Service BT_GATT_SERVICE_DEFINE(adc_svc, BT_GATT_PRIMARY_SERVICE(¬ification_uuid), BT_GATT_CHARACTERISTIC(&adc_notify_uuid.uuid, BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_NONE, NULL, NULL, NULL), ); // load in the advertising services so we can see whats available static const struct bt_data advertisingData[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NOTIFICATION_SERVICE_VAL), }; static void connected(struct bt_conn *conn, uint8_t err) { if (err) { printk("Connection failed (err 0x%02x)\n", err); } else { printk("Connected\n"); } } static void disconnected(struct bt_conn *conn, uint8_t reason) { printk("Disconnected (reason 0x%02x)\n", reason); } BT_CONN_CB_DEFINE(conn_callbacks) = { .connected = connected, .disconnected = disconnected, }; // ble had been enabled and not we start advertising static void bt_ready(void) { int err; printk("Bluetooth initialized\n"); if (IS_ENABLED(CONFIG_SETTINGS)) { settings_load(); } //start advertising err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, advertisingData, ARRAY_SIZE(advertisingData), NULL, 0); if (err) { printk("Advertising failed to start (err %d)\n", err); return; } printk("Advertising successfully started\n"); } static void notify_adc(void){ printk("notify adc2 "); static uint8_t level[VND_MAX_LEN + 1] = { 'a', 'b', 'a', 'a', '8', 'a', 'a', 'a'}; int rc; rc = bt_gatt_notify(NULL, &adc_svc.attrs[1], &level, sizeof(level)); return rc == -ENOTCONN ? 0 : rc; } void main(void) { printk("testing 20221118"); int err; err = bt_enable(NULL); if (err) { printk("Bluetooth init failed (err %d)\n", err); return; } bt_ready(); while (1) { k_sleep(K_SECONDS(1)); notify_adc(); } }
and my prj.conf is
# Increased stack due to settings API usage CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048 CONFIG_BT=y CONFIG_BT_DEBUG_LOG=y CONFIG_BT_SMP=y CONFIG_BT_SIGNING=y CONFIG_BT_PERIPHERAL=y CONFIG_BT_DIS=y CONFIG_BT_ATT_PREPARE_COUNT=5 #CONFIG_BT_BAS=n #CONFIG_BT_HRS=n #CONFIG_BT_IAS=n CONFIG_BT_PRIVACY=y CONFIG_BT_DEVICE_NAME="Notification A1" CONFIG_BT_DEVICE_APPEARANCE=833 CONFIG_BT_DEVICE_NAME_DYNAMIC=y CONFIG_BT_DEVICE_NAME_MAX=65 CONFIG_BT_KEYS_OVERWRITE_OLDEST=y CONFIG_BT_SETTINGS=y CONFIG_FLASH=y CONFIG_FLASH_PAGE_LAYOUT=y CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y # setup the debugging CONFIG_USE_SEGGER_RTT=y CONFIG_RTT_CONSOLE=y CONFIG_UART_CONSOLE=n