In the past, you may have had the opportunity to use Bluetooth devices in your projects. However, in all probability, that would have been an earlier version of BT, now commonly referred to as "classic" Bluetooth. Before we jump into an actual recipe on how to use these small radio devices, we should take a look at some of the significant differences between the classic version of Bluetooth and the Bluetooth Low Energy (BLE) spec because they are very different animals. Although they are both available as part of the 4.0 spec, we will work only with the BLE piece for our recipe.
Classic Bluetooth
With the classic version of the stack, one device basically connects to another device, data is exchanged serially, and the data does not persist. For example, when you set up a BT headset, the audio comes piping into your ears, and the data goes on after the throughput. Furthermore, the connection does not reveal or show anything about what's happening in the middle of that pipe with the data itself.
Bluetooth Low Energy
Although more challenging to work with and still nascent with its documentation, BLE (sometimes referred to as Bluetooth Smart) is a far more robust environment for managing data, particularly in the world of IoT. Several key elements distinguish it from its earlier kin:
You can easily hook up to different services and characteristics with a few lines of code. For example, you can interact with a battery service and easily determine the power levels remaining for a device. Alternatively, your use case may be related to health or fitness, which means that you can leverage the onboard health service that includes a characteristic (such as heartbeat data).
Beacons and iBeacons
Where GPS leaves off, Beacons begin. This emerging BLE technology is quickly becoming the next big thing in location sensing. Although proven, powerful, and ubiquitous, GPS can only determine the coordinates of an object within 20-30 feet. However, the BLE Beacon technology is commonly referred to as hyperlocal in its sensitivity because it is accurate within a range of 100 meters down to inches. Thus far, most of the interest around the Beacon technology is focused on how marketers and advertisers want to use it in order to send you targeted offers… Yuck!
This is largely due to the fact that Apple's marketing-centric version of the protocol called iBeacons has been the most commonly referenced and easiest type to get running quickly. However, the intrinsic BLE Beacon stack, iBeacon or otherwise, is very exciting for several reasons:
For IoT-like scenarios, both indoor and outdoor, Beacons provide much promise far beyond annoying marketing messages. Also, you can actually buy premade beacons for very little money, sometimes for only a few dollars (USD).
It may seem like overkill to be using a full-blown microcomputer, such as the BBB, to serve as a Beacon. However, our intention is to create our own BBB version so that we have more flexibility in understanding and building the constituent parts of a Beacon network. Doing this as an open source recipe allows you to pull the levers yourself.
A further point to clarify here is that BLE Beacons do not actually send content that a user receives on their smartphone app. The beacon simply provides the rudimentary data to a mobile app, which allows that app to determine its location in a venue or outdoor spot and in turn sends the user location-specific data. Alternatively, it can capture and make use of that data in other ways.
Furthermore, iBeacons do not track users based on any kind of profiling requirements; the interaction can be—and generally is—completely anonymous, so concerns about privacy are negligible.
With Beacons, there are several data fields that you typically want to watch. This helps in differentiating one beacon from another. For instance, let's say that we are in the Museum of Natural History in New York City that has a beacon network. Here are the relevant data fields:
The major and minor IDs can be used to trigger certain behaviors in your application when it discovers one of your beacons. All of these fields come together to make pinpointing locations extremely accurate.
The BBB is powered up via USB and connected online either via Wi-Fi or Ethernet with two additional items:
Part 1—Setting up your smartphone
As mentioned earlier in the chapter, we need to install some apps on our smartphone in order to make this recipe meaningful and demonstrate the BBB's beacon-ness.
For the iPhone, we installed and tested two different free apps from the App Store, one is called Locate (by Radius Networks) and the other is called Beacon Tool. (The developers must gotten up very early in the morning to come up with those names…).
Locate has a few more functionalities than Beacon Tool because it lets us set the major and minor values for the Beacon.
Part 2—Setting up a simple IoT test environment
You can test and track the iBeacon using a web service. There are several businesses in this space. We used a platform called EvoThings, which is a free, open source platform.
Evothings allows you to use a simple IDE on your desktop and then demonstrate an iBeacon app on a smartphone with a Beacon-enabled BBB in the middle of the mix. Perform the following steps:
There are numerous demos and examples that come with Evothings. Although they tend to be focused on how to use an Arduino as a beacon, the code is very flexible (and open source), so everything is customizable for other platforms. For our first quick example, in your desktop Evothings Workbench, open the following file in a text editor:
Evothings_Studio/examples/ibeacon-scan/index.js
Then, scan about halfway down the page to find the following code:
// Add your own manufacturer UUIDs to this list. {uuid:'B9407F30-F5F8-466E-AFF9-25556B57FE6D'}, {uuid:'F7826DA6-4FA2-4E98-8024-BC5B71E0893E'}, {uuid:'8DEEFBB9-F738-4297-8040-96668BB44281'}, {uuid:'A0B13730-3A9A-11E3-AA6E-0800200C9A66'},
Now, add your generated UUID from the earlier step to line up with the following syntax:
{uuid:'3784B5B0-2DA7-11E4-8C21-0800200C9A66'},
Part 3: Setting up the BBB
Let's start with the essential package on how to control and interact with Bluetooth devices (LE or otherwise) and the Linux Bluetooth stack BlueZ. On the current Debian distro that comes with any newly purchased BBB, BlueZ should be already installed. Perform the following steps:
$ sudo apt-get update
$ bluetoothd -v
If you see a version number appear, skip to step 4; otherwise, proceed to the next step.
$ sudo apt-get install bluez
$ lsusb
hciconfig
tool and begin by taking a look at the address of the USB adapter as follows:$ hcitool dev Devices: hci0 00:19:0E:15:DE:87
0
that is prepended with hci
; this is part of the protocol's naming convention. We will take this ID and append it to the command, include the (-a
) option to output information about all the devices present, and check the dongle's ready state, as shown in the following code:$ hciconfig -a hci0
Your screen should show the following code:
hci0: Type: BR/EDR Bus: USB BD Address: 00:19:0E:15:DE:87 ACL MTU: 1021:8 SCO MTU: 64:1 DOWN RX bytes:1267 acl:0 sco:0 events:47 errors:0 TX bytes:747 acl:0 sco:0 commands:47 errors:0 Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87 Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 Link policy: RSWITCH SNIFF Link mode: SLAVE ACCEPT
DOWN
, which is not what we want. So, up, up, and away we go:$ sudo hciconfig hci0 up
stdout
should look similar to the following command:$ hciconfig -a hci0 hci0: Type: BR/EDR Bus: USB BD Address: 00:19:0E:15:DE:87 ACL MTU: 1021:8 SCO MTU: 64:1 UP RUNNING RX bytes:990 acl:0 sco:0 events:44 errors:0 TX bytes:738 acl:0 sco:0 commands:44 errors:0 Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87 Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 Link policy: RSWITCH SNIFF Link mode: SLAVE ACCEPT Name: 'BCM20702A' Class: 0x000000 Service Classes: Unspecified Device Class: Miscellaneous, HCI Version: 4.0 (0x6) Revision: 0x1000 LMP Version: 4.0 (0x6) Subversion: 0x220e Manufacturer: Broadcom Corporation (15)
le
) mode using the flag 3
; this sets the connection mode:$ sudo hciconfig hci0 leadv 3
A further word about nomenclature in the iBeacon/BLE landscape is that you will often see references to the word advertising when you develop with BLE. The term is used generically to refer to a packet of data that is being sent from a BT device to make its presence known; it has nothing to do with Apple's original advertising/marketing-centric purposes for iBeacon.
$ sudo hciconfig hci0 noscan
$ sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 37 84 B5 B0 2D A7 11 E4 8C 21 08 00 20 0C 9A 66 00 00 00 00 C8
The instant you send the preceding command, you should see a notification/popup on your smartphone. With the iLocate app on the iPhone, it should say Entered region: BBB beacon 1.
(This assumes that a smartphone app is already installed and set up to recognize the BBB beacon UUID).
Congratulations! You are now able to broadcast hyperlocal data without the need for a big GPS satellite.
The following references provide more detail on BTLE:
18.220.64.128