This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Beacon adevrtising switch over while button pressing?

Hi,

I have been trying for switching advertising parameters like m_beacon_info1[6]=0x00 to 0xff when button pressing.After 10 advertising ,the parameter back to normal as m_beacon_info1[8]=0x00. as I m working with nrf52840 sdk15.2 BLE_APP_BEACON. 

While button is pressed, i stopped ble advertisement and re initiate the beacon parameter from  0xff to 0x00 then start advertising with new value.it works fine .

My problem is after 10 advertising it wont back to initial stage ie m_beacon_info1[6]=0x00 .

#include "app_timer.h"
#include "ble_advdata.h"
#include "bsp.h"
#include "nordic_common.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "nrf_soc.h"
#include <stdbool.h>
#include <stdint.h>
#include "nrf_drv_gpiote.h"
#include "nrf_drv_gpiote.h"
#include "nrf_drv_gpiote.h"
#include "ble_srv_common.h"
#include "boards.h"
#include "nrf_delay.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
 
#define APP_BLE_CONN_CFG_TAG 1                                              // Tag identifying the BLE configuration.    
#define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(500, UNIT_0_625_MS)      // iBeacon specifies an advertising interval of 100ms
#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 0x004C                                       // Company identifier for Nordic Semiconductor 

#define DEAD_BEEF 0xDEADBEEF                                                // Value used as error code on stack dump
uint32_t mac_addr[];                                                        
uint8_t service_uid[17];
uint8_t *service_uid_pointer;
uint8_t m_beacon_info1[APP_BEACON_INFO_LENGTH]={0};
uint8_t serial_number[4];
uint32_t test_sno;

static ble_gap_adv_params_t m_adv_params;                                      // Parameters to be passed to the stack when starting advertising. 
static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;                  // Advertising handle used to identify an advertising set. 
static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX];                   // Buffer for storing an encoded advertising set. 


bool is_advertising = false;

#ifdef BSP_BUTTON_0
    #define PIN_IN BSP_BUTTON_0
#endif
#ifndef PIN_IN
    #error "Please indicate input pin"
#endif

#ifdef BSP_LED_0
    #define PIN_OUT BSP_LED_0
#endif
#ifndef PIN_OUT
    #error "Please indicate output pin"
#endif


      /*Encoded advertising data. */
static ble_gap_adv_data_t m_adv_data =
    {
        .adv_data =
            {
                .p_data = m_enc_advdata,
                .len = BLE_GAP_ADV_SET_DATA_SIZE_MAX},
        .scan_rsp_data =
            {
                .p_data = NULL,
                .len = 0

            }

};

void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) {
  app_error_handler(DEAD_BEEF, line_num, p_file_name);
}
/** Initializing the Advertising.**/
static void advertising_init(void) {
  uint32_t err_code;
  ble_advdata_t advdata;
  uint32_t pin_value;
  uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
/* Beacon Configuration*/
  ble_advdata_manuf_data_t manuf_specific_data;

  manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
  m_beacon_info1[0]=APP_DEVICE_TYPE;                       
  m_beacon_info1[1]=APP_ADV_DATA_LENGTH;

  /* service id*/
  for(int i=5;i>1;i--)
  {
    m_beacon_info1[i]=*service_uid_pointer++;
  }

//   pin_value = nrf_gpio_pin_read(11);
//   if(pin_value
//  
 m_beacon_info1[6]=0x00;                                  // Aided
  m_beacon_info1[7]=0xff;                                  //Reserved id
  m_beacon_info1[8]=0xff;                                  // reserved id
  //m_beacon_info1[18]=0x20;                                 //Major 1st byte
  m_beacon_info1[18]=0x54; 
  m_beacon_info1[19]=0x57;  //Major 2nd byte
  m_beacon_info1[20]=0x4F;                                 //Minor 1st byte
  m_beacon_info1[21]=0x54; 
  //m_beacon_info1[21]=0x55;//Minor  2nd byte
  m_beacon_info1[22]=0xC3;                                 //Rssi @1m

 manuf_specific_data.data.p_data = (uint8_t *)m_beacon_info1;
 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;

  // Initialize advertising parameters.
  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;                                               //Filter policy
  m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL;
 // m_adv_params.channel_mask[4];
 // m_adv_params.channel_mask[4] = 0xA0;

  m_adv_params.duration = 0;                                                                     // Never time out.

  err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
  APP_ERROR_CHECK(err_code);

  err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
  APP_ERROR_CHECK(err_code);
}

