Creating Ble Service with Nrf connect

I have a problem to advertise a service. When I include a header file, an extra service is automatically created. It seems like that the service will be advertised twice. Everything in the main code is running once and everything in the header file is running two times. Do you know the problem? Below you can see all the codes.

5050.main_mwe.cpp

8182.blewrap.cpp

  • #ifndef BLE_WRAP_H
    #define BLE_WRAP_H
    
    #include <settings/settings.h>
    #include <bluetooth/bluetooth.h>
    #include <bluetooth/hci.h>
    #include <bluetooth/conn.h>
    #include <bluetooth/uuid.h>
    #include <bluetooth/gatt.h>
    
    static char char_deviceName[11] = {'F', 'o', 'o', ' ', '0', '0', '0', '0', '0', '3', '\0'};
    static const struct bt_data ad[] = {
        BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    };
    
    static uint8_t bufferTemperature[4];
    static uint8_t bufferhumidity[4];
    
    static struct bt_conn *connection;
    
    static void connected(struct bt_conn *conn, uint8_t err)
    {
        if(connection != NULL)
        {
            printk("Connection already established\n");
            return;
        }
    
        if (err)
        {
            printk("Connection failed (err 0x%02x)\n", err);
        }
        else
        {
            connection = conn;      
            printk("Connected\n");
        }
    }
    
    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
        printk("Disconnected (reason 0x%02x)\n", reason);
        connection = nullptr;   
    }
    
    BT_CONN_CB_DEFINE(conn_callbacks) = {
        .connected = connected,
        .disconnected = disconnected,
    };
    
    static struct bt_le_adv_param advertisingParam;
    
    class BleWrap
    {
    public:
        static BleWrap *getInstance(void);
        ~BleWrap();
        void init();
    
        static ssize_t readBytes(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset);
    
    protected:
        BleWrap(void);
        static BleWrap *_pInstance;
    };
    
    BT_GATT_SERVICE_DEFINE(tempHuMService,
                           BT_GATT_PRIMARY_SERVICE(BT_UUID_ESS),
                           BT_GATT_CHARACTERISTIC(BT_UUID_TEMPERATURE, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, BleWrap::readBytes, NULL, &bufferTemperature),
                           BT_GATT_CHARACTERISTIC(BT_UUID_HUMIDITY, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, BleWrap::readBytes, NULL, &bufferhumidity), );
    
    #endif

    This is the header file

  • CONFIG_SOC_SERIES_NRF52X=y
    CONFIG_SOC_NRF52833_QIAA=y
    CONFIG_ARM_MPU=y
    CONFIG_HW_STACK_PROTECTION=y
    CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y
    
    CONFIG_DEBUG_THREAD_INFO=n
    CONFIG_DEBUG_OPTIMIZATIONS=n
    
    # enable segger comm?
    CONFIG_USE_SEGGER_RTT=y
    CONFIG_SERIAL=y
    CONFIG_CONSOLE=y
    CONFIG_STDOUT_CONSOLE=y
    
    CONFIG_RTT_CONSOLE=y
    
    CONFIG_CBPRINTF_FP_SUPPORT=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
    CONFIG_RTTI=y
    CONFIG_LOG=y
    
    CONFIG_MINIMAL_LIBC=n
    CONFIG_CPLUSPLUS=y
    CONFIG_STD_CPP11=y
    CONFIG_NEWLIB_LIBC=y
    CONFIG_LIB_CPLUSPLUS=y
    
    # BLE
    CONFIG_BT=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DIS=y
    CONFIG_BT_GATT_DYNAMIC_DB=y
    CONFIG_BT_DEVICE_NAME="Foo 000000"
    CONFIG_BT_DEVICE_NAME_DYNAMIC=y
    CONFIG_BT_DEVICE_NAME_MAX=11
    #0x0540 = 1344 generic sensor 0x0552 =1362 multisensor
    CONFIG_BT_DEVICE_APPEARANCE=1344
    CONFIG_BT_DIS_PNP=n
    CONFIG_BT_DEBUG_LOG=n
    CONFIG_BT_HCI=y
    CONFIG_BT_CTLR=y
    CONFIG_BT_SMP=y
    CONFIG_BT_SIGNING=y
    CONFIG_BT_ATT_PREPARE_COUNT=5
    # CONFIG_BT_IAS=y
    CONFIG_BT_PRIVACY=y
    CONFIG_BT_KEYS_OVERWRITE_OLDEST=y
    CONFIG_BT_SETTINGS=n
    CONFIG_SETTINGS=n
    CONFIG_BT_BUF_ACL_TX_SIZE=64

    This is the prj.config file

  • # SPDX-License-Identifier: Apache-2.0
    
    cmake_minimum_required(VERSION 3.20.0)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(peripheral_hr_cpp C CXX)
    
    target_sources(app PRIVATE
      src/main_mwe.cpp src/blewrap.h src/blewrap.cpp
      )
    
    

    And this is the cmakelist

  • Hi Mark,

    Thank you for contacting DevZone at NordicSemi. 

    Based on the information you have provided; I have created the project including these files and settings.

    I changed few settings as per my DK (NRF52840DK). I could build, flash, and see the output from this project.

    Here is the output as seen on the NRF Terminal:

    Do you see the same output?

    What do you mean by "everything in the header file is running two times". What specifically you mean by this? You have a class defination and few functions defined in the header file.

    Can you please elaborate more on it (with example / output etc.)? Also, with is the expected output / behavior and what actually is happening.

    Regards,

    Naeem

  • Thank you for youre response. I have the same output as you. The problem is when I connect the device ( which called "Foo 000003") in de NRF connect app I will see two times a "Environmental Sensing". But I only make it once. I have test a code with only a "#include "blewrapper.h". Then i see 3 times a  "Environmental Sensing" in the NRF connect app. So it looked like something is going wrong with the includes. Do you know the solution for this?

Related