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
  • Hi Abdullah_M,

    Could you elaborate on what your implementation to send both packets simultaneously is?

    Best regards,

    hieu

  • I send them simultaneously because I wanted to send a message, let's say advertising, to both android and apple users. If I send only Eddystone for example, I will only reach android users, likewise for ibeacon, I will only reach apple users. Whereas if I send both, I could reach both. Here is the code if you want to see how it looks like.

  • Hi Abdullah,

    Your English is just fine, please don't worry.

    Sorry for the long wait. The past couple of days I have spent a bit of time to look into the Eddystone specs and our Eddystone library. I found that it is not trivial to fully understand the library at all, mainly due to the security features associated with Eddystone-EID.

    On the other hand, the nRF5 SDK is in maintenance mode (no update, bug fix) since Aug 2021, and all platforms related to Eddystone is completely shut down. That means that further investigation on this is not very beneficial for either of us; and I cannot afford more time than I have.

    So instead of showing you how toggling to an Eddystone advertising packet using the SDK's library can be done, I can show you how to build an Eddystone-UID and/or an Eddystone-URL packet from the ground up; and I can show you how to toggle between them and an iBeacon.

    I'm sorry for the long wait and disappointment, but how does this sound to you?

    By the way, as you are new to the technology and are probably not bound to any specific SDK, what do you think about getting started on the nRF Connect SDK  (NCS) instead? That is our new SDK that is being actively working on. Newer generation chips from Nordic will also be supported on NCS, like the nRF53 that is otherwise not supported in the nRF5 SDK.

    Best regards,

    Hieu

  • Hi Hieu,

    I hope you are well, thank you for the compliment even if I think my English is not very good,
    No problem to wait, I rather thank you for dedicating your time on me.

    Yes, I will be very grateful if you show me how to switch between Eddystone and ibeacon because that is where I am stuck.


    In fact, when I send the Eddystone packets, the packets contain the values of the rangefinder(telemeter) that is displayed on the app (Kbeacon or nrf connect with bluetooth enabled). So when I send the packet I can see Eddystone URL on the app (you have to wait maybe a few minutes to see them), but the problem is this:

    imagine that if I send ibeacon packets first, ibeacon will work, then if I send eddystone packets, the code indicates that they were sent but on the application there is nothing that's means eddystone did not work. 

    on the other hand imagine now that if I send eddystone packets first, eddystone will work, then if I send ibeacon packets the code shows that it was sent but on the application there is nothing that means ibeacon didn't work. 

    by the way, I change the mode by pressing button 1 to stop sending the first packets and button 3 to start sending the second packets.

    I use nrf52832 and PCA10040, is it ok or do I have to change the card?


    Best Regards.

    Abdullah

  • Hi Abdullah,

    Just want to let you know that I still have your case on my book, just trying to get through my backlog to it, since I want to make sure what I give you work.

    And just to set expectation straight. I will not be using the nRF5 SDK Eddystone Library. Instead, I will just use raw advertising or the Advertisement Module to recreate an Eddystone-UID or Eddystone-URL packet.

    Sorry again for the very long wait.

    Best regards,

    Hieu

  • Hi Hieu,

    No problem with the wait, thank you very much.

    I was wondering why you are not using the Eddystone library from the nRF5 SDK. Is it because nrf52832 is compatible or not with something?

    Or because you don't have the Eddystone library of the nRF5 SDK?

    And the other question is, why use raw advertising or the Advertisement Module to recreate an Eddystone-UID or Eddystone-URL packet.?

    Best regards.

    Abdullah

  • Hi Abdullah,

    The Eddystone Library is pretty complicated. The complication is mostly for supporting the Eddystone-EID and Encrypted Eddystone-TLM features.

    Meanwhile, the Eddystone protocol is completely deprecated. The nRF5 SDK, along with the Eddystone library, is no longer recommended for new projects.

    Therefore, the cost of learning it is pretty high, while the benefit is pretty low.

    On the other hand, I can quickly use other basic advertising data to create an Eddystone-UID or Eddystone-URL beacon, because their specs are pretty simple.
    As you are already creating your own iBeacon data, I believe you are also more than capable of creating your Eddystone-UID or Eddystone-URL beacons. What I do is going to just be showing you how to toggle between different advertising data.

    Best regards,

    Hieu

Reply
  • Hi Abdullah,

    The Eddystone Library is pretty complicated. The complication is mostly for supporting the Eddystone-EID and Encrypted Eddystone-TLM features.

    Meanwhile, the Eddystone protocol is completely deprecated. The nRF5 SDK, along with the Eddystone library, is no longer recommended for new projects.

    Therefore, the cost of learning it is pretty high, while the benefit is pretty low.

    On the other hand, I can quickly use other basic advertising data to create an Eddystone-UID or Eddystone-URL beacon, because their specs are pretty simple.
    As you are already creating your own iBeacon data, I believe you are also more than capable of creating your Eddystone-UID or Eddystone-URL beacons. What I do is going to just be showing you how to toggle between different advertising data.

    Best regards,

    Hieu

Children
  • Hi Hieu,

    Ok I understand, let's do it like this then, Yes please I want to know how to switch between eddystone advertisement and ibeacon advertisement.

    Again, thank you very much,

    Best regards,

    Abdullah

  • Hi Abdullah,

    Just want to let you know that I have started working on it. There are a few issues, I will hopefully be done soon.

    I was using my colleague's guide here:  How to update advertising data dynamically using BLE Advertising library 

    It turned out the library that he used is only for connectable advertising. Here we are using beacon, so that method is not applicable.

    However, I still share the link, because that example shows a way to change advertising data during connectable advertising.

    I will have to redo this work using the basic API. However, I am out of office tomorrow, so I will resume on next Monday, unless I find some time in the weekend.

    Sorry again for the wait. It is quite bad, this is not supposed to be a major workload.

    Hieu

  • Hi Hieu,

    No problem, take your time, and thanks again.

    Best regards,

    Abdullah

  • 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

Related