Contact Trace is an Android + iOS solution to help trace contacts you come across. It uses Bluetooth Low Energy to exchange an anonymous token with devices nearby running the Contact Trace App. It has been quite a learning experience trying out different solutions to enable this functionality. Part 1 and Part 2 cover my approaches and the limitations I came across. In this article, we'll take a look at my last take on this.
Trial 3 - Bluetooth Smart - Connection Mode
Following the iOS limitations for a solution in Advertise Mode only, the next phase was establishing a Bluetooth connection between two nearby devices. This enabled the devices to discover each other's services and in addition, explore the characteristics in those services.
To establish a connection, the 2 devices must be working in the Central/Peripheral roles.
- The Peripheral advertises Trace UUID in its advertisement data
- Central only scans for devices advertising this Trace UUID
- After detection of the desired UUID, the Central initiates connection
- Central asks Peripheral for its services (again filtering only services it desires -- TraceService)
- Upon receiving the services, Central requests for TraceService characteristics
- From the received characteristics, Central asks to read the value of the TraceToken characteristic
- Peripheral responds with TraceToken characteristic value
The above diagram doesn't depict the some responses from either devices as well as the disconnection events both of which occur in the implementation.
Let's take the scenario where iOS will function as the Central and Android as the Peripheral device. The following is an implementation excerpt:
Android advertises Trace Service UUID
Starts GATTServer to serve requests from remote Central device
CBCentralManagerDelegate reports .poweredOn state, iOS's
CentralManager starts scanning for devices (Peripherals) with TraceService UUIDs
After connecting to a peripheral, we set up a
CBPeripheralDelegate to receive peripheral related callbacks.
The implementation also has the reverse roles for both of these platforms to facilitate duplex token exchange.
And there you have it - Contact Tracing app using Bluetooth Low Energy. There's a lot more that went into the app such as maintaining anonymity during tracking, handling permissions on both platforms, intricacies of background vs foreground processing, rotating scans and advertisements, among others.
This was quite an adventure and looking forward to more of them. Happy coding!