Understanding the core of Aarogya Setu-Bluetooth

Niharika Arora
Jun 19, 2020 · 6 min read

Aarogya Setu is a mobile application developed by the Government of India to connect essential health services with the people of India in our combined fight against COVID-19. Do visit this link for more details.

AarogyaSetu started with an idea of automatic contact tracing. Proximity logging within an app addresses a key limitation of manual contact tracing: it is dependent on a person’s memory and is therefore limited to the contacts that a person is acquainted with and remembers having met.

For contact tracing, we had two options to go with: Bluetooth or GPS.

Bluetooth vs GPS:

Bluetooth was chosen because it is able to classify close contacts with a significantly lower false-positive rate than GPS. Given that GPS accuracy decreases in indoor environments, entire shopping malls or skyscrapers would be within the margin of error of a single GPS point.

So, with an idea that is more scalable, accurate and less resource-intensive, we created an app which:

  • Can help us in tracing the nearby contacts through Bluetooth.
  • Consumes low battery power.
  • Performs continuous.
  • Is compatible with both Android and iOS.

Implementation

We have used Bluetooth Low Energy(BLE) technology that has built-in platform support in the central role and provides APIs that apps can use to discover devices, query for services, and transmit information since Android 4.3(API level 18). In contrast to Classic Bluetooth, BLE is designed to provide significantly lower power consumption.

Permissions required:

"android.permission.BLUETOOTH","android.permission.BLUETOOTH_ADMIN", "android.permission.ACCESS_COARSE_LOCATION"

Note: Some of the devices (above 6.0) requires location permission to use Bluetooth. You can read more here.

BLE works with advertisement and scanning. The device in the central role scans, looking for advertisement, and the device in the peripheral role makes the advertisement.

Advertising Over BLE

We have BluetoothLeAdvertiser class for starting and stopping advertising. An advertiser(Added in API level 21) can broadcast up to 31 bytes of advertisement data represented by AdvertiseData.

Advertising Settings :

The AdvertiseSettings provide a way to adjust advertising preferences for each Bluetooth LE advertisement instance.

Advertising Data:

This is the data to be advertised as well as the scan response data for active scans.

  • setIncludeDeviceName: We are setting an 8 bytes(+2 bytes header) unique id as Bluetooth name for distinguishing a packet during the scan. So, we have made this flag true.
  • addServiceUuid: Setting a 16 bytes(+2 bytes header) UUID that is used for identifying our packets when they are advertised.

So now, we know how all 31 bytes are getting consumed. For more clarification on byte structure, please check this class.

GATT server:

We need to connect to the GATT server on the device to make devices talk to each other. After opening a GATT server for connection, we have added one service with 2 characteristics (UniqueID & Pinger characteristics).

Reason for implementing:

As per the iOS limitation, iOS is not able to scan if it goes in background. So, we found that making advertisement connectable through GATT can actually minimize this limitation by pinging the devices for connection and getting the unique id from there so that the app remains active and the connection breaks only if the device goes too far. Furthermore, you don't need to worry about multiple connections at the same time.GATT itself handles it for a good number of connections.

Scanning through BLE

We have BluetoothLeScanner(Added in API level 21) to perform scan related operations for Bluetooth LE devices. An application can scan for a particular type of Bluetooth LE devices using ScanFilter.

Scan Filter:

A ScanFilter allows clients to restrict scan results to only those that are of interest to them.

  • setServiceUUID: We are filtering using the Service UUID which we sent in our advertisement packet.

ScanSettings:

Bluetooth LE scan settings are passed to BluetoothLeScanner#startScan to define the parameters for the scan.

  • setScanMode: We are setting the scan mode based on the number of unique scans/interval and changing it accordingly to provide lower power consumption. The scan mode can be one of ScanSettings#SCAN_MODE_LOW_POWER, ScanSettings#SCAN_MODE_BALANCED or ScanSettings#SCAN_MODE_LOW_LATENCY.
  • setMatchMode(Added in API level 23): We have set it MATCH_MODE_STICKY (For sticky mode, a higher threshold of signal strength and sightings is required before reporting by h/w)
  • setPhy(Added in API level 26): Setting the Physical Layer to use during this scan. This is used only if ScanSettings.Builder#setLegacy is set to false. To improve scan performance, we have used PHY_LE_1M channel for scanning. Read more about it here.

ScanCallback:

Scan results are reported using these callbacks.

In onScanResult,

  • We get out scanResult from which we parse the device name, RSSI and txPower(Added in API 26).
  • Change the scan and advertisement mode for the device accordingly.
  • Store the scanResult in our local storage i.e database.

Also, we are checking the duplicacy of a scan record within a few seconds before we add it to our local storage.

Android 7.0 introduced a BLE scan timeout, where any scan running for 30 minutes or more is effectively stopped automatically and can only resume “opportunistically”. So we are starting the scan again after an interval.

Adaptive Scanning

We have implemented adaptive scanning through which we are changing the scan and advertisement mode of the scanning device based on the number of unique scans/interval. As scan and advertisement modes are directly related to power consumption, this has helped us in adjusting the consumption based on the number of close contacts. So, you can now worry even less for your battery power usage. Cheers !!

App to scan continuously

To make our app scan continuously, we are doing all this in a Service and to cater to the background service restrictions in Android API 26 or higher, we are using startForegroundService which is similar to startService(android.content.Intent), but with an implicit promise that the Service will call startForeground(int, android.app.Notification) to make the service run in the foreground. We use this method if killing your service would be disruptive to the user. Read more about the foreground mechanism here.

Bluetooth Vulnerabilities

There are vulnerabilities in Bluetooth technology that have to be patched at the operating system-level, and we, therefore, urge users to ensure that their operating systems are regularly patched.

That’s all from my side for this article. :-)

Originally published here.

We have tried to make the most out of the Bluetooth and we are constantly improving.

Check out my talk on Understanding the BLE in integration in AarogyaSetu:

Here is the link for Slides:

https://speakerdeck.com/niharika28/understanding-the-core-of-aarogyasetu-app-bluetooth-low-energy-ble

You can find the source code here:

I request everyone to download the Aarogya Setu app and be part of this fight against the COVID-19 pandemic.

Thank You!!

I am fortunate enough to be a part of the magnificently electrifying @SetuAarogya team. Thanks to the Government Of India for giving me this opportunity to work on a project that has an impact on human lives. Stay Safe!!

Please feel free to share your views and feedback in the comments section below. You can also reach out to me on LinkedIn and Twitter.

मैं सुरक्षित, हम सुरक्षित, भारत सुरक्षित!

AarogyaSetu

Aarogya Setu

AarogyaSetu

Aarogya Setu is a Govt of India effort to build a holistic contact tracing application for the safety and timely help of Indian Citizens

Niharika Arora

Written by

Google Developer Expert for Android | Senior Android Engineer @ 1mg | Know more here: https://thedroidlady.com/

AarogyaSetu

Aarogya Setu is a Govt of India effort to build a holistic contact tracing application for the safety and timely help of Indian Citizens

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store