Reducing Connection Friction
In a future world filled with wireless audio broadcasts, from public announcements at airports to silent TVs at the gym, finding and connecting to the right audio source can be a clumsy process. Remember, an Auracastâ„¢ assistant's job is to scan for broadcasts and provide a UI for users to join them. But how does the assistant know which of the dozens of potential broadcasts is the right one?
One elegant solution is the Broadcast Audio URI (BAU), a standardized link format designed to share information about an audio broadcast. While my previous posts have shown how this can be shared via QR codes, but here we will explore how a similar experience could be made, using Near Field Communication (NFC) and the Web NFC API.
Imagine this: you walk up to a screen, tap your phone on a small sticker, and your earbuds are instantly tuned to the correct audio stream.
A Quick Recap: The Broadcast Audio URI (BAU)
Before we look at the code, let's remember what a BAU is. It's a string that starts with BLUETOOTH:UUID:184F
and contains key-value pairs with information like the Broadcast ID (BI
), device address (AD
), and the Broadcast Name (BN
). This gives an assistant all the information it needs to find and synchronize to a specific broadcast stream.
A typical BAU might look like this:
BLUETOOTH:UUID:184F;BN:UHVibGljIE5ld3M=;BI:DE51E9;...
This tiny piece of text is the key to unlocking a specific audio broadcast. Now, let's put it on an NFC tag.
Reading the URI with Web NFC
The Web NFC API allows a web application, with the user's permission, to read from and write to NFC tags.
Note: In this post, we'll just cover how to the read the NFC tag.
The process involves two main steps:
- Initiating a scan and waiting for the user to tap a tag.
- Handling the
reading
event to process the data from the tag.
Let's build a simple function within a hypothetical web-based Auracastâ„¢ Assistant. This function will be triggered by a "Scan NFC" button.
async function scanForBroadcastURI() {
const ndef = new NDEFReader();
console.log("User activated NFC scan...");
try {
// Start scanning for NFC tags
await ndef.scan();
console.log("NFC Scan started. Please tap a tag.");
// Add a one-time event listener for the 'reading' event
ndef.addEventListener('reading', ({ message }) => {
console.log("NFC tag read.");
// Iterate over the records in the message
for (const record of message.records) {
if (record.recordType === "text" || record.recordType === "url") {
const textDecoder = new TextDecoder();
const uri = textDecoder.decode(record.data);
// Check if it's a valid Broadcast Audio URI
if (uri.startsWith("BLUETOOTH:")) {
console.log(`Broadcast Audio URI found: ${uri}`);
// Pass the URI to the assistant's connection logic
tuneToBroadcast(uri);
// We found what we needed, no need to process other records
return;
}
}
}
}, { once: true }); // Use { once: true } to automatically stop listening after one read
} catch (error) {
console.error(`NFC Scan failed: ${error}`);
}
}
function tuneToBroadcast(uri) {
// This is where the assistant's logic would parse the URI
// and instruct the underlying Bluetooth hardware to sync
// to the broadcast source.
console.log(`Tuning to broadcast with data: ${uri}`);
// e.g., connectToAuracast(parseBAU(uri));
}
In this code, we start a scan and listen for a reading
event. When a tag is successfully read, we decode its payload, check if there is a Broadcast Audio URI in the records, and then pass it off to another function to tune into the broadcast.
The Web NFC User Experience
With this implementation, the user journey becomes incredibly simple:
- The user opens our web-based Broadcast Assistant on their phone.
- They press a "Scan NFC" button, which calls the
scanForBroadcastURI()
function. - The user taps their phone against an NFC tag placed near the audio source (e.g., on a museum exhibit, a TV screen, or a conference hall door).
- The web app instantly reads the Broadcast Audio URI and automatically tunes the user's connected earbuds to the correct stream.
Conclusion
By combining the simple, physical interaction of NFC with the standardized data format of the Broadcast Audio URI, we can create a powerful and intuitive user experience for Auracastâ„¢. The Web NFC API provides the final, critical piece of the puzzle, allowing developers to build these advanced, hardware-integrated experiences directly for the web platform.
This is yet another testament to how the modern web is closing the gap, enabling interactions that were once the exclusive domain of native applications.
Enjoy ;)
Top comments (0)