Background Bluetooth Scanning on Android

There was a time when it was very easy to scan for beacons in background on Android. You just set up a service using the standard Bluetooth APIs. Over the years, Google has crippled the ability to perform reliable background processing to the point where it’s nearly as bad as on iOS. David Young says:

“Today, Android Services are often more trouble than they are worth”

David has an answer to the problem in the form of using Android BroadcastReceivers and threads. He explains the problem and solution on his blog. David also has a useful Github app demonstrating the technique.

Read about custom solutions

Working with GATT on Android

Most of the time, beacons transmit and the receiving software such as apps on iOS and Android or applications on single board computers (SBC) only read the advertising data. There’s no connection to the beacon. However, for programmatic setup of beacon parameters or accessing some sensor data, applications might need to connect via what’s known as Bluetooth GATT.

There’s an article on How to Work Properly With BT LE On Android. It provides some useful pointers such as not performing scanning and GATT connection simultaneously, avoiding auto-connect and not blocking GATT callbacks.

GATT can be unreliable on Android. While scanning for advertising data usually works very well, we have found that GATT connections fail all the time on about 5% of devices. This is due to poorly implemented OS Bluetooth software. This means beacon manufacturer-supplied configuration apps sometimes can’t connect. The only solution is to use a different phone (or the iOS version of the app on iPhone).

Troubleshooting Beacon Problems

Some people come to us, having set up their beacons, saying “It doesn’t work”. Most scenarios involve a beacon, an app and a phone. Solving most problems involves breaking the problem down by swapping out each of the beacon, app and phone until it works.

If you have more than one beacon, you can swap out the beacon. Having said this, it’s rare for beacons to fail and if the problem is with the beacon, it’s more likely to be the beacon settings that are incorrect.

While you can’t swap out the manufacturer configuration app, you can use another app such as Nordic’s nRF Connect (on iOS and Android) to scan for a beacon, see if it’s advertising and if so, what type of advertising it is sending.

It’s common for individual phones to have have problems. First, make sure you have Location and Bluetooth on. Anyone working with beacons will usually need to have both Android and iOS devices to diagnose problems. Run the app (and nRF Connect) on multiple phones of different platform type (iOS/Android) to help narrow down problems.

If you still can’t get it working, send us a support ticket. Please don’t just say “It doesn’t work” and instead describe what you have done and at what stage it doesn’t work with any error messages.

Also read

Why Doesn’t the Manufacturer’s Configuration App Connect?

Testing if a Beacon is Working

Proof of Concept for Beacon Projects

It’s easy to buy into an idea and commit significant resources only to find very late on that a project is overly difficult or impossible to implement. We see too many companies only come to us after they have gone a long way down a particular road only to discover they made a big mistake early on. It might be, for example, they have heavily committed to the wrong beacon, wrong platform or have assumed something on one of the mobile platforms. They didn’t do their research. Often we can help them get on the right track but sometimes not.

We always recommend organisations research upfront. Test risky areas. Create a low cost proof of concept exercising risky areas. A proof of concept is the implementation of a small subset of the whole system to prove implementing the whole thing is possible. Good candidates for functionality for proof of concepts are specific usecases, scenarios or user stories. Choose specific usecases to exercise what you think might be the most difficult or unknown parts of the system.

Proof of concepts provide a feel for the development effort that will be required to develop the complete system thus giving an indication of the project’s cost and the financial viability of the project.

It’s also possible to create proof of concepts that include business goals. Think ‘proof of value’ rather than ‘proof of concept’. Proving a project has value to stakeholders can help unlock realistic funding for development of the complete project.

Read about consultancy

Read about feasibility studies

Is it Possible to Continuously Scan for Bluetooth Devices on iOS and Android?

We sometimes get asked if it’s possible to use a smartphone as a gateway to scan for Bluetooth devices. The thinking is usually that workers or users already have devices so why not make use of them?

While it is possible, there are many reasons why you might not want to do this:

  • On iOS, Apple hide Bluetooth MAC addresses and for some APIs hide the iBeacon ids making unique identification more difficult.
  • You will find it very difficult to get a continuously scanning app through Apple app store review. You will need strong justifications.
  • Scanning continuously uses lots of battery power, even when advertising with periodic ‘off’ and ‘on’ periods.
  • Capabilities of devices vary meaning you will almost certainly get some end user devices where your implementation won’t work. For example, some manufacturers stop long running processes.
  • Some users will play with their phones and end up purposely or inadvertently disabling your application.

The best scenarios are those where you can dictate the phone type, it can be mains (PSU) powered and the phone isn’t owned by a user (i.e. it’s just used as a gateway). It’s almost always better to use a dedicated gateway.

PubNub Android Tutorials

We recently came across a great resource on PubNub that shows how to use Android to detect beacons and also transmit as a beacon.

The tutorial is in three parts that 1) Describes beacon advertising and how to scan for beacons 2) How to filter detected beacons 3) Setting up Android as an emitter.

There’s also a related PubNub article by the same author on how to Create a Tessel Beacon with a BLE Module.

Capacitor Plugin for Bluetooth Low Energy (BLE)

There’s a new Capacitor plugin for Bluetooth LE. Capacitor is new way to build web apps. Capacitor’s native plugin APIs allow easy access to common functionality, such as Bluetooth, across multiple platforms.

The plugin supports the web, Android and iOS. It allows scanning, GATT connecting, reading, writing and notifications. The source code is available on GitHub.

Rapid Prototyping Android Bluetooth Apps

Elektor Magazine has a webinar Rapid Prototyping BLE Android Apps Using MIT App Inventor taking place on June 10, 2021 at 1600 CEST.

MIT App Inventor is a cloud-based tool that allows you to build apps in the web browser. It provides a visual programming block-based environment allowing anyone, even children, to build fully functional apps for smartphones and tablets.

The first 50 registrants are being entered into a raffle to win 10 PIC-IoT boards and vouchers worth €10 and €25 for the Elektor Store.