NRF52832 study notes (36)-iBeacon

1. Introduction

iBeacon is a new feature Apple's September 2013 release of mobile devices equipped with the OS on (iOS7). The way it works is that a device equipped with a **Bluetooth Low Energy (BLE)** communication function uses BLE technology to send its own unique ID to the surroundings, and the application software that receives the ID will take some actions based on the ID. For example, if the iBeacon communication module is installed in the store, an information notification server can be run on the iPhone and iPad, or the server can send discount coupons and store points to customers. In addition, you can also use iBeacon to send information to the application when a home appliance malfunctions or stops working.

Two, iBeacon format

iBeacon uses BLE technology. Specifically, it uses a broadcast frame called "Advertising" in BLE. Announcement frames are frames that are sent periodically and can be received by devices that support BLE. iBeacon is implemented by embedding data in Apple's own format in the payload part of this notification frame.

AD Field Length Type Company ID iBeacon Type iBeacon Length UUID Major Minor TX Power

AD Field Length: The length of Advertisement Data, which indicates the length of useful broadcast information.
Type: Broadcast type.
Company ID: The data field starts with a two-byte company ID code. SIG issued these ID codes to the company, where 0x004C represents the Apple id (only this ID, the device is called iBeacon)
iBeacon Type: byte 0x02 represents the device is a Beacon
iBeacon Length: the length of the remaining fields
UUID: defined as The 128-bit identifier
Major and Minor of the ISO/IEC11578:1996 standard are set by the iBeacon issuer and are 16-bit identifiers. For example, chain stores can write regional information in Major, and individual store IDs in Minor. In addition, when embedding the iBeacon function in home appliances, you can use Major to represent the product model and Minor to represent the error code, which is used to notify the outside of the fault.
TX Power: The RSSI strength at 1 meter estimated by the APP through the iBeacon signal strength

Three, modify the code

Open the project SDK\examples\ble_peripheral\ble_app_beacon

First of all, we first define the data related to beacon. Among them, there are three parameters that our users need to pay attention to, UUID, Major and Minor . The other parameters can be understood as a fixed format (the format is fixed, but the data content is not fixed and may be different. Manufacturer information). There is another data worthy of our attention, that is APP_COMPANY_IDENTIFIER, if we define this parameter as 0x004C(that is, Apple id), then our base station equipment is called iBeacon.

// BEACON数据
#define APP_BEACON_INFO_LENGTH          0x17                     // BEACON数据总长度
#define APP_ADV_DATA_LENGTH             0x15                     // BEACON特殊字节的长度
#define APP_DEVICE_TYPE                 0x02                     // 字节0x02代表这个设备是BEACON
#define APP_MEASURED_RSSI               0xC3                     // BEACON在1米距离处的信号强度
#define APP_COMPANY_IDENTIFIER          0x004C                   // 004C代表的是Apple id(只有这个ID,设备才会叫iBeacon)
#define APP_MAJOR_VALUE                 0x01, 0x02               // major
#define APP_MINOR_VALUE                 0x03, 0x04               // minor
#define APP_BEACON_UUID                 0x01, 0x12, 0x23, 0x34, \
                                        0x45, 0x56, 0x67, 0x78, \
                                        0x89, 0x9a, 0xab, 0xbc, \
                                        0xcd, 0xde, 0xef, 0xf0   /**< Proprietary UUID for Beacon. */

// BEACON数据数组,用于初始化广播数据内容
static uint8_t m_beacon_info[APP_BEACON_INFO_LENGTH] = 
{
    
    
    APP_DEVICE_TYPE,
    APP_ADV_DATA_LENGTH, 
    APP_BEACON_UUID,
    APP_MAJOR_VALUE,
    APP_MINOR_VALUE,
    APP_MEASURED_RSSI
};

For the iBeacon data, broadcasted in the form of broadcast, then its main setting is the broadcast initialization part in the main function (see NRF52832 study notes (9)-GAP slave broadcast ), all defined parameters must be carried out during broadcast initialization Configuration.

static void advertising_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;  // 公司ID号

#if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
    // If USE_UICR_FOR_MAJ_MIN_VALUES is defined, the major and minor values will be read from the
    // UICR instead of using the default values. The major and minor values obtained from the UICR
    // are encoded into advertising data in big endian order (MSB First).
    // To set the UICR used by this example to a desired value, write to the address 0x10001080
    // using the nrfjprog tool. The command to be used is as follows.
    // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val <your major/minor value>
    // For example, for a major value and minor value of 0xabcd and 0x0102 respectively, the
    // the following command should be used.
    // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val 0xabcd0102
    uint16_t major_value = ((*(uint32_t *)UICR_ADDRESS) & 0xFFFF0000) >> 16;
    uint16_t minor_value = ((*(uint32_t *)UICR_ADDRESS) & 0x0000FFFF);

    uint8_t index = MAJ_VAL_OFFSET_IN_BEACON_INFO;

    m_beacon_info[index++] = MSB_16(major_value);
    m_beacon_info[index++] = LSB_16(major_value);

    m_beacon_info[index++] = MSB_16(minor_value);
    m_beacon_info[index++] = LSB_16(minor_value);
#endif

    manuf_specific_data.data.p_data = (uint8_t *) m_beacon_info;  // 数据结构体
    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 (used when starting advertising).
    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        = NON_CONNECTABLE_ADV_INTERVAL;
    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);
}

In the main function, call the initialization broadcast function advertising_init(). After the function advertising_start() starts the broadcast, the broadcast information can be sent out, that is, the broadcast of the beacon broadcast packet

int main(void)
{
    
    
    // Initialize.
    log_init();
    timers_init();
    leds_init();
    power_management_init();
    ble_stack_init();  // 协议栈初始化
    advertising_init();  // 广播初始化

    // Start execution.
    NRF_LOG_INFO("Beacon example started.");
    advertising_start();  // 开始广播

    // Enter main loop.
    for (;; )
    {
    
    
        idle_state_handle();
    }
}

Written by Leung on January 6, 2021

• Reference: Qingfeng Electronic Community

Guess you like

Origin blog.csdn.net/qq_36347513/article/details/112260113