Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Al Williams

Dr. Dobb's Bloggers

Bluetooth Phone Home

November 14, 2014

Last time, I talked about how you could deploy Bluetooth Low Energy (BLE) applications using a simple mbed-based board. The example code beamed a temperature over a Bluetooth beacon. The missing piece, of course, is something to receive it.

Modern Android phones can receive a beacon like this, and it is pretty straightforward if you are familiar with Android programming. You need to set up the manifest properly, create an instance of the Bluetooth manager, and scan for compatible Bluetooth devices.

The manifest is easy enough to set up. You need two permissions:


You also need a "uses" line:

<uses-feature android:name="android.hardware.bluetooth_le"
android:required="true" />

If your program doesn't have to have BLE capability (that is, BLE enhances your application but isn't absolutely necessary), you can change the android:required key to false. In that case, you should check the availability of BLE at runtime:

if (!getPackageManager().hasSystemFeature

(PackageManager.FEATURE_BLUETOOTH_LE)) { // no BLE here

Once you are ready, you can get the Bluetooth Manager instance using the getSystemService API with the Context.BLUETOOTH_SERVICE argument.

The manager has a getAdapter method that returns a BlueToothAdapter object. You should check the adapter's isEnabled method to see if the adapter is actually turned on. If it isn't, you can create an ACTION_REQUEST_ENABLE intent and send it via startActivityForResult to prompt the user to enable Bluetooth.

Once you have the adapter enabled, you can call startLeScan to find BLE devices (this will not find regular Bluetooth devices). There is also a version of startLeScan that takes an array of UUIDs to look only for specific BLE devices.

Either way, your code provides a BluetoothAdapter.LeScanCallback interface that the Bluetooth system calls when it finds a device. Scanning can be hard on the battery, so your program should scan only when necessary.

When your callback activates, you'll receive a Bluetooth device instance, an array containing the beacon data, and the received signal strength indication (RSSI). This is the signal level and is useful for estimating how far away the remote device is (or finding the closest — or at least the strongest — remote device).

This is just the start of using BLE devices. If you only need to read a beacon, there isn't much more to it than this. If, however, you want to connect to a service, you have a bit more work to do. Using mbed on the embedded side to quickly create a BLE device and with the Android API doing most of the heavy lifting on the phone side, it can be pretty painless to create a complete BLE application.

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.