/**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);
}

static void advertising_init1(void) {
//sd_ble_gap_adv_stop(m_adv_handle);

  uint32_t err_code;
  ble_advdata_t advdata;
  uint32_t pin_value;
  uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
/* Beacon Configuration*/
  ble_advdata_manuf_data_t manuf_specific_data;

  manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
  m_beacon_info1[0]=APP_DEVICE_TYPE;                       
  m_beacon_info1[1]=APP_ADV_DATA_LENGTH;
  m_beacon_info1[6]=0xff; 
  /* service id*/
  for(int i=5;i>1;i--)
  {
    m_beacon_info1[i]=*service_uid_pointer++;
  }
  
                                  // Aided
  m_beacon_info1[7]=0xff;                                  //Reserved id
  m_beacon_info1[8]=0xff;                                  // reserved id
  //m_beacon_info1[18]=0x20;                                 //Major 1st byte
  m_beacon_info1[18]=0x54; 
  m_beacon_info1[19]=0x57;  //Major 2nd byte
  m_beacon_info1[20]=0x4F;                                 //Minor 1st byte
  m_beacon_info1[21]=0x54; 
  //m_beacon_info1[21]=0x55;//Minor  2nd byte
  m_beacon_info1[22]=0xC3;                                 //Rssi @1m

 manuf_specific_data.data.p_data = (uint8_t *)m_beacon_info1;
 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;

  // Initialize advertising parameters.
  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;                                               //Filter policy
  m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL;
 // m_adv_params.channel_mask[4];
 // m_adv_params.channel_mask[4] = 0xA0;

  m_adv_params.duration = 0;                                                                     // Never time out.

  err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
  APP_ERROR_CHECK(err_code);

  err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
  APP_ERROR_CHECK(err_code);
 // advertising_start();
}

/**Starting advertising.*/
//static void advertising_start1(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);
//}
/* Getting MAc address*/
void device_name_get(void) {

  memcpy(serial_number,(uint32_t*)0x10001084,4);
    //NRF_LOG_INFO("Serial Number : %x, %x, %x, %x",serial_number[0],serial_number[1],serial_number[2],serial_number[3]);
    //NRF_LOG_FLUSH();
  mac_addr[0]=NRF_FICR->DEVICEADDR0;
  
  test_sno = test_sno | (serial_number[0] <<24)|(serial_number[1] <<16)|(serial_number[2] <<8)|(serial_number[3]);

  if (test_sno != 0xFFFF7FFFF)
  {
      
    for(int i=0;i<4;i++){
      service_uid[i]=serial_number[i];
      NRF_LOG_INFO("S.NO:%2X",service_uid[i]);
    }
   }
   else
   {
        for(int i=0;i<4;i++){
        service_uid[i]=(uint8_t)((mac_addr[0])>>(8*i));
        NRF_LOG_INFO("MAC:%2X",service_uid[i]);
      }
   }
service_uid_pointer=&service_uid;

}
/*Initializing the BLE stack.*/
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.
  // 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);
}

/** Initializing logging. */
static void log_init(void) {
  ret_code_t err_code = NRF_LOG_INIT(NULL);
  APP_ERROR_CHECK(err_code);

  NRF_LOG_DEFAULT_BACKENDS_INIT();
}

/**Initializing LEDs. */
static void leds_init(void) {
  ret_code_t err_code = bsp_init(BSP_INIT_LEDS, NULL);
  APP_ERROR_CHECK(err_code);
}

/**Initializing timers. */
static void timers_init(void) {
  ret_code_t err_code = app_timer_init();
  APP_ERROR_CHECK(err_code);
}

