Build a COVID-19 Contact Tracing App using Flutter & Google Nearby API
Every country I see are using mainly 2 tactics to curb this COVID-19 epidemic. Lock-down & Contact Tracing App. Singapore has launched their own version of Contact Tracing App called TraceTogether which according to them took 40 engineers almost 10,000 man-hours to develop. So I thought, okay anyway I have nothing to do while sitting around in home all day during this lock-down. Why not try to develop a similar contact tracing app myself from scratch and see how far can I go.
Link to Github library: https://github.com/realdiganta/Contact-Tracing-App-TracerX
Okay firstly, lets see. What should be the core functionalities of a contact tracing app:-
- If I have the app, and I come near someone who also has the app, then without me doing anything, the app must somehow detect that there is someone else near me and store that data in a database.
- If someone I came in contact-with in the past 14 days is found to be COVID-19 positive, then the app must immediately notify me that this guy I came in contact with was found to be positive.
So after 7 days of hard work, I came up with something like this.
Lets call the app TracerX
So when anyone came near you (within a radius of 10–30m) TracerX would automatically detect it and add that guy’s username and some other data into your app. (Also the app must be running in the background and the other user must also have TracerX, otherwise detection is not possible.)
To detect another user, TracerX uses the Google Nearby API which uses a combination of Bluetooth, WiFi And BLE technology to detect other devices.
Lets go step by step and build this app.
Registering New Users
Firstly we need to register new users
We will be using firebase_auth library to register the new users into our application and store the data in firebase.
Detecting Other Users using Google Nearby API
Next and probably the most important part of the application is detecting new users. Fortunately this is not as hard as it looks.
We will be using the nearby_connections library from Flutter packages which implements Google Nearby API to detect new devices.
Once you register into the app, you will see this page.
You can see there is a “Start Tracing” Button on the page. When you click on that page your device will start tracing for other devices in a radius of 10–30 m and will also be visible to other devices in the same area.
Now lets go behind the code and see what happens.
It triggers a discovery function which starts discovering for other devices using the nearby_connections library. It also triggers an Advertise function which starts making the user’s device available to other devices too.
Its kind of like the bluetooth you use in your device. When you switch on bluetooth, then other devices with bluetooth switched on will start detecting you.
But you may notice one thing, that the users dont need to connect their devices to be discoverable. All this will happen whille your phones are still in your pocket.
As soon as a device is detected we store it in a Firebase Database in the collection named ‘met-with’. And by a device we mean, we store their usernames, that they used to register. i,.e the other devices can only discover the username and no other personal information.
But the users will be logged in with emails, not username, so we will use this function to extract the username of the email, from the database.
Now suppose your device discovers some other device with username riya.
Your screen will immediately show this: the username of the device and whether she is infected or not.
And if you meet another person. It will be added to the above list and the data will be stored in a Firebase Database.
However there are few limitations with the application:
- Very high range of 10–30m. This must be reduced to more like 2–3 m.
- Anonymization of data
Also many more features can be added:
- Details like location and time of contact may be added.
- Realtime information, warnings and notifications when near high risk areas(using GPS Heatmap) or high-risk individuals.
- Notifying a user when some of their contact is infected, is still to be added.
- Risk-scores will be assigned to individuals and this data will be public. People with high risk scores must self-isolate. As a result, people will themselves voluntarily do all the right things so as to maintain a low risk score. Calculation of risk scores will be done through a machine learning algorithm.
And that is the main reason I am writing this article. So that if some of you is working on this app or thinks about it may refer to this app I made. All the code is open-source here in github: TracerX
Also you may try adding the features I mentioned above as an exercise.
At a dark time like this, if I could be of any little help to anyone I would be very glad.
Thank You for your time.
For any other details please contact me at:- firstname.lastname@example.org