# Contact Tracing - Part 3

# Contact Trace
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](https://charlesmuchene.hashnode.dev/contact-tracing-part-1-ck912ur0501uo7vs15n6sk5kh) and [Part 2](https://charlesmuchene.hashnode.dev/contact-tracing-part-2-ck92lkork035u7vs14fn9jwre) 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.

![ble_trace.001.jpeg](https://cdn.hashnode.com/res/hashnode/image/upload/v1587093662715/Yo874tGzQ.jpeg)

To establish a connection, the 2 devices must be working in the Central/Peripheral roles. 

1. The **Peripheral** advertises *Trace UUID* in its advertisement data
2. **Central** only scans for devices advertising this *Trace UUID*
3. After detection of the desired UUID, the **Central** initiates connection
4. **Central** asks **Peripheral** for its services (again filtering only services it desires -- *TraceService*)
5. Upon receiving the services, **Central** requests for *TraceService* characteristics
6. From the received characteristics, **Central** asks to read the value of the *TraceToken* characteristic
7. **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:

## Peripheral
Android advertises Trace Service UUID

%[https://gist.github.com/charlesmuchene/ffe8efa183a5b770a04785a68f85a80a]

Starts GATTServer to serve requests from remote Central device

%[https://gist.github.com/charlesmuchene/58c63827ba330f52c3272d48d032ca39]

## Central
After `CBCentralManagerDelegate` reports *.poweredOn* state, iOS's `CentralManager` starts scanning for devices (Peripherals) with TraceService UUIDs

%[https://gist.github.com/charlesmuchene/f89ac3a2cea3937583065dc66d25a85c]

After connecting to a peripheral, we set up a `CBPeripheralDelegate` to receive peripheral related callbacks.

%[https://gist.github.com/charlesmuchene/71d99f672a67ba2ef52cf542767bc4a9]

The implementation also has the reverse roles for both of these platforms to facilitate duplex token exchange.

## Conclusion
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!

## References
- 
Apple's [Core Bluetooth Framework](https://developer.apple.com/documentation/corebluetooth)
- 
Android's BLE [overview](https://developer.android.com/guide/topics/connectivity/bluetooth-le)