/** 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);
}

/*Function for handling the idle state (main loop).
 *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();
  }
}

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    ret_code_t err_code;
   err_code = sd_ble_gap_adv_stop(m_adv_handle);
   nrf_drv_gpiote_out_toggle(14);
  // m_beacon_info1[6]=0xff;
  advertising_init1();
  uint16_t i;
  for(i=0;i<10;i++)
  {
   err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);


  NRF_LOG_INFO("Beacon.");
  }
  err_code = sd_ble_gap_adv_stop(m_adv_handle);
  advertising_init();
  err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
//  ret_code_t err_code;
//
//    nrf_drv_gpiote_out_toggle(PIN_OUT);
//   
//
//    if(is_advertising){ 
//        err_code = sd_ble_gap_adv_stop(m_adv_handle);
//        APP_ERROR_CHECK(err_code);
//        NRF_LOG_INFO("Stopping advertising");
//        is_advertising = false;
//    } else{
//        err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
//        APP_ERROR_CHECK(err_code);
//        NRF_LOG_INFO("Starting advertising");
//        is_advertising = true;
// }   

}

static void gpio_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);

    err_code = nrf_drv_gpiote_out_init(PIN_OUT, &out_config);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP;

    err_code = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(PIN_IN, true);
}

/*Main Loop */
int main(void) {
  // Initialize.
  uint32_t pin_value;
  //nrf_gpio_cfg_input(11,GPIO_PIN_CNF_PULL_Pullup);
  nrf_gpio_cfg_output(14);
  log_init();
  timers_init();
 // leds_init();
  device_name_get();
  power_management_init();
  ble_stack_init();
  advertising_init();
  nrf_drv_clock_init();
  gpio_init();
  sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, BLE_GAP_TX_POWER_ROLE_ADV, 0);     //Set transmission Power
  // Start execution.
  NRF_LOG_INFO("Beacon example started.");
  
  advertising_start();

  // Enter main loop.
  for (;;)
 {
    
// pin_value = nrf_gpio_pin_read(11);
//   if(pin_value==0)
//   {
//     NRF_LOG_INFO("Beacon example started.");
//      m_beacon_info1[6]=0xff;      
//      nrf_gpio_pin_set(14);        
//     // nrf_delay_ms(100);// Aided
//   }
//   else
//   {
//     m_beacon_info1[6]=0x00;
//    nrf_gpio_pin_clear(14);
//   }

     //advertising_start();
    idle_state_handle();
  }
}

/**
 * @}
 */

#include "app_timer.h"
#include "ble_advdata.h"
#include "bsp.h"
#include "nordic_common.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "nrf_soc.h"
#include <stdbool.h>
#include <stdint.h>
#include "nrf_drv_gpiote.h"
#include "nrf_drv_gpiote.h"
#include "nrf_drv_gpiote.h"
#include "ble_srv_common.h"
#include "boards.h"
#include "nrf_delay.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
 
#define APP_BLE_CONN_CFG_TAG 1                                              // Tag identifying the BLE configuration.    
#define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(500, UNIT_0_625_MS)      // iBeacon specifies an advertising interval of 100ms
#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 0x004C                                       // Company identifier for Nordic Semiconductor 

#define DEAD_BEEF 0xDEADBEEF                                                // Value used as error code on stack dump
uint32_t mac_addr[];                                                        
uint8_t service_uid[17];
uint8_t *service_uid_pointer;
uint8_t m_beacon_info1[APP_BEACON_INFO_LENGTH]={0};
uint8_t serial_number[4];
uint32_t test_sno;

static ble_gap_adv_params_t m_adv_params;                                      // Parameters to be passed to the stack when starting advertising. 
static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;                  // Advertising handle used to identify an advertising set. 
static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX];                   // Buffer for storing an encoded advertising set. 


bool is_advertising = false;

#ifdef BSP_BUTTON_0
    #define PIN_IN BSP_BUTTON_0
#endif
#ifndef PIN_IN
    #error "Please indicate input pin"
#endif

#ifdef BSP_LED_0
    #define PIN_OUT BSP_LED_0
#endif
#ifndef PIN_OUT
    #error "Please indicate output pin"
#endif


      /*Encoded advertising data. */
static ble_gap_adv_data_t m_adv_data =
    {
        .adv_data =
            {
                .p_data = m_enc_advdata,
                .len = BLE_GAP_ADV_SET_DATA_SIZE_MAX},
        .scan_rsp_data =
            {
                .p_data = NULL,
                .len = 0

            }

};

