BLE app development in JavaScript explained – Bluetooth Low Energy in action

Mikael KindborgBlogs, Tutorials

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+

Bluetooth Low Energy, estimote-beacons-group-small commonly referred to as BLE, is a cool low-power wireless technology that is perfect for connecting things to mobile apps. In the near future, we will see lots of consumer products that use BLE; toys, home appliances, bikes, cars, sensors for sports equipment, clothes, you name it! Many of these devices will need an associated mobile application to be useful. Therefore, app developers have vast opportunities to create innovative applications for both custom-built and consumer BLE hardware.

In this tutorial, we will first make an overview of BLE and then look at a mobile app for BLE written in JavaScript that you can run, modify and extend.

BLE is used for connecting things

Bluetooth Low Energy is a low-power wireless radio technology that consumes very little energy (hence the name). Typically BLE devices are small, battery-powered and the battery can last for years. The transmission range depends on the nature of the surrounding area (walls, buildings etc), but is somewhere between 10 and 30 meters, some even claim up to 100 meters.

BLE devices buggy can be used for both reading (input) information and for writing (output) information to perform operations. Some devices are “read-only”, for example iBeacons and sensors that gather information from their environment. Others you can interact with, like door controllers (e.g. to open a garage-door), light controllers and radio-controlled toys.

Mobile phones and tablet are perfect to read/control BLE-devices. iOS has great support for BLE, and Android (4.3 and later) also has BLE support (but not all Android units have the required hardware). BLE makes it possible to program apps that interact with hardware devices, creating and eco-system of apps and all sorts of hardware.

The origin of BLE is Bluetooth (“Bluetooth Classic”), and Bluetooth 4.0 includes the BLE standard. That means that a device that supports Bluetooth 4.0 supports BLE. BLE is also referred to as “Bluetooth Smart”. One things that is handy with BLE is that it can operate without pairing. This makes it easy to monitor BLE devices (which is how iBeacons work) and connect to and communicate with devices.

Today, LightBlueBean BLE is built into fitness wristbands, BLE powers iBeacons (such as Estimote), the Texas Instruments SensorTag, the LightBlue Bean, the RFduino, and you can easily equip an Arduino with a BLE Shield. There are a number of professional BLE development boards available, like Bluegiga Bluetooth 4.0 Smart Modules, CSR µEnergy® Development Kit, Laird Bluetooth Modules, and Bluetooth products from Texas Instruments.

In the near future, we will see lots of consumer products that use BLE; toys, home appliances, bikes, cars, sports equipment, bags, clothes, and much more. It is a truly innovative area, with lots of projects on crowdfunding sites Kickstarter. BLE is one of the core technologies of the Internet of Things (IoT).

A interesting and useful property of BLE is that you can determine roughly how close a device is. This is possible because of the Received Signal Strength Indicator (RSSI) which is a value that hints at the distance from which the signal originated. iBeacons use this to detect the proximity of beacons. For example, if you stroll along a city street, you can use a mobile app that tells you when you approach a restaurant or cafe that serves your favourite dish or snack.

Importantly, BLE is a “local area” technology that works outside of the Internet and outside of WiFi networks. It is an independent radio technology. Note that mobile apps that connect to BLE devices can in turn connect to the Internet to transmit data, but this is not a requirement for BLE, it is rather an application of BLE. This makes Bluetooth Low Energy a very flexible technology for communication with things (it would even work on an expedition to Mars!).

A reference book for learning about BLE in depth is Bluetooth Low Energy: The Developer’s Handbook.

Quick guide to BLE programming

BLE devices have one of two roles, “central” or “peripheral”. A peripheral is typically the “small thing”. It is that battery-powered sensor or iBeacon or radio-controlled quadcopter. A mobile device typically has the central role. This is usually where the “intelligence” or “business logic” is handled.

A peripheral can be in one of two states, advertising or connected. When advertising, the device sends out a signal that says “Here I am, and here is my name”. The central device can listen for advertising devices, and select one to connect to. Being connected is required to be able to exchange data, to read values from and to execute commands on the peripheral device.

Some peripherals use advertisement mode only, one example is iBeacons. An iBeacon sends out a signal every now and then (ranging from lots of times per second to once a minute or so) telling who it is. The RSSI value (signal strength) can then be used to determine who close the beacon is. This technique can even be used for in-door positioning, using triangulation of multiple beacons.

When you connect as a central to a device, you can read data from it. This is what the data structure of a BLE device looks like this:

device (peripheral)
  services
    characteristics
      descriptors

Key points:

  • Services group functionality.
  • Characteristics represent data that you can read, write, or subscribe to using notifications.
  • Descriptors contain “meta-data” for characteristics, however not all characteristics have descriptors.

For example, a thermometer device would have a service that has a characteristic for reading the temperature. You can then read the temperature by reading the value of the characteristic. You could also enable a notification for the characteristic, to get periodic calls with the updated temperature value (in this case you don’t have to poll the value).

To connect to a BLE device, you first scan for BLE devices, then connect to the device you wish to use. Here is an outline:

  • Scan for devices.
  • When a device is found, you can identify the device, and determine its RSSI value.
  • When the device you want is found, you can connect to it.
  • When connected you can read services, characteristics, descriptors.
  • Then write and/or read the characteristics you wish to use. Enable any notifications you want to use.

When you program a mobile app with Evothings Studio, you use JavaScript to perform the above. Now we will look at a hands on example of a BLE app you can try out yourself.

Hands on example – BLE Scan

The example app “BLE Scan” in Evothings Studio is a great starting point for learning how to scan for devices.

BLEScan

It is quick and easy to get started:

  • Install the Evothings Client app on your iOS or Android device (available in the AppStore and on Google Play). Note that for Android you need at least Android 4.3 and the required hardware.
  • Download Evothings Studio.
  • Launch Evothings Workbench on your computer.
  • Start the Evothings Client app and scan for and connect to the Workbench.
  • Locate the example app “BLE Scan” in the Workbench project list and press Run.
  • Press the “Scan” button in the app.
  • Nearby BLE devices that are in advertisement mode should now display, along with the RSSI value.

If you don’t have any BLE device at home, take a walk around the town to see what you can detect! :-)

There are currently some quirks on Android, check out the documentation of the BLE Scan example to learn how to work around them.

JavaScript BLE plugin

To access BLE from JavaScript, the Evothings BLE plugin for Apache Cordova is used.

Files of interest:

  • ble.js is the API for the BLE plugin. This file is shipped with the Evothings Client app (which is built with Cordova), and is not included in the source code for the BLE Explorer app. You don’t have to include this file in your HTML code, it is included automatically with cordova.js.
  • easy-ble.js is a high-level abstraction of the API in ble.js. This file you need to include explicitly in your HTML code if you wish to use it.

More Evothings BLE example apps and tutorials

Evothings Studio comes with several apps that show how to use BLE. There are also lots of tutorials to try out – about iBeacons, radio controlled models, and the TI SensorTag. Here is a listing.

Example apps:

Tutorials:

Hands on example for exploring device data:

Call to action!

Download Evothings Studio and reveal any hidden BLE devices near you – there may be more of them than you think!

Good luck, have fun!

Welcome to ask questions at the Evothings Forum.