1. 程式人生 > >nrf51822教程系列 第二課 nrf51822 softdevice GAP Advertising

nrf51822教程系列 第二課 nrf51822 softdevice GAP Advertising

The following figure describe how the advertising packet and scan response packet is sent.

image description

In some applications when we don't expect a connection and don't have extra data in scan response packet, we can advertise in non-connectable mode and can skip the RX period to save power. The beacon application is one of the use cases.

Note that all Advertising packet, Scan Request packet, Scan Response packet share the same on-air Access Address = 0x8E89BED6. This common address allows any device to scan and receive advertising/scan response data.

Bluetooth Smart uses 40 RF channels in the ISM band (2.4GHz). These RF channels have center frequencies 2402 + k*2MHz where k ranges from 0 to 39. Note that k is not the same as "Channel Index", or channel number (Section 1.4 Vol 6 Part B).

Three of them is dedicated for advertising which is channel 37 (2402MHz), 38 (2426MHz) and 39 (2480MHz). They were selected to avoid interference with the busy channels used by Wifi. This figure shows the 3 advertising channels, 37 data channels, and the curved shapes are wifi busy channels.

image description

In the implementation of our stack, by default we transmit the advertising packet in all 3 channels on every advertising event, on channel 37, 38, 39 respectively. Next figure shows how we do that, and how the advertising packet on a channel is captured by the scanner, which scan on one of the three channel at a time.

image description

II. Broadcast topology

When advertising, the network topology is Broadcast topology. There could be multiple advertisers and multiple scanner at the same time. It is a connection-less network:

image description

Note that one device can do scanning and advertising simultaneously. And one can be in a connection with a central or peripheral and can do advertising at the same time.

The only packet the active scanner can send to the advertiser is the Scan Request packet, which contain only the scanner address. Passive scanner doesn't do Scan Request.

III. Advertising types and advertising with whitelist

There are 4 defined types of Advertising (Section 2.3.1 Vol 6 Part B):

• ADV_IND: connectable undirected advertising . This is the normal advertising type where any device can send scan response packet and connect request to the advertiser.

• ADV_DIRECT_IND: connectable directed advertising . You use this to direct your advertise packet to one specific central to ask for connection. The packet is still a broadcast packet but other scanners will ignore the packet if the peer address is not matched with them. And connect request or scan request from unmatched central will be ignored by the advertiser. Directed advertising usually comes with high duty cycle with interval fix at 3.75ms. For low duty cycle directed advertising, it's configurable and should be <10ms. (Section 4.4.2 Part B Vol 6).

• ADV_SCAN_IND: scannable undirected advertising . This advertising packet won't accept connect request but accept scan request.

• ADV_NONCONN_IND: non-connectable undirected advertising . This is non RX mode, which mean the advertiser will not accept any connect request or scan request. Staying in this mode the advertiser doesn't need to switch to receiver mode and can save power. The main application for this is beacon application, where maximize battery life time is most important and the beacon doesn't need to interact with the scanner.

/**@defgroup BLE_GAP_ADV_TYPES GAP Advertising types
 * @{ */
#define BLE_GAP_ADV_TYPE_ADV_IND          0x00   /**< Connectable undirected. */
#define BLE_GAP_ADV_TYPE_ADV_DIRECT_IND   0x01   /**< Connectable directed. */
#define BLE_GAP_ADV_TYPE_ADV_SCAN_IND     0x02   /**< Scannable undirected. */
#define BLE_GAP_ADV_TYPE_ADV_NONCONN_IND  0x03   /**< Non connectable undirected. */
/**@} */
Advertise with Whitelist

The advertiser can use a whitelist to limit the interaction to a number of scanner/central device. The whitelist contains an array of the peer device addresses or IRK numbers (when central use resolvable random address). It will reject packets from scanners/centrals whose addresses are not in the list. Whitelist can be configured to filter scan request packets, connect request packets or both.