void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) {
  app_error_handler(DEAD_BEEF, line_num, p_file_name);
}
/** Initializing the Advertising.**/
static void advertising_init(void) {
  uint32_t err_code;
  ble_advdata_t advdata;
  uint32_t pin_value;
  uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
/* Beacon Configuration*/
  ble_advdata_manuf_data_t manuf_specific_data;

  manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
  m_beacon_info1[0]=APP_DEVICE_TYPE;                       
  m_beacon_info1[1]=APP_ADV_DATA_LENGTH;

  /* service id*/
  for(int i=5;i>1;i--)
  {
    m_beacon_info1[i]=*service_uid_pointer++;
  }

//   pin_value = nrf_gpio_pin_read(11);
//   if(pin_value
//  
 m_beacon_info1[6]=0x00;                                  // Aided
  m_beacon_info1[7]=0xff;                                  //Reserved id
  m_beacon_info1[8]=0xff;                                  // reserved id
  //m_beacon_info1[18]=0x20;                                 //Major 1st byte
  m_beacon_info1[18]=0x54; 
  m_beacon_info1[19]=0x57;  //Major 2nd byte
  m_beacon_info1[20]=0x4F;                                 //Minor 1st byte
  m_beacon_info1[21]=0x54; 
  //m_beacon_info1[21]=0x55;//Minor  2nd byte
  m_beacon_info1[22]=0xC3;                                 //Rssi @1m

 manuf_specific_data.data.p_data = (uint8_t *)m_beacon_info1;
 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;

  // Initialize advertising parameters.
  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;                                               //Filter policy
  m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL;
 // m_adv_params.channel_mask[4];
 // m_adv_params.channel_mask[4] = 0xA0;

  m_adv_params.duration = 0;                                                                     // Never time out.

  err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
  APP_ERROR_CHECK(err_code);

  err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
  APP_ERROR_CHECK(err_code);
}

/**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);
}

static void advertising_init1(void) {
//sd_ble_gap_adv_stop(m_adv_handle);

  uint32_t err_code;
  ble_advdata_t advdata;
  uint32_t pin_value;
  uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
/* Beacon Configuration*/
  ble_advdata_manuf_data_t manuf_specific_data;

  manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
  m_beacon_info1[0]=APP_DEVICE_TYPE;                       
  m_beacon_info1[1]=APP_ADV_DATA_LENGTH;
  m_beacon_info1[6]=0xff; 
  /* service id*/
  for(int i=5;i>1;i--)
  {
    m_beacon_info1[i]=*service_uid_pointer++;
  }
  
                                  // Aided
  m_beacon_info1[7]=0xff;                                  //Reserved id
  m_beacon_info1[8]=0xff;                                  // reserved id
  //m_beacon_info1[18]=0x20;                                 //Major 1st byte
  m_beacon_info1[18]=0x54; 
  m_beacon_info1[19]=0x57;  //Major 2nd byte
  m_beacon_info1[20]=0x4F;                                 //Minor 1st byte
  m_beacon_info1[21]=0x54; 
  //m_beacon_info1[21]=0x55;//Minor  2nd byte
  m_beacon_info1[22]=0xC3;                                 //Rssi @1m

 manuf_specific_data.data.p_data = (uint8_t *)m_beacon_info1;
 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;

  // Initialize advertising parameters.
  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;                                               //Filter policy
  m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL;
 // m_adv_params.channel_mask[4];
 // m_adv_params.channel_mask[4] = 0xA0;

  m_adv_params.duration = 0;                                                                     // Never time out.

  err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
  APP_ERROR_CHECK(err_code);

  err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
  APP_ERROR_CHECK(err_code);
 // advertising_start();
}

