Channels ▼

Al Williams

Dr. Dobb's Bloggers

Sharper Than a Blue Tooth

November 03, 2014

Last time, I talked about BlueTooth Low Energy (BLE) and the Nordic nRF51822 demo board that you can use with mbed. I put a small project together on mbed that you can open online.

This simple project sends out beacon data using the board's onboard temperature sensor (granted, it measures the internal chip temperature, which might not be that useful). If you open the project, the heart of it is the global array beaconPayload:


uint8_t beaconPayload[] = {

0x4C, 0x00, // vendor ID

0x02, // packet type (2)

0x15, // length

0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, // UUID

0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61,

0x00, 0x00, // Major

0x00, 0x00, // Minor

0xC8 // TXPower

, 0x99 // one spare byte!

};

The packet aims to be mostly compatible with an iBeacon, which explains the vendor ID. The UUID is a unique number for this beacon and, although not the official purpose of these bytes, my code will use the major and minor data items to carry sensor data. The TxPower field is a two's compliment of how strong the device expects to be (in dBm) from one meter away. By comparing that number to the actual received signal, you can get an idea of the relative distance between the devices.

The program calls setupBLE every time it wants to create a new advertisement. This fills in some header information (for example, LE_GENERAL_DISCOVERABLE tells the world that the beacon offers no services). It also populates the sensor data.

Here's the code:

/// Build up advertising packet including sensors

void setupBLE()

{

  ble.clearAdvertisingPayload();

  readSensors();

  errTry(ble.accumulateAdvertisingPayload(

  GapAdvertisingData::BREDR_NOT_SUPPORTED |

  GapAdvertisingData::LE_GENERAL_DISCOVERABLE),5);

  errTry(ble.accumulateAdvertisingPayload(

  GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,

  beaconPayload, sizeof(beaconPayload)),6);

}

This gets called to set up the initial payload and each time the program rereads its sensor. The main loop effectively hands off to the library code that manages BLE and lets it do all the difficult work.

There's more, of course, but you can see it online and, thanks to the provided libraries, it is pretty straightforward. Of course, the question is, how do you read the beacon? You can find some BLE software on the major mobile platform marketplaces. Next time, I'll talk about how Android reads a BLE beacon like this.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video