B. Setting up and start advertising with our Softdevice

I. APIs provided by the softdevice:

  • Setting up advertising data and the scan response data:
  • Start advertising with the parameters:

Here is how the 31 bytes Advertising data and Scan response data should look like(Chapter 11 Part C Vol 3).

image description

It's up to the application to prepare the advertising data and set it in the softdevice usingsd_ble_gap_adv_data_set().

Note:

  • The data in the input parameter is an array of uint8_t. You need to encode data to match with this.
  • You should set the length of the advertising data (dlen) to match with the significant part length so that the non-significant part (zero padding) will not be transferred over the air.
  • 31 bytes includes also the overhead so the actual payload for application is 27 bytes.

After you have set-up the advertising packet you can tell the softdevice to start advertising by calling sd_ble_gap_adv_start(). For this call, you need to configure:

  • Advertising interval (the period between each advertising)

  • Advertising timeout: how long you want to advertise. You will receive BLE_GAP_EVT_TIMEOUT event after this timeout.

  • Advertising types (connectable, non-connectable, directed, etc),

  • The peer address if you do directed advertising,

  • The whitelist list if you have.

  • Filter policy: Choose how to use the whitelist, filter scan request, connect request or both.

  • Channel(s) you want to advertise, you can choose one of or two of or all 3 channels to advertise.

II. GAP events you may receive when advertising:

  • BLE_GAP_EVT_TIMEOUT: Occurs when the advertising timeout is passed. The application can decide to continue advertising in different mode or to enter sleep mode. The application should check the src parameter to check if the timeout event is from advertising timeout or not. SeeBLE_GAP_TIMEOUT_SOURCES list.

  • BLE_GAP_EVT_SCAN_REQ_REPORT: The application receives this event when there is a scan request received by the advertiser. The event comes with address of the peer device and RSSI value. Note: you only get this event if you enable it using the option API sd_ble_opt_set().

  • BLE_GAP_EVT_CONNECTED: You receive this when there is a central device send connect request and the connection is established.

  • /**@brief GAP advertising parameters.*/
    typedef struct
    {
      uint8_t               type;                 /**< See @ref BLE_GAP_ADV_TYPES. */
      ble_gap_addr_t       *p_peer_addr;          /**< For @ref BLE_GAP_ADV_TYPE_ADV_DIRECT_IND mode only, known peer address. */
      uint8_t               fp;                   /**< Filter Policy, see @ref BLE_GAP_ADV_FILTER_POLICIES. */
      ble_gap_whitelist_t  *p_whitelist;          /**< Pointer to whitelist, NULL if none is given. */
      uint16_t              interval;             /**< Advertising interval between 0x0020 and 0x4000 in 0.625 ms units (20ms to 10.24s), see @ref BLE_GAP_ADV_INTERVALS.
                                                       - If type equals @ref BLE_GAP_ADV_TYPE_ADV_DIRECT_IND, this parameter must be set to 0 for high duty cycle directed advertising.
                                                       - If type equals @ref BLE_GAP_ADV_TYPE_ADV_DIRECT_IND, set @ref BLE_GAP_ADV_INTERVAL_MIN <= interval <= @ref BLE_GAP_ADV_INTERVAL_MAX for low duty cycle advertising.*/
      uint16_t              timeout;              /**< Advertising timeout between 0x0001 and 0x3FFF in seconds, 0x0000 disables timeout. See also @ref BLE_GAP_ADV_TIMEOUT_VALUES. If type equals @ref BLE_GAP_ADV_TYPE_ADV_DIRECT_IND, this parameter must be set to 0 for High duty cycle directed advertising. */
      ble_gap_adv_ch_mask_t channel_mask;         /**< Advertising channel mask. @see ble_gap_channel_mask_t for documentation. */
    } ble_gap_adv_params_t;
    


  • /**@brief Function for starting advertising.
     */
    static void advertising_start(void)
    {
        uint32_t             err_code;
        ble_gap_adv_params_t adv_params;
    
        // Always initialize all fields in structs to zero or you might get unexpected behaviour
        memset(&adv_params, 0, sizeof(adv_params));
    
        adv_params.type        = BLE_GAP_ADV_TYPE_ADV_IND;
        adv_params.p_peer_addr = NULL;
        adv_params.fp          = BLE_GAP_ADV_FP_ANY;
        adv_params.interval    = APP_ADV_INTERVAL;
        adv_params.timeout     = APP_ADV_TIMEOUT_IN_SECONDS;
    
        err_code = sd_ble_gap_adv_start(&adv_params);
        APP_ERROR_CHECK(err_code);// Check for errors
    }
    