/**Starting advertising.*/
//static void advertising_start1(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);
//}
/* Getting MAc address*/
void device_name_get(void) {

  memcpy(serial_number,(uint32_t*)0x10001084,4);
    //NRF_LOG_INFO("Serial Number : %x, %x, %x, %x",serial_number[0],serial_number[1],serial_number[2],serial_number[3]);
    //NRF_LOG_FLUSH();
  mac_addr[0]=NRF_FICR->DEVICEADDR0;
  
  test_sno = test_sno | (serial_number[0] <<24)|(serial_number[1] <<16)|(serial_number[2] <<8)|(serial_number[3]);

  if (test_sno != 0xFFFF7FFFF)
  {
      
    for(int i=0;i<4;i++){
      service_uid[i]=serial_number[i];
      NRF_LOG_INFO("S.NO:%2X",service_uid[i]);
    }
   }
   else
   {
        for(int i=0;i<4;i++){
        service_uid[i]=(uint8_t)((mac_addr[0])>>(8*i));
        NRF_LOG_INFO("MAC:%2X",service_uid[i]);
      }
   }
service_uid_pointer=&service_uid;

}
/*Initializing the BLE stack.*/
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.
  // 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);
}

/** Initializing logging. */
static void log_init(void) {
  ret_code_t err_code = NRF_LOG_INIT(NULL);
  APP_ERROR_CHECK(err_code);

  NRF_LOG_DEFAULT_BACKENDS_INIT();
}

/**Initializing LEDs. */
static void leds_init(void) {
  ret_code_t err_code = bsp_init(BSP_INIT_LEDS, NULL);
  APP_ERROR_CHECK(err_code);
}

/**Initializing timers. */
static void timers_init(void) {
  ret_code_t err_code = app_timer_init();
  APP_ERROR_CHECK(err_code);
}

/** 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);
}

/*Function for handling the idle state (main loop).
 *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();
  }
}

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
    ret_code_t err_code;
   err_code = sd_ble_gap_adv_stop(m_adv_handle);
   nrf_drv_gpiote_out_toggle(14);
  // m_beacon_info1[6]=0xff;
  advertising_init1();
  uint16_t i;
  for(i=0;i<10;i++)
  {
   err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);


  NRF_LOG_INFO("Beacon.");
  }
  err_code = sd_ble_gap_adv_stop(m_adv_handle);
  advertising_init();
  err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
//  ret_code_t err_code;
//
//    nrf_drv_gpiote_out_toggle(PIN_OUT);
//   
//
//    if(is_advertising){ 
//        err_code = sd_ble_gap_adv_stop(m_adv_handle);
//        APP_ERROR_CHECK(err_code);
//        NRF_LOG_INFO("Stopping advertising");
//        is_advertising = false;
//    } else{
//        err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
//        APP_ERROR_CHECK(err_code);
//        NRF_LOG_INFO("Starting advertising");
//        is_advertising = true;
// }   

}

static void gpio_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);

    err_code = nrf_drv_gpiote_out_init(PIN_OUT, &out_config);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP;

    err_code = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_gpiote_in_event_enable(PIN_IN, true);
}

/*Main Loop */
int main(void) {
  // Initialize.
  uint32_t pin_value;
  //nrf_gpio_cfg_input(11,GPIO_PIN_CNF_PULL_Pullup);
  nrf_gpio_cfg_output(14);
  log_init();
  timers_init();
 // leds_init();
  device_name_get();
  power_management_init();
  ble_stack_init();
  advertising_init();
  nrf_drv_clock_init();
  gpio_init();
  sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, BLE_GAP_TX_POWER_ROLE_ADV, 0);     //Set transmission Power
  // Start execution.
  NRF_LOG_INFO("Beacon example started.");
  
  advertising_start();

  // Enter main loop.
  for (;;)
 {
    
// pin_value = nrf_gpio_pin_read(11);
//   if(pin_value==0)
//   {
//     NRF_LOG_INFO("Beacon example started.");
//      m_beacon_info1[6]=0xff;      
//      nrf_gpio_pin_set(14);        
//     // nrf_delay_ms(100);// Aided
//   }
//   else
//   {
//     m_beacon_info1[6]=0x00;
//    nrf_gpio_pin_clear(14);
//   }

     //advertising_start();
    idle_state_handle();
  }
}

/**
 * @}
 */
Advertising Beacon 

