Location-based recipes – Bluetooth LE and Beacons

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:

  • Central device versus peripheral device rubric: In a BLE ecosystem, there will be a broadcast (peripheral) device that sends out data and a (central) device or devices that reads from this broadcast. The device relationship is analogous to a community bulletin board with a bunch of readers or a client/server relationship. However, in BLE-land, the normal assumptions are flipped: the server is a very low-powered device, whereas the client is typically a more sophisticated device, such as a smartphone, a laptop, a tablet, or other wireless device.
  • Not one-to-one: BLE allows multiple simultaneous connections to the same server or peripheral device.
  • Low power: The connections between devices are very short in BLE. This results in extremely low power usage. Devices can operate with simple lithium coin cell batteries.
  • Broad services and characteristics schema: The data presented by a peripheral is structured as services, which are then subdivided into characteristics. Services are akin to the notices on a bulletin board, whereas characteristics are similar to the individual paragraphs of these notices.

    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:

  • It delivers low cost radio signaling
  • It provides fairly rapid and simple development requirements
  • It requires very low power with its use of BLE
  • It broadcasts far more precise location data than GPS

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:

  • UUID: Services are identified by unique numbers known as UUIDs (Universal Unique Identifier), which you may be familiar with from other projects. Therefore, the museum would have its own unique UUID string.
  • The major field (ID): Inputting a specific value in this field can specify a more general location, for example, a particular floor in the museum.
  • The minor field (ID): Inputting a value in this field will specify a particular gallery on a specific floor.
  • TX Power: This is used to try to estimate the distance based on the signal strength of the Wi-Fi connection (the RSSI value) in a particular room.

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.

Location-based recipes – Bluetooth LE and Beacons

Getting ready

The BBB is powered up via USB and connected online either via Wi-Fi or Ethernet with two additional items:

  • USB Bluetooth LE (BLE), also known as Bluetooth 4.0 adapter—we will use a Targus dongle (product ID ACB10US1). This uses the Broadcom BCM20702 chipset. There are far cheaper ones that should work fine on the BBB that cost around USD $10.00-15.00. Here is a list of compatible Bluetooth dongles for the Raspberry Pi (http://elinux.org/RPi_USB_Bluetooth_adapters). Many of those listed will likely work with the BBB, although they have not all been tested for compatibility.
  • Smartphone with Bluetooth LE—iPhones starting from 4S to any of the current models are BLE-compliant. On the Android side, there are exceptions. By default, the current models of an Android phone runs on 4.3 or higher BLE. However, any Android phones that cannot run on version 4.3 or higher will not work with this set of recipes.

How to do it…

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:

  1. Download and install Evothings Studio (http://evothings.com/). The installation requires a Workbench desktop and a mobile app, all of which is explained on their website.
  2. After downloading Evothings Studio, you will find a Mac OS X and a Windows version on the desktop. Install your preferred client.
  3. For the mobile app, install and start the Evothings client app on your mobile device. Android and iOS are available at their respective online stores.

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'},

Note

Other than their own identity, iBeacons do not push notifications to the central (receiving) devices. However, a mobile app on your phone can use signals received from iBeacons to trigger their own push notifications.

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:

  1. First, update and upgrade the BBB with the following command:
    $ sudo apt-get update
    
  2. Then, we will verify that BlueZ is installed using the following command:
    $ bluetoothd -v
    

    If you see a version number appear, skip to step 4; otherwise, proceed to the next step.

  3. Now, install BlueZ with the following command:
    $ sudo apt-get install bluez
    

    Note

    You may want to install BlueZ from its source. Here is the code for it:

        sudo mkdir bluez
        cd bluez
        sudo wget www.kernel.org/pub/linux/bluetooth/bluez-x.xx.tar.xz
    

    Check for the current version of BlueZ before running wget.

  4. Then, connect the dongle to the USB port and verify that it is recognized with the following command:
    $ lsusb
    
  5. Now, we want to set up Bluetooth communications between the BBB and any other BLE devices that require using a series of BlueZ commands. To do this, we will use the 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
    
  6. The preceding output shows one device present with the ID 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
    
  7. Ensure that it says DOWN, which is not what we want. So, up, up, and away we go:
    $ sudo hciconfig hci0 up
    
  8. Then, verify that the command worked by repeating our earlier step. Your 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)
    
  9. Now, we will set up the dongle to begin what is known as advertising in the BLE parlance. We will perform this in the low energy (le) mode using the flag 3; this sets the connection mode:
    $ sudo hciconfig hci0 leadv 3
    

    Note

    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.

  10. By default, the BLE dongle will scan for other devices. However, we want to turn this function off because it can interfere with advertising as follows:
    $ sudo hciconfig hci0 noscan
    
  11. The last command adds the UUID, as shown in the following command:
    $ 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.

There's more...

The following references provide more detail on BTLE:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.64.128