pdPurpledecks ← Case studies Start a conversation
Consumer health · Sonar · SDK

Sonar sleep tracking SDK, on the phone's own speaker and microphone

Client · ResMed End product · SleepScore

The SDK does its work on the device. No audio leaves it.

PURPLEDECKS · EST. 2012 · 53.7°N 7.8°W · IRELAND · EU

Outcome

Purpledecks built the iOS and Android SDK that put ResMed's sonar sleep tracking on a phone. The sonar science is ResMed's. The SDK a licensee could build a product on is ours.

That licensee shipped SleepScore, announced as the world's first non-contact sleep tracking app. It ships on that SDK today.

The brief

ResMed measure sleep with sound. The phone's speaker sends out a tone you cannot hear. The microphone reads what comes back off your breathing and your movement. Nothing worn, nothing attached.

They already had a bedside device doing this. The brief was to do the same job with nothing but the phone, and lose nothing on the way. What they had was an algorithm, written in C by their own researchers. What they needed was something a third party could build a product on.

The research first

The science was proven. The question was the hardware. A phone's speaker and microphone are built for calls and music, not for sensing, and there are thousands of models of each. Before anyone builds an SDK, someone has to prove the parts can do the job at all.

So the engagement began as research. A staged investigation, on real handsets: could the phone's own speaker and microphone be driven precisely enough, and read raw enough, untouched by the phone's own processing, for ResMed's signal science to work with. They could. The research also mapped what the build would be up against: phones that differ from maker to maker, slow themselves when nobody touches the screen, and must not drop a sample all night. The science was ResMed's and already proven. What the research proved was that consumer hardware could carry it. Then the SDK.

What Purpledecks built

The SDK

The sleep tracking library, and the interfaces a licensee builds against. The algorithms are ResMed's, written in C. On iOS that C plugs into the normal app layer. Android's managed runtime cannot call it directly, so on Android we went down to the NDK to reach the algorithm and the native audio layer beneath it. One algorithm, two platforms, two depths of integration.

The host app

A working app built around the SDK, so the SDK could be tested on real devices and handed over ready to build on. The licensee shipped their own app, not this one. Shipping a library is one thing. Making it adoptable is the other, and it is the part that gets skipped.

Protecting what was not ours

The algorithm ships as object code, so a licensee gets the capability without getting the science. The public interface stays legible, so they can still work with it.

The SDK was licensed to a third party who built their own product on it. That product is SleepScore. ResMed is the client, the science and the algorithm are theirs, and what we built is the SDK and the app around it.

A phone on a bedside table showing the SleepScore app's sleep score screen.
SleepScore, the end product the SDK shipped in. The app is the licensee's own. We built the SDK underneath it.

The hard part

You are running a speaker at a sleeping person, every night, for years

The sensing is inaudible, and it still has to sit inside the continuous exposure limits for audio safety. It has to do that without heating the phone, without draining the battery any harder than an ordinary app that plays and records at once, and without asking more of the speaker and microphone than they can give night after night.

Android does not agree with itself

Devices and OS versions kill background processes on their own terms, and this is the heaviest thing the app does. A partial wake lock keeps the CPU from going to sleep through a session. The SDK reports its own state the whole way through, started, running, paused, resumed, finished, failed, so the licensee's app always knows where it stands. A session that dies is a session the app knows died.

A night can be quietly wasted

The phone gets picked up at three in the morning. Someone plays music. There is noise in the room. The SDK watches its own environment and pauses when the reading stops being trustworthy, rather than recording something it cannot stand over. It also flags a bad setup at the start, before the night is spent rather than after it.

The failure here is silent, device-specific and unpredictable, and each real test run costs a night. Sleep data cannot be backfilled. The user wakes to nothing and blames the app, not the phone.

How we knew the SDK worked

Two tracks

Known sleep files injected to prove the processing path. Fast, repeatable, and it catches what the maths can catch. Then continuous overnight running on real devices for everything only a real night reaches. The OS. Memory. Whether the process is still alive at six in the morning.

Make the untestable testable

ResMed's algorithm arrived as a C library. We wrapped it so it could be swapped out under test. That is what let the SDK be tested automatically, without the real library present.

Sonar phones cannot share a room

Every device under test is emitting sound. Put them in one room and they interfere with each other. The rig had to physically separate them.

Both teams ran real devices continuously, day and night, in parallel, for as long as it took. And the work was delivered against a formal, numbered, revision-controlled requirements specification with a signature matrix. We deliver inside our clients' quality systems rather than holding one of our own.

Next

A research group had proven science, the science was a C library, and it was not a product yet. That is the problem we take on.

Start a conversation The science works. Now it has to be a product. →

Checked by Brian Egan, who worked on this engagement.

Purpledecks is a senior software engineering firm based in Ireland, in the EU, working since 2012. We build and rescue software that has to work.