/** Initializing the Advertising.**/
static void advertising_init(void) {
uint32_t err_code;
ble_advdata_t advdata;
uint32_t pin_value;
uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
/* Beacon Configuration*/
ble_advdata_manuf_data_t manuf_specific_data;

manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
m_beacon_info1[0]=APP_DEVICE_TYPE;
m_beacon_info1[1]=APP_ADV_DATA_LENGTH;

/* service id*/
for(int i=5;i>1;i--)
{
m_beacon_info1[i]=*service_uid_pointer++;
}

// pin_value = nrf_gpio_pin_read(11);
// if(pin_value
//
m_beacon_info1[6]=0x00; // Aided
m_beacon_info1[7]=0xff; //Reserved id
m_beacon_info1[8]=0xff; // reserved id
//m_beacon_info1[18]=0x20; //Major 1st byte
m_beacon_info1[18]=0x54;
m_beacon_info1[19]=0x57; //Major 2nd byte
m_beacon_info1[20]=0x4F; //Minor 1st byte
m_beacon_info1[21]=0x54;
//m_beacon_info1[21]=0x55;//Minor 2nd byte
m_beacon_info1[22]=0xC3; //Rssi @1m

manuf_specific_data.data.p_data = (uint8_t *)m_beacon_info1;
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;

// Initialize advertising parameters.
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; //Filter policy
m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL;
// m_adv_params.channel_mask[4];
// m_adv_params.channel_mask[4] = 0xA0;

m_adv_params.duration = 0; // Never time out.

err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
APP_ERROR_CHECK(err_code);

err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
APP_ERROR_CHECK(err_code);
}

Button Handler

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
ret_code_t err_code;
err_code = sd_ble_gap_adv_stop(m_adv_handle);   //stop advertsising
nrf_drv_gpiote_out_toggle(14);
// m_beacon_info1[6]=0xff;
advertising_init1();      //Re initiate advertising  with m_beacon_info1[8]=0xff; // reserved id
 

uint16_t i;
for(i=0;i<10;i++)    //  next 10 advertising
{
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);


NRF_LOG_INFO("Beacon.");
}
err_code = sd_ble_gap_adv_stop(m_adv_handle);            //stop  Re initiated advertising
advertising_init();
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG); // start as m_beacon_info1[8]=0x00; // reserved id}

Parents
  • I will make sure I understand you correctly before I proceed.

    • You start advertising with m_beacon_info1[6]=0x00
    • A button is pressed, and m_beacon_info1[6] is set to 0xff
    • Then the device sends out 10 advertising packets and m_beacon_info1[6] is 0xff
      • Have you confirmed that the advertising packet contains this value, by using a sniffer or similar?
    • When the next advertising packet is sent out, m_beacon_info1[6] is equal to 0x00
      • Have you confirmed that the advertising packet contains this value, by using a sniffer or similar?

    Do I understand you correctly?

    Best regards,

    Simon

Reply
  • I will make sure I understand you correctly before I proceed.

    • You start advertising with m_beacon_info1[6]=0x00
    • A button is pressed, and m_beacon_info1[6] is set to 0xff
    • Then the device sends out 10 advertising packets and m_beacon_info1[6] is 0xff
      • Have you confirmed that the advertising packet contains this value, by using a sniffer or similar?
    • When the next advertising packet is sent out, m_beacon_info1[6] is equal to 0x00
      • Have you confirmed that the advertising packet contains this value, by using a sniffer or similar?

    Do I understand you correctly?

    Best regards,

    Simon

Children
  • Hi simon,

    yes you are correct. Thanks for your quick reply

    1) First two points , it works fine.

    2) 3rd point is my problem. i cant see the advertising packets as m_beacon_info1[6] is 0xff in nrf connect app.

           how should i send 10 advertising packets with m_beacon_info1[6] is 0xff?

          how should i count advertising ?

    3) Immediately the packets changed as m_beacon_info1[6] to 0x00.

    Here i tried 

    if(flag==1)   interrupt handler
    {
    ret_code_t err_code;
    err_code = sd_ble_gap_adv_stop(m_adv_handle);
    // nrf_drv_gpiote_out_toggle(14);
    device_name_get();
    advertising_init1();
    uint32_t i;
    err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
    NRF_LOG_INFO("Beacon.");


    for(i=0;i>100;i++)
    {
    //i=0;
    idle_state_handle();
    i++;
    }
    advertising_init();
    // nrf_drv_gpiote_out_toggle(14);
    flag=0;
    }

Related