David Vassallo's Blog

If at first you don't succeed; call it version 1.0

BLE Health Devices: First Steps with Android

Bluetooth Low Energy (also known as Bluetooth v4) is the current standard in Bluetooth Technology. It is particularly interesting to me when applied to healthcare devices, for a number of reasons:

  1. No pairing necessary. These healthcare devices are normally handled by carers or vulnerable people who do not want to go through the hassle of pairing bluetooth devices, or entering PIN codes, etc. They just want an easy, quick connection process – BLE does this wonderfully. The BLE device acts as a server which broadcasts a number of services [1]. Different devices offer different services – for example a thermometer would advertise a “Health Thermometer” service (0x1809), and a SP02 device would advertise a “Heart Rate” service (0x180D). A BLE client (an android smartphone in this case) can scan for BLE device, determine which one offers the service it needs, connect to it, and get a reading
  2. “Push” model. In healthcare devices it’s easier to use a push model… that is, a device will notify the smartphone that a reading is ready and send the reading. This in contrast to early versions where the smartphone had to poll the device
  3. Standards. The bluetooth standard covers services [1] so BLE devices from different manufacturers present their data in the same format – which is a big plus since it avoids developers having to hassle with reverse engineering data representations

This article outlines the steps I took as a newbie android BLE developer to get a basic Android app talk to a couple of BLE devices.

  • First, download the extremely handy “BLE Scanner” from BluePixel here:


This app helps save a lot of time by displaying which services and characteristics are present on a device. Connect to your BLE device and snoop around which services are being offered. See if you can read any values

  • Second, read the fantastic article by Marcelle Gibble on toastdroid regarding android BLE here:


I had to pay special attention to the “Configure Descriptor for Notify” and “Receive Notifications” sections…

  • Understand the basic android code on how to connect and read information from a BLE device in “Android Bluetooth Low Energy (BLE) Example” by Mohit Gupt here:


  • Using the last link above to provide me with a skeleton app, I then set about modifying the code to:
    • Receive notifications as described by the toastdroid article, i.e. by configuring the descriptor for notify and overriding the “onCharacteristicChanged” [2] method

The full code can be found here [3]:

  • Figuring out which service to subscribe to takes some fiddling around with the Android Studio debugger, by setting a breakpoint on line 267 above and inspecting the “services” object
  • In line 294 in the code above we read the float value with an offset of “1”, since this is a temperature record as described in [4], we see the first byte is reserved for flags, and the actual temperature reading starts at the second byte.

The resulting app automatically detects which device is sending data to it, and displays the information read on screen:

device-2015-09-02-150827 device-2015-09-02-151028


[1] Bluetooth developer portal, https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx

[2] Android Developers, https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html#onCharacteristicChanged

[3] BLE demo app gist, https://gist.github.com/dvas0004/e78cd9f73a331d856bec

[4] Bluetooth Temperature Measurement, https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml

ELK : exporting to CSV

Note: the following requires the “jq” json parser, available from: http://stedolan.github.io/jq/

1. Run the desired query through the Kibana WebUI

2. Expand the additional options pane by clicking on the arrow underneath the graph as indicated in the below screenshot:


3. Select “Request” and copy the request displayed:


4. Open a linux terminal and use the following command, pasting the above where indicated, and changing the template name as appropriate:

curl -XGET ‘*/_search?pretty’ -d ‘—–PASTE HERE———‘ | jq -r ‘.hits.hits[]._source | del(.tags) | [to_entries[]] | map(.value) | @csv’ > /tmp/output.csv

Note: “size” in the copy/pasted text needs to be modified according to how many records are to be exported.

The csv output will be stored in the /tmp/output.csv file, which can be downloaded via SFTP and manipulated as necessary

Note: Since ELK deals with unstructured data, it may be the case that the csv file does not have the same number of columns for each entry, especially if different types of records are queried. This is to be expected and is by design. In order to get a list of column names for each row, the following command can be run:

curl -XGET ‘*/_search?pretty’ -d ‘—–PASTE HERE———‘ | jq -r ‘.hits.hits[]._source | del(.tags) | [to_entries[]] | map(.key) | @csv’ > /tmp/output.csv

The above command will generate the /tmp/output.csv file but will contain column headings for each row rather than actual data.


Get every new post delivered to your Inbox.

Join 229 other followers