III. Example code

The following code (for S110v8.0/S130v1.0) will setup the advertising packet with device name, flag, Battery service UUID, and advertise with interval = 40ms (0.625 ms unit) and advertising timeout = 180 seconds. It advertises with whitelist applied for connect request. And the only peer address in the whitelist is 0xE05FDAECA271. _

uint32_t             err_code;ble_gap_adv_params_t adv_params;ble_gap_whitelist_t  whitelist;ble_gap_addr_t* p_whitelist_addr[1];ble_gap_addr_t        whitelist_addr={BLE_GAP_ADDR_TYPE_RANDOM_STATIC,{0x71,0xA2,0xEC,0xDA,0x5F,0xE0}};uint8_t addr[6]={0x71,0xA2,0xEC,0xDA,0x5F,0xE0};uint8_t adv_data[15]={0x07,0x09,0x4E,0x6F,0x72,0x64,0x69,0x63,0x02,0x01,0x04,0x03,0x03,0x0F,0x18};uint8_t adv_data_length =15;//Setting up the advertising data with scan response data = Null
err_code = sd_ble_gap_adv_data_set(adv_data, adv_data_length, NULL, NULL);//Configure the advertising parameter and whitelist
memset(&adv_params,0,sizeof(adv_params));
adv_params.type        = BLE_GAP_ADV_TYPE_ADV_IND;
adv_params.p_peer_addr = NULL;
adv_params.interval    =64;
adv_params.timeout     =180;
p_whitelist_addr[0]=&whitelist_addr;
whitelist.addr_count =1;
whitelist.pp_addrs   =  p_whitelist_addr;
whitelist.pp_irks = NULL;
whitelist.irk_count =0;
adv_params.fp          = BLE_GAP_ADV_FP_FILTER_CONNREQ;
adv_params.p_whitelist =&whitelist;
err_code = sd_ble_gap_adv_start(&adv_params);

Here is what the scanner sees when receiving the advertising packet:

image description

Breaking down the raw advertising data:{0x07,0x09,0x4E,0x6F,0x72,0x64,0x69,0x63,0x02,0x01,0x04,0x03,0x03,0x0F,0x18}

0x07= length 7 octets    
0x09= AD type CompleteLocalName0x4E,0x6F,0x72,0x64,0x69,0x63="Nordic"in ASCII

0x02= length 2 octects    
0x01= AD type flags     
0x04=Flag BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED

0x03=length 3 octects    
0x03= AD type CompleteList of 16-bit ServiceClassUUIDs0x0F,0x18=BatteryService

List of AD types can be found here.

To make the code short and simple, the check of the return code (err_code) of the API call is not added here. When you implement your code, make sure the return code of each API is checked and it should return NRF_SUCCESS (0x00).

The examples in our SDK provide an Advertising module (ble_advertising.c) with abstraction layers and predefined modes. Documentation can be found here.

In addition we provide the advertising data encoding module (ble_advdata.c) that you can use to generate the advertising/scan response packet.

C. FAQ

1. Why am I seeing the advertising period is not the same between each packet ?
Bluetooth spec defined a random delay (0-10ms) on each advertising period to perturb interval and avoid interference (Section 4.4.2.2 part B Vol 6).

