admin管理员组

文章数量:1641945

BLE Advertising, a beginner's tutorial

the purpose of this tutorial is to get you started with the basics of the nRF5x and Bluetooth Low Energy (BLE). The tutorial is fairly superficial and is meant to be a hands-on introduction to BLE advertising and broadcasting of a limited payload. It is intended to be a fun way to get you started with something functional, and maybe inspire you to go further with BLE. Here you will not learn details of the BLE protocol, but we will have a quick look at what you should know before making an advertising device.

Before we begin

Table of context

    1. Before we begin
    2. The "Hello world" example
    3. Advertising
      • RSSI
      • Addresses
      • Address types
      • Advertising types
      • Bonding
    4. Advertising data
    5. Scan response data

Change Log 2016.03.14: Updated tutorial to suit SDK V11.0.0 and the possibility to use both nRF51 and nRF52 kits.

 

Equipment and software

To complete this tutorial you will need the following:

 

    • nRF51 DK or nRF52 DK Kit
    • nRF51 Dongle
    • Keil V5.12 or later.
    • Master Control Panel (MCP). You can also use the Master Control Panel found in the nRF Toolbox app for Android or the LightBlue app for iPhone.
    • nRFgo Studio
    • SDK V11.0.0 NOTE! This tutorial is written for SDK V11.0.0. Example files for SDK V8.x.0 and SDK V9.0.0 can also be found on github, but the APIs are different.
    • For nRF51 DK use SoftDevice S130 V2.0.0 for nRF52 DK use S132 V2.0.0.
    • Example files found on github.

Other kits, dongles and software versions might work as well, but this is what I have used. This tutorial will not cover how to install and setup the software. Please search the forum if you run into trouble before posting questions here.

 

Necessary prior knowledge

It is expected that you have basic knowledge of how to use Keil, MCP and nRFgo Studio and how to download your application to your kit. Please browse the  Tutorial section  and the  Infocenter  to find guides on how to use your equipment and compile your first BLE application.  Here  is an excellent blogpost that I highly recommend you read. It provides a more in depth explanation on how advertising works and is a natural supplement to this tutorial.

 

If you run into troubles please browse devzone, look for documentation on our Infocenter, and read the user guides for your kits. I urge you to post any questions you might have on the forum and not below the tutorial. This makes it is easier for other users and Nordic employees to see and search for your question and you will actually most likely get a faster response(!).

The “Hello world” example

First thing first: download the example code from github. It is based on the template example found in the SDK V11.0.0, but stripped of all code that is not necessary for our purpose. There is still quite a lot of code left though, which is used to initiate and utilize processes running in the background. The code is not essential for this tutorial and can be left as is. If you are curious of what it is anyway then here are some links to documentation on some of the modules that are used in the code:

 

  1. The Device Manager. This is a module that handles active and bonded connections. Since this tutorial is about advertising and not connections it is not really relevant. Still I chose to leave it in the code as it is extensively used in more advanced examples, so I figured you might just as well get used to it being there and it doesn't do any harm.
  2. The Button Support Package (bsp). This module is used to handle the LEDs and the buttons. In this example you will notice that an LED is blinking when your board is advertising. After 180 seconds the board will stop advertising and go to sleep. Then you can push Button 1 and the board will wake up and start advertising again. All this is handled by the bsp module.
  3. Various _evt() and _dispatch() functions. These are functions that handle BLE and system events that originates in the Softdevice. They also allow your application to take various actions based on what is going on in the Softdevice. We will not dwell on these functions in this tutorial.

To compile the example download the files and copy the folder "nrf5x-ble-tutorial-advertising" to "your_SDK_folder\examples\ble_peripheral". Your main.c file should then be found in the folder "your_SDK_folder\examples\ble_peripheral\nrf5x-ble-tutorial-advertising". If you need help with this please have a look at this thread on devzone: Compiling github projects. Open the project file "ble_app_tutorial_advertising.uvprojx" and hit “Build”. It should compile without any errors or warnings.

 

Advertising

Now, let us have some fun right away. Make sure you have programmed the correct SoftDevice to your nRF5x DK kit and compiled the example successfully. Then hit the download button in Keil and load the example onto your kit as well. Open MCP and start discovery using your nRF51 Dongle as master emulator. To learn how to use MCP and a master emulator read this tutorial: Testing and using a simple BLE application. A device named “HelloWorld” should show up in the list of discovered devices as shown in the picture below.

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 to something of your own choice (just don’t make it too long). Now compile and download your application again and see how your device name has changed.
    • The Bluetooth Device Address. Similar to a Media Access Control address (MAC address), but not necessarily universally unique.
    • The Received Signal Strength Indicator (RSSI). This is a value of how good your signal strength is. Move your kit around or lay your hand over the PCB antenna on your kit and see how the RSSI changes.

 

The Advertising packet

Now expand the "HelloWorld" device tree by clicking on the little cross to the left. You should see something similar to the next image. 

 

What you see here is:

 

RSSI

The Received Signal

本文标签: 蓝牙一课教程BLEAdvertising