Case study · Android app · Side project
Traffic Alert: warnings the moment you get in the car
An Android app that starts by itself when the phone connects to the car and gives you an early warning of accidents, closures and queues on the roads around you, including your daily commute. It reads them out through the car speakers and opens Google Maps in one tap.
- Role
- Design & development
- Platform
- Android 13 and later
- Traffic data
- TomTom Traffic API
- Status
- In testing
Traffic data © TomTom. Traffic Alert is an independent project, not affiliated with Google or TomTom.
The problem
Stuck on the A1237, with no warning
I started this sitting in my car, waiting to pick my daughter up from work, looking at the traffic chaos on the A1237, York's northern ring road. Google Maps will route you around trouble once you've set a destination, but on a trip you know by heart you rarely set one. I wanted to know about a problem nearby before I set off, without touching the phone.
The brief I gave myself was four lines: the phone connects to the car, the app checks the roads, it alerts me straight away if there is a problem, and Google Maps is one tap away.
What it's for, and what it isn't
Traffic Alert is only meant to run when the phone connects to the car. Its job is that first warning as you set off, about problems on local roads or on your daily commute, so you can choose another way before you're committed. It keeps an eye out while the car stays connected, but it isn't a sat nav and doesn't try to replace one. For directions, it hands you straight to Google Maps.
What it does
Get in, and it's already watching
-
Starts with the car
When the car's Bluetooth connects (including Android Auto), the app starts checking. When the car disconnects, it stops.
-
Reads alerts out
New problems are spoken through the car speakers, and music is turned down while it talks, so your eyes stay on the road.
-
One tap to Google Maps
Tap an alert to open Google Maps at the incident, or use the big Open Google Maps button.
-
Only what matters
Accidents and road closures always come through. Queues, roadworks and weather are filtered by the severity you choose.
-
Choose your area
Choose a radius of 5, 10, 15 or 25 km, and switch off any type of problem you never want to hear about.
-
No account
Settings stay on the phone, and the app talks to TomTom directly with your own free API key.
Choosing the data
Why TomTom, when the button says Google Maps
The first technical decision was where the traffic data would come from. Google Maps has no public API for traffic incidents, so the app can't ask Google what's happening on the A1237.
TomTom's Traffic Incident Details API (version 5) can. Given a bounding box, it returns current accidents, closures, roadworks and jams, each with a category, a delay magnitude, the delay in seconds, road numbers, start and end points, and the incident's shape on the road. Google Maps is still used for what it's best at: looking and navigating.
Asking for less
The request uses TomTom's fields parameter to ask for only the properties the app uses, and timeValidityFilter=present to leave out planned works that haven't started. Smaller responses matter on a car's patchy mobile signal.
Before you start
You'll need a free TomTom API key
Traffic Alert won't check anything until you paste in your own TomTom API key; until then it simply asks for one. Getting a key is free, takes a few minutes at developer.tomtom.com and needs no credit card. Asking every user for a key was a deliberate choice.
TomTom needs a key on every request
With no traffic incident API from Google, the data has to come from TomTom, and TomTom only answers requests that carry an API key. The key identifies who is asking and counts their usage against an allowance.
A key built into the app wouldn't stay private
Anything inside an Android app can be unpacked and read, so a key shipped with the app would soon be public. Anyone could copy it and use it for something else.
One shared key would run out
Every user would also be drawing on the same allowance. Once it ran out, TomTom would refuse requests and the alerts would stop for everyone, unless I paid for everyone's usage.
Your own key keeps it simple and private
The other option is a server of my own that holds the key and passes requests on. That means hosting to run, accounts to manage, and everyone's location going through my server. With your own key the phone talks straight to TomTom: your location only goes to TomTom, and there's no account with me at all. One driver's checks fit comfortably within TomTom's free allowance.
The hard part
Getting Android to start an app on its own
Since Android 12, apps can't start a foreground service from the background, which is exactly what an app that wakes up when you get in the car needs to do. A normal Bluetooth broadcast receiver can see the car connect, but Android refuses to let it start a location service.
The way through is Android's Companion Device Manager. When you tap Choose car, the app asks the system to link it to your car's Bluetooth. That link is what earns the exemption: the app is allowed to start its foreground service from the background, and the system itself wakes a CompanionDeviceService when the car appears or disappears.
Two triggers, one service
The app listens in two ways: the companion presence callbacks, and a receiver for Bluetooth connect and disconnect events, which Android still delivers to apps that declare them. Either one starts the same monitoring service, which ignores a second start if it's already running. Every device address is checked against the chosen car, so your headphones don't set it off.
A better car picker
The first version scanned for new Bluetooth devices nearby. Testing showed up a problem with that, so choosing a car now starts from the Bluetooth connections already set up on the phone: your car is already paired, so there's nothing to scan for. The app lists those devices, then hands the one you pick to the Companion Device Manager with a single-device filter, so the system only has to confirm it.
Failing clearly
If Android still refuses a start, or location permission is missing, the service catches the error, stops itself, and shows the reason on the app's screen instead of failing silently.
How a check works
From connecting to the first warning
-
Step 1: The car connects
The first check runs as soon as the car connects, which is the warning that matters most. While the car stays connected, it checks again every five minutes.
-
Step 2: Find the phone
Ask for a fresh location fix, waiting up to 20 seconds, then fall back to the most recent known location so a check never stalls.
-
Step 3: Draw the box
Turn the radius into a bounding box around the phone, allowing for lines of longitude getting closer together further north.
-
Step 4: Ask TomTom
Fetch current incidents in that box, with clear messages for a rejected key or a rate limit, and retry on the next check.
-
Step 5: Measure to the road
Work out each incident's distance from the phone, then drop anything outside the circle or below the chosen severity.
-
Step 6: Alert once
Notify and speak only for incidents that are new, or have got worse, since the last alert.
Under the hood
The details that make it usable
Distance to the road, not to a pin
An incident can be a single point or a line along the road, sometimes several kilometres long. Measuring to its first point would make a long queue look further away than it is. The app measures the shortest distance from the phone to every segment of the line, using a flat projection that's accurate at these distances, and a unit test checks that a point beside the middle of a long segment comes out at nearly zero.
The bounding box is a square, so its corners sit outside the circle you chose. Filtering by real distance afterwards drops anything in those corners, so you only hear about problems inside your radius.
Severity rules
TomTom rates each incident's delay from 0 (unknown) to 3 (major), and uses 4 for events with no delay figure, such as closures and roadworks. Accidents and closures always alert. A 4 on anything else is treated as moderate, so roadworks appear at the moderate setting but not at major only. Alerts are sorted with accidents and closures first, then by distance.
Muted incident types are saved as a list of what's switched off, not what's on, so any new type added in a later version is on by default.
No repeated alerts
The service remembers the delay level it last alerted at for each incident. An incident that stays the same is shown in the app but not announced again. One that gets worse, say a minor queue that turns major, is.
Speaking through the car
Spoken alerts use Android's text-to-speech with the audio marked as navigation guidance, the same as directions from a sat nav, so they play through the car speakers over Bluetooth or Android Auto. The app asks for temporary audio focus with ducking, so music dips instead of stopping, and keeps a count of messages still being spoken so it hands focus back only when the last one finishes. Alerts that arrive before the speech engine is ready are queued, and only the first three of a batch are read out, after a line such as "2 traffic problems nearby".
An incident becomes one natural sentence, for example: Accident on the A64, Grimston Bar to Dunnington. Delay about 12 minutes. 3 kilometres away.
One tap to Google Maps
Each alert opens a geo: link at the incident, labelled with its title, and sent straight to Google Maps when it's installed, or to any map app when it isn't. The ongoing notification has Open Maps and Stop buttons, so you rarely need to open the app at all.
Where it's at
Small, tested, and quick to build
- from the first commit to severity filters, type toggles and a new car picker
- 6 hrs
- unit tests for parsing, distances, filtering and the spoken summary
- 8
- servers or accounts: settings stay on the phone
- 0
- between checks after the first one, while the car is connected
- 5 min
On real journeys it has worked as expected every time: as soon as my phone connects to the car's Bluetooth, it reads out every incident within the radius I've set. It's a light service for now, but a working one, and I'll keep adding features to it.
How I built it
AI as a pair programmer, again
-
Step 1: Started from four lines
Sent Claude the four-line brief from my phone, still sitting in the car. My setup is connected to GitHub, so a first prototype was waiting by the time I got home. From there I built it out with Claude as a pair programmer, reviewing every change before merging.
-
Step 2: Kept it plain
Kotlin, Jetpack Compose and Android platform APIs, with no networking or JSON libraries, so there is very little to keep up to date.
-
Step 3: Tested the logic on the JVM
Parsing, geometry, severity rules and the spoken sentence are plain Kotlin, tested against a sample TomTom response set in York.
-
Step 4: Built in CI
GitHub Actions runs the unit tests and builds an installable APK on every push, so I can install a new build straight from my phone.
What I learned
Lessons from the build
The platform sets the design
The biggest decision was how Android lets an app start in the background, and getting that right first shaped everything else.
Check the API exists
The brief said Google Maps, but the data had to come from somewhere else. Separating where the data comes from and where the user acts kept both parts simple.
Design for the driver
Speaking alerts, alerting once per problem and one-tap actions all come from the same rule: you shouldn't need to look at the phone.
What's next
From the ring road to the whole commute
Planned
- Commute mode, to watch a usual route such as home to work as well as the circle around you
- A stable signing key, so updates install over the top
Exploring
- An Android Auto screen, using the Car App Library
- Android
- Kotlin
- Jetpack Compose
- Companion Device Manager
- Foreground services
- Text-to-speech
- TomTom Traffic API
- GitHub Actions
- AI-assisted development
Got an app idea?
hello@robertwood.me