2. If I want to advertise my custom data how should I put it in the advertising packet ? You should use Manufacturer Specific Data ADtype (0xFF) as defined here. Note that with that type, the 2 first bytes are for Company ID.

References:

Useful links:

BLE Advertising, a beginner's tutorial: A tutorial on how to set up advertising packet using our ble_advdata.c in the nRF51 SDK.



相關推薦

nrf51822教程系列 第二 nrf51822 softdevice GAP Advertising

The following figure describe how the advertising packet and scan response packet is sent. In some applications when we don't expect a connection and

nrf51822教程系列nrf51822 flash中寫入資料(flash write )

前言 暫存器介紹 0 .1 Non-Volatile Memory Controller (NVMC) Functional descriptionThe Non-volatile Memory Controller (NVMC) is used for writing a

Java EE入門教程系列第二章JSP(一)——第一個JSP程式

2、1JSP概述 2.1.1JSP簡介 JSP,全稱是Java Server Pages,是在Servlet技術的基礎上形成的,主要完成網頁中伺服器動態部分的編寫。 有如下特點: (1)一次編寫,隨處執行 (2)可重用元件技術 (3)標記化頁面開發:JSP將許多常用功能封裝起

Java EE入門教程系列第二章JSP(四)——內建物件

2.4 內建物件 2.4.1 常用的內建物件 在JSP頁面中已經預先定義好了9個內建物件,可以在Web應用中直接使用。內建物件的構建基於HTTP協議,所以它們可以完成收集瀏覽器請求發出的資訊、響應瀏覽器請求以及儲存客戶資訊等工作,極大簡化了Web開發工作。 JSP內建列表如下圖所示:

Java EE入門教程系列第二章JSP(三)——JSP指令與動作元件

2.3 指令與動作元件 2.3.1 page指令 page指令的基本語法為: <%@ page 屬性1="屬性1的值" 屬性2="屬性2的值"···%> 屬性值記得用“”或者‘’括起來,這樣寫比較規範,不易出錯。 舉例: <%@ page language=

MongoDB基礎教程系列--第二篇 MongoDB基本操作(一)

1、安裝環境 在官網上下載MongoDB的最新版本,根據自身Windows版本下載正確的MongoDB版本。下載後,雙擊32位或者64位.msi檔案,按操作提示安裝就可以了。 說明: 32 位版本的 MongoDB 只支援 2G 以下的資料庫,只適用於測試及評估。 在

Hyperledger Fabric 1.0 實戰開發系列 第二 Fabric環境搭建

一.安裝GO語言 下載最新版的go 開啟Terminal,輸入命令(以下命令都是以root管理員的角色進行的) su 輸入密碼:***** wget https://storage.googleapi

Java EE入門教程系列第二章JSP(六)——JSP標準標籤和自定義標籤的配置與使用

2.6 JSP的標籤 2.6.1 標籤簡介 標籤就是把一段具體業務的Java程式碼封裝起來,然後以標記語言的形式在頁面檔案中對它進行呼叫,增強頁面檔案和Java程式的獨立性。 目前標籤庫有兩種形式:標準標籤庫和自定義標籤。 JSP標準標籤(JSTL)是一個可以實現We

Java EE入門教程系列第二章JSP(五)——表示式語言EL

2.5 表示式語言——EL 大量Java指令碼使得JSP頁面難以維護,於是,一種類似JavaScript的語言——EL表示式可用於在網頁上生成動態內容,並代替JSP指令碼元素的技術被推出。 2.5.1 基本語法 ${EL expression} 其中,$是EL語法中的

一步步帶你做vue後臺管理框架(二)——上手使用 系列教程《一步步帶你做vue後臺管理框架》第二

線上體驗地址:立即體驗  閒扯再多不會用也沒白搭,這節課我來帶大家直接上手框架,體驗到簡單方便之後你就會愛上這個框架欲罷不能的。  首先跟所有的vue專案一樣,兩種方式,一種去專案上下載程式碼包到本地,或者使用git clone https://github.com/hero

