Ble eddystone and ibeacon simultaneously nrf52832

Hello, I am working on a project that consists of sending Eddystone and ibeacon packets simultaneously, to begin with, I started by sending one package of each separately, it was a success but the problem is that when I send both packages through my uc button,

it sends only the first packet while the second one is never sent, here's a screenshot for a better understanding :

I want to stop sending the first package (ibeacon) by pressing the button_1 (it works).

I want to send the second package (Eddystone) by pressing the button_2 (this one doesn't work) and yet it detects the button press.



Ps : the button is on a chip nordic nrf52832.

Parents Reply Children
  • Hi Abdullah,

    Thank you for your patience. There were some delays due to unforeseen sicknesses, but I finally completed the sample code I promised.

    There is one problem however, I haven't fully tested the behavior. I am working remotely, and my remote test setup is malfunctioning in some parts, so I am could not test the following:
    - Eddystone URL Service Data
    - Advertising Flags
    - Button handling (so I use a timer-based approach to change advertising data instead)

    So, if you want it now, below is the code. Otherwise, I will perform the necessary test next week when I return to office.

    Just create a new project using the <SDK root>/example/ble_peripheral/ble_app_template, replace main.c with the content below, and observe that the device should cycle between
    - 2 seconds of advertising as an iBeacon
    - 4 seconds of advertising as an Eddystone-URL beacon

    You can find the update implemented in update_gap_adv_data() and app_timer_handler().

    Below is the tested code that works as you expected. To test, just create a new project using the <SDK root>/example/ble_peripheral/ble_app_template, replace main.c with the content below.

    The expected behavior is:
    It starts up broadcasting as an iBeacon.
    Button 1 makes it broadcast as an iBeacon.
    Button 2 makes it broadcast as an Eddystone URL

    You can find the update implemented in update_gap_adv_data().

    I hope I included sufficient comments. Don't hesitate to let me know if you have any questions.

    Sorry again for the very long wait.

    /**
     * Copyright (c) 2014 - 2022, Nordic Semiconductor ASA
     *
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     *
     * 1. Redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     *
     * 2. Redistributions in binary form, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     *
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     *
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     *
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     *
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */
    /** @file
     *
     * @defgroup ble_sdk_app_template_main main.c
     * @{
     * @ingroup ble_sdk_app_template
     * @brief Template project main file.
     *
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <string.h>
    
    #include "nordic_common.h"
    #include "nrf.h"
    #include "app_error.h"
    #include "ble.h"
    #include "ble_hci.h"
    #include "ble_srv_common.h"
    #include "ble_advdata.h"
    #include "nrf_sdh.h"
    #include "nrf_sdh_soc.h"
    #include "nrf_sdh_ble.h"
    #include "app_timer.h"
    #include "bsp_btn_ble.h"
    #include "nrf_pwr_mgmt.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    
    #define DEAD_BEEF                       0xDEADBEEF                              /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
    
    
    /**@brief Callback function for asserts in the SoftDevice.
     *
     * @details This function will be called in case of an assert in the SoftDevice.
     *
     * @warning This handler is an example only and does not fit a final product. You need to analyze
     *          how your product is supposed to react in case of Assert.
     * @warning On assert from the SoftDevice, the system can only recover on reset.
     *
     * @param[in] line_num   Line number of the failing ASSERT call.
     * @param[in] file_name  File name of the failing ASSERT call.
     */
    void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
    {
        app_error_handler(DEAD_BEEF, line_num, p_file_name);
    }
    
    /**@brief Function for initializing power management.
     */
    static void power_management_init(void)
    {
        ret_code_t err_code;
        err_code = nrf_pwr_mgmt_init();
        APP_ERROR_CHECK(err_code);
    }
    
    
    /**@brief Function for handling the idle state (main loop).
     *
     * @details If there is no pending log operation, then sleep until next the next event occurs.
     */
    static void idle_state_handle(void)
    {
        if (NRF_LOG_PROCESS() == false)
        {
            nrf_pwr_mgmt_run();
        }
    }
    
    // ================================================================================================
    // Dummy function for quick browsing in SES
    static void BLE_INIT_SECTION(void) {}
    
    #define APP_BLE_OBSERVER_PRIO   3   /**< Application's BLE observer priority. You shouldn't need to modify this value. */
    #define APP_BLE_CONN_CFG_TAG    1   /**< A tag identifying the SoftDevice BLE configuration. */
    
    
    /**@brief Function for handling BLE events.
     *
     * @param[in]   p_ble_evt   Bluetooth stack event.
     * @param[in]   p_context   Unused.
     */
    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ret_code_t err_code = NRF_SUCCESS;
    
        switch (p_ble_evt->header.evt_id)
        {
            default:
                // No implementation needed.
                break;
        }
    }
    
    /**@brief Function for initializing the BLE stack.
     *
     * @details Initializes the SoftDevice and the BLE event interrupt.
     */
    static void ble_stack_init(void)
    {
        ret_code_t err_code;
    
        err_code = nrf_sdh_enable_request();
        APP_ERROR_CHECK(err_code);
    
        // Configure the BLE stack using the default settings.
        // Fetch the start address of the application RAM.
        uint32_t ram_start = 0;
        err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
        APP_ERROR_CHECK(err_code);
    
        // Enable BLE stack.
        err_code = nrf_sdh_ble_enable(&ram_start);
        APP_ERROR_CHECK(err_code);
    
        // Register a handler for BLE events.
        NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
    }
    
    
    // ================================================================================================
    // Dummy function for quick browsing in SES
    static void EDDYSTONE_URL_SECTION(void) {}
    
    
    static const uint16_t GOOGLE_LLC_EDDYSTONE_UUID16 = 0xFEAA;
    
    static const ble_uuid_t es_uuid =   
    {
        .uuid = GOOGLE_LLC_EDDYSTONE_UUID16,
        .type = BLE_UUID_TYPE_BLE
    };
    
    static ble_uuid_t es_uuid_array[] = { es_uuid };
    
    static uint8_t es_url_service_data_raw[] = "\x10\x00\x01nordicsemi\x00";
    /*                                      |   |   |             |-> \x00: Eddystone's special URL encoding 0x00, ".com"
                                            |   |   |-> \x03: URL Scheme 0x03, "https://www."
                                            |   |-> \x00: TX Power. If TX Power is not updated, it is 0 dBm
                                            |-> \x10: Frame Type 0x10, Eddystone URL
        Overall, the URL is "https://www.nordicsemi.com"
        Details of the byte encoding rules can be found at https://github.com/google/eddystone/tree/master/eddystone-url
    */
    
    /** @brief Buffer for storing an encoded advertising set. */
    static uint8_t es_encoded_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX];  
    
    /** @brief GAP buffer structs that contains pointers to the encoded advertising data buffers. */
    static ble_gap_adv_data_t es_url_gap_adv_data =
    {
        .adv_data =
        {
            .p_data = es_encoded_advdata,
            .len    = BLE_GAP_ADV_SET_DATA_SIZE_MAX
        },
        .scan_rsp_data =
        {
            .p_data = NULL,
            .len    = 0
    
        }
    };
    
    
    static void es_url_gap_adv_data_init(void)
    {
        ret_code_t err_code;
        
        // Helper "translator" struct for encoding human-readable adv data into GAP adv data buffers
        ble_advdata_t ble_advdata = {0};
    
        ble_advdata.flags =  (BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE | BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED);
    
        ble_advdata.uuids_complete.uuid_cnt =    1; // Only Eddystone UUID
        ble_advdata.uuids_complete.p_uuids =     es_uuid_array;
    
        ble_advdata_service_data_t t_service_data;
        t_service_data.service_uuid =   GOOGLE_LLC_EDDYSTONE_UUID16;
        t_service_data.data.size =      sizeof(es_url_service_data_raw) - 1;
        t_service_data.data.p_data =    es_url_service_data_raw;
        
        // Since there would be only one member in the array, just pass the service data's address instead
        ble_advdata.p_service_data_array = &t_service_data;
        ble_advdata.service_data_count = 1;
        
        // Encode the adv data into GAP adv data buffers
        err_code = ble_advdata_encode(&ble_advdata, es_url_gap_adv_data.adv_data.p_data, &es_url_gap_adv_data.adv_data.len);
    
        uint8_t i;
        NRF_LOG_INFO("es_url_gap_adv_data: len = %d", es_url_gap_adv_data.adv_data.len);
        NRF_LOG_HEXDUMP_INFO(es_url_gap_adv_data.adv_data.p_data, es_url_gap_adv_data.adv_data.len);
    
        APP_ERROR_CHECK(err_code);
    }
    
    
    // ================================================================================================
    // Dummy function for quick browsing in SES
    static void IBEACON_SECTION(void) {}
    
    
    #define APP_BEACON_INFO_LENGTH  0x17                        /**< Total length of information advertised by the Beacon. */
    
    #define APP_ADV_DATA_LENGTH     0x15                        /**< Length of manufacturer specific data in the advertisement. */
    #define APP_DEVICE_TYPE         0x02                        /**< 0x02 refers to Beacon. */
    #define APP_COMPANY_IDENTIFIER  0x0059                      /**< Company identifier for Nordic Semiconductor ASA. as per www.bluetooth.org. */
    #define APP_MAJOR_VALUE         0x01, 0x02                  /**< Major value used to identify Beacons. */
    #define APP_MINOR_VALUE         0x03, 0x04                  /**< Minor value used to identify Beacons. */
    #define APP_BEACON_UUID         0x01, 0x12, 0x23, 0x34, \
                                    0x45, 0x56, 0x67, 0x78, \
                                    0x89, 0x9a, 0xab, 0xbc, \
                                    0xcd, 0xde, 0xef, 0xf0      /**< Proprietary UUID for Beacon. */
    #define APP_MEASURED_RSSI       0xC3                        /**< The Beacon's measured RSSI at 1 meter distance in dBm. */
    
    
    static const uint8_t iBeacon_manuf_data_raw[APP_BEACON_INFO_LENGTH] = /**< Information advertised by the Beacon. */
    {
        APP_DEVICE_TYPE,     // Manufacturer specific information. Specifies the device type in this
                             // implementation.
        APP_ADV_DATA_LENGTH, // Manufacturer specific information. Specifies the length of the
                             // manufacturer specific data in this implementation.
        APP_BEACON_UUID,     // 128 bit UUID value.
        APP_MAJOR_VALUE,     // Major arbitrary value that can be used to distinguish between Beacons.
        APP_MINOR_VALUE,     // Minor arbitrary value that can be used to distinguish between Beacons.
        APP_MEASURED_RSSI    // Manufacturer specific information. The Beacon's measured TX power in
                             // this implementation.
    };
    
    static uint8_t iBeacon_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX];  /**< Buffer for storing an encoded advertising set. */
    
    /**@brief Struct that contains pointers to the encoded advertising data. */
    static ble_gap_adv_data_t iBeacon_gap_adv_data =
    {
        .adv_data =
        {
            .p_data = iBeacon_enc_advdata,
            .len    = BLE_GAP_ADV_SET_DATA_SIZE_MAX
        },
        .scan_rsp_data =
        {
            .p_data = NULL,
            .len    = 0
    
        }
    };
    
    
    static void iBeacon_gap_adv_data_init(void)
    {
        uint32_t      err_code;
        ble_advdata_t advdata;
        uint8_t       flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
    
        ble_advdata_manuf_data_t manuf_specific_data;
    
        manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
    
        manuf_specific_data.data.p_data = (uint8_t *) iBeacon_manuf_data_raw;
        manuf_specific_data.data.size   = APP_BEACON_INFO_LENGTH;
    
        // Build and set advertising data.
        memset(&advdata, 0, sizeof(advdata));
    
        advdata.name_type             = BLE_ADVDATA_NO_NAME;
        advdata.flags                 = flags;
        advdata.p_manuf_specific_data = &manuf_specific_data;
    
        err_code = ble_advdata_encode(&advdata, iBeacon_gap_adv_data.adv_data.p_data, &iBeacon_gap_adv_data.adv_data.len);
        APP_ERROR_CHECK(err_code);
    }
    
    
    // ================================================================================================
    // Dummy function for quick browsing in SES
    static void ADVERTISING_SECTION(void) {}
    
    
    static uint8_t              m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET; /**< Advertising handle used to identify an advertising set. */
    static ble_gap_adv_params_t m_adv_params;                                  /**< Parameters to be passed to the stack when starting advertising. */
    
    
    /**@brief Initialize advertising parameters (used when starting advertising).
     */
    static void adv_params_init(void)
    {
        memset(&m_adv_params, 0, sizeof(m_adv_params));
    
        m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
        m_adv_params.p_peer_addr     = NULL;                                    // Undirected advertisement.
        m_adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
        m_adv_params.interval        = BLE_GAP_ADV_INTERVAL_MIN;
        m_adv_params.duration        = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED;   // Never time out.
    }
    
    
    /**@brief Function for initializing the Advertising functionality.
     */
    static void advertising_init(void)
    {
        uint32_t      err_code;
        es_url_gap_adv_data_init();
        iBeacon_gap_adv_data_init();
        adv_params_init();
        
        //err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &es_url_gap_adv_data, &m_adv_params);
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &iBeacon_gap_adv_data, &m_adv_params);
        APP_ERROR_CHECK(err_code);
    }
    
    
    /**@brief Function for starting advertising.
     */
    static void advertising_start(void)
    {
        ret_code_t err_code;
    
        err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
        APP_ERROR_CHECK(err_code);
    
        err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
        APP_ERROR_CHECK(err_code);
    }
    
    
    // ================================================================================================
    // Dummy function for quick browsing in SES
    static void APP_TIMER_SECTION(void) {}
    
    
    APP_TIMER_DEF(app_timer_instance);
    #define APP_TIMER_PERIOD    APP_TIMER_TICKS(2000L)  // 2000 ms
    
    
    static void update_gap_adv_data(ble_gap_adv_data_t* p_gap_adv_data)
    {
        ret_code_t err_code;
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, p_gap_adv_data, NULL);
        
        // sd_ble_gap_adv_set_configure returns NRF_ERROR_INVALID_STATE in two cases:
        //  1. The 3rd parameter "p_adv_params" is not NULL, while advertising
        //  2. While advertising, the same data buffers (as the one being advertising) is provided
        // Using this function, case 1 is not possible.
        // Case 2 is acceptable. For example, if some advertising data is requested at a button press
        // Therefore, only perform error handling if the err_code is not NRF_ERROR_INVALID_STATE
        if (err_code != NRF_ERROR_INVALID_STATE)
        {
            APP_ERROR_CHECK(err_code);
        }
    
    }
    
    static void app_timer_handler(void * p_context)
    {
        
        static uint8_t x;
        NRF_LOG_INFO("%d", ++x);
    
        //switch (x % 3)
        //{
        //    case 0:
        //    {
        //        update_gap_adv_data(&iBeacon_gap_adv_data);
        //        NRF_LOG_INFO("iBeacon_gap_adv_data");
        //    }
        //    break;
        //    case 1:
        //    {
        //        update_gap_adv_data(&es_url_gap_adv_data);
        //        NRF_LOG_INFO("es_url_gap_adv_data");
        //    }
        //    break;
        //    case 2:
        //    {
        //        // Intentionally repeat a case to emulate the same button is pressed twice
        //        update_gap_adv_data(&es_url_gap_adv_data);
        //        NRF_LOG_INFO("es_url_gap_adv_data");
        //    }
        //    break;
        //}
    }
    
    
    /**@brief Function for the Timer initialization.
     *
     * @details Initializes the timer module. This creates and starts application timers.
     */
    static void app_timers_init(void)
    {
        ret_code_t err_code;
    
        err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    
        err_code = app_timer_create(&app_timer_instance, 
                                      APP_TIMER_MODE_REPEATED, 
                                      app_timer_handler);
        APP_ERROR_CHECK(err_code);
        
    }
    
    
    /**@brief Function for starting timers.
     */
    static void app_timers_start(void)
    {
           ret_code_t err_code;
           err_code = app_timer_start(app_timer_instance, 
                                        APP_TIMER_PERIOD, 
                                        NULL);
           APP_ERROR_CHECK(err_code);
    }
    
    // Dummy function for quick browsing in SES
    static void BUTTON_SECTION(void) {}
    
    /**@brief Function for handling events from the BSP module.
     *
     * @param[in]   event   Event generated when button is pressed.
     */
    static void bsp_event_handler(bsp_event_t event)
    {
        ret_code_t err_code;
    
        switch (event)
        {
            case BSP_EVENT_KEY_0:
                NRF_LOG_INFO("BSP_EVENT_KEY_0");
                update_gap_adv_data(&iBeacon_gap_adv_data);
                break;
            case BSP_EVENT_KEY_1:
                NRF_LOG_INFO("BSP_EVENT_KEY_1");
                update_gap_adv_data(&es_url_gap_adv_data);
                break;
            default:
                break;
        }
    }
    
    
    /**@brief Function for initializing buttons and leds.
     *
     * @param[out] p_erase_bonds  Will be true if the clear bonding button was pressed to wake the application up.
     */
    static void buttons_leds_init(void)
    {
        ret_code_t err_code;
        bsp_event_t startup_event;
    
        err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = bsp_btn_ble_init(NULL, &startup_event);
        APP_ERROR_CHECK(err_code);
    
    }
    
    
    /**@brief Function for initializing the nrf log module.
     */
    static void log_init(void)
    {
        ret_code_t err_code = NRF_LOG_INIT(NULL);
        APP_ERROR_CHECK(err_code);
    
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    }
    
    
    /**@brief Function for application main entry.
     */
    int main(void)
    {
        bool erase_bonds;
    
        // Initialize.
        log_init();
        app_timers_init();
        buttons_leds_init();
        power_management_init();
        ble_stack_init();
        advertising_init();
    
        // Start execution.
        NRF_LOG_INFO("Template example started.");
        app_timers_start();
    
        advertising_start();
    
        // Enter main loop.
        for (;;)
        {
            idle_state_handle();
        }
    }
    
    
    /**
     * @}
     */
    

    Hieu

  • Hi Abdullah,

    There was one missing line in the previous code after all. I fixed it and updated the behavior to work like you initially want. All information is edited into my previous comment.

    By the way, if you are curious, the missing line of code is:

        // Since there would be only one member in the array, just pass the service data's address instead
        ble_advdata.p_service_data_array = &t_service_data;
        
        // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        ble_advdata.service_data_count = 1; // <<<<<<<<<<<<<< This is the missing line
        // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        
        // Encode the adv data into GAP adv data buffers
        err_code = ble_advdata_encode(&ble_advdata, es_url_gap_adv_data.adv_data.p_data, &es_url_gap_adv_data.adv_data.len);
    

    Hieu

  • Hi Hieu,

    Thank you very much Hieu!!!!, you helped me a lot, sorry for the delay, I hope now you are well and not sick anymore. I wish you the best!!!!

    Best reagrds, THANK YOU VERY MUCH,

    Abdullah

  • Hi Abdullah,

    I am well now. Thank you for your kind words.

    Your reply was not late. It is me who have to apologize for the amount of time I took to resolve your question. Sorry again for that.

    I noticed you initially ask about how to read from an accelerometer and then add that data to an iBeacon? I assume you edited your comment and no longer wish to ask about that.

    In any cases, I can quickly tell you what I know on that topic.

    Regarding how to get data from your accelerometer, it is going to depends on the sensor's specifics. However, that topic is sufficiently different. If you need help with that, please open a new ticket.

    Regarding adding data to beacons, I can tell you that adding a few (<10) bytes of data to a beacon data is simple and doable.

    For iBeacon specifically, I am not sure it is allowed. I have not been able to find official documents from Apple about the iBeacon specifications other than their Getting Started guide. Therefore, I cannot claim whether adding something to its beacon data is allowed. Perhaps you could find develop support from Apple on that.

    This stackoverflow thread suggest that is allowed. However, while it could be OK for the current date, but in the future, iOS can be changed to judge such beacon data not iBeacon, and then not process your beacon properly. It is totally up to Apple's discretion.


    Please feel free to follow up if you have further questions regarding the same initial topic. If everything is fine now, it would be great if you can verify an answer Smiley

    Happy coding!

    Hieu

  • Hi Hieu

    No thank you very much and no I don't have any questions, you have answered all my questions, thank you very much,

    Good luck, I wish you the best !!!

    Abdullah

Related