nrf51822教程 第一 BLE Advertising 藍芽廣播

What you see in the picture is: The device name as defined in our main.c file. Try to search for #define DEVICE_NAME in your code and change the string

微信硬體開發系列教程06-藍芽nrf51822開發環境(airkiss/airsync)

對藍芽nrf51822開發,需要安裝他的開發環境,Keil_MDK+nrf51_sdk+nrfgostudio+JLinkARM。Keil_MDK:大名鼎鼎的Keil開發軟體。nrf51_sdk:官方NRF51822的SDK。nrfgostudio:官方NRF51822的操

自動駕駛模擬 PresSan進階教程第二——Viewer使用方法及視訊輸出

例項:Example_2_viewer 1.顯示畫面設定 1.1 顯示畫面選擇及調整 CameraSensor_1 不可調整 Default View 可調整,可儲存 儲存檢視方便快速切換到自己所需的檢視 1.2 3D world設定 Experiment → General set

三維互動數字沙盤開發教程第二 利用WPF建立3d gis數字地球(非axhost方式)

三維互動視覺化數字沙盤開發教程第二課 利用WPF建立3d gis數字地球(非axhost方式) 上一篇實現了一個基本球,在基本球中已經實現了類似google earth的操作,即滑鼠單鍵拖動,滾輪放大,滑鼠中鍵按下左右旋轉、上下拉伸。 多點觸控操作:單指拖動,雙指旋轉和縮放,5指上下拉伸、左

BLE-NRF51822教程4-串列埠BLE解析

本講逐行程式碼解析官方串列埠BLE例程demo PS: 基於SDK5.1   主要分一下幾個部分: 1 :Main函式的整體註釋 2 :函式單獨解析。 3 :接收串列埠資料併發送給對端裝置

Quartz 框架 教程(中文版)2.2.x 之第二 Quartz API,Jobs和Triggers簡介

第二課:QuartzAPI,Jobs和Triggers簡介 Quartz API Quartz API 關鍵的幾個介面:           Scheduler:跟任務排程相關的最主要的API介面。           Job:你期望任務排程執行的元件定義(排程器執行

用OpenInventor實現的NeHe OpenGL教程第二十六

用OpenInventor實現的NeHe OpenGL教程-第二十六課   NeHe教程在這節課中向我們介紹瞭如何建立鏡面顯示效果,它使用剪裁平面,蒙板快取等OpenGL中一些高階的技巧。 在OpenInventor中已經提供了剪裁面節點SoClipPlane。但蒙板快

OpenGL教程翻譯 第二十二 使用Assimp載入模型

第二十二課 使用Assimp載入模型 背景 到現在為止我們都在使用手動生成的模型。正如你所想的,指明每個頂點的位置和其他屬性有點時候並不是十分方便。對於一個箱子、錐體和簡單平面還好,但是像人們的臉怎麼辦?現實的商業應用和遊戲中,程式中使用模型一般都是

用OpenInventor實現的NeHe OpenGL教程第二十四

用OpenInventor實現的NeHe OpenGL教程-第二十四課   NeHe教程在這節課中向我們介紹瞭如何讀取顯示卡支援的OpenGL的擴充套件,如何使用Targa(TGA)影象檔案作為紋理,以及如何利用OpenGL的剪裁區域來滾動螢幕文字。 在OpenInven

實戰SpringCloud響應式微服務系列教程第二章)

接上一篇:實戰SpringCloud響應式微服務系列教程(第一章) 1.1.2背壓 背壓是響應式程式設計的核心概念,這一節也是我們瞭解響應式程式設計的重點。 1.背壓的機制 在生產者/消費者模型中,我們意識到消費者在消費由生產者生產的資料的同時,也需要有一種能夠向上遊反饋流量需求的機制,這種能夠向上遊