Joe Todd
Joe Todd
May 25, 2018 · 4 min read

This is a brief overview on how to use Firebase with Chirp — for more detailed step by step instructions for building the iOS app, click here.

In this tutorial we will run through setting up an iOS application with Chirp Connect and Firebase.

Chirp Connect enables your apps to send and receive data using sound. This means that data can be transmitted to any device within hearing range, with no device pairing necessary. As the frequency of sound is relatively low, the data rate is limited, so Chirp Connect will send a shortcode associated with the data instead, and this is where Firebase comes in.

Firebase is an invaluable resource for many developers, providing a managed cloud based infrastructure which is easily integrated and ideal for mobile development. It offers many different services including authentication, real time databases, and file storage which is all included in a generous free plan.

Firestore is a scalable NoSQL database which keeps your data in sync using realtime listeners. Although technically a schemaless database, you have the ability to reference other documents much like a relational database, which gives you much more powerful querying options and more controlled access to your data.

We will be using Firestore to hold records of our data, Storage to store the data in the cloud, and Chirp Connect to send the data to anyone nearby.

Chirp & Firestore in action

Before we get started you will need to set up the following requirements.

Xcode

Install from the App Store

CocoaPods

sudo gem install cocoapods

Chirp Connect iOS SDK

Sign up at developers.chirp.io


We will be using Swift to write the code, but before that we need to set up the project.

1. Create an Xcode project.

2. Sign in to Firebase and create a new project.

Enable Firestore by clicking into the Database section and selecting Cloud Firestore.

If you are not authenticating users then you will need to open up the rules for Storage in the console, with allow read, write;

3. Run through `Set up your iOS app` on the `Project Overview` page. After this step you must close the .xcodeproj and use the .xcworkspace instead.

You will need the Bundle Identifier from the General Tab in your Xcode Project Settings.

Once the Podfile is created you will need to add the following dependencies, before running pod install.

# Pods for <project>
pod 'Firebase/Core'
pod 'Firebase/Firestore'
pod 'Firebase/Storage'

4. Download the latest Chirp Connect iOS SDK from the developer hub.

5. Follow steps at developers.chirp.io to integrate Chirp Connect into Xcode.

Go to Documentation / Getting Started / iOS and follow the Swift set up instructions.
This will involve importing the framework, creating a bridging header and allowing microphone permissions.

6. Now the project is all set up we can start writing some code. So let’s initialise Chirp Connect in the `viewDidLoad` method. Your application key and secret can be copied from developers.chirp.io.

let connect: ChirpConnect = ChirpConnect(appKey: APP_KEY, andSecret: APP_SECRET)connect.getLicenceString {
(licence: String?, error: Error?) in
if error == nil {
if let licence = licence {
connect.setLicenceString(licence)
connect.start()
}
}
}

7. And now we can start integrating Firebase, so import Firebase into your ViewController.

import UIKit
import Firebase

Chirp Connect payloads are just byte arrays, which are represented by the NSData type in iOS. To convert the payloads to a hexadecimal string we need to create an extension for NSData (placed outside of the ViewController class).

extension Data {
var hexString: String {
return map { String(format: "%02x", UInt8($0)) }.joined()
}
}

We will use Chirp Connect to generate a random identifier, and upload this to a collection in Firestore called uploads along with a timestamp.

let key: Data = connect.randomPayload(withLength: UInt(connect.maxPayloadLength))Firestore.firestore().collection("uploads").addDocument(data: [
"key": key.hexString,
"timestamp": FieldValue.serverTimestamp()
]) { error in
if let error = error {
print(error.localizedDescription)
}
}

Once this is complete, we will upload our data to Storage using the same identifier. This could be images, text, videos etc, but in this example is image data.

When the upload is complete, we will send the identifier encoded as a sequence of tones using Chirp Connect.

let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
Storage.storage().reference().child(key.hexString).putData(data, metadata: metadata) {
(metadata, error) in
if let error = error {
print(error.localizedDescription)
} else {
connect.send(key)
}
}

On the receiving side we will retrieve the data directly from Storage using the received payload.

connect.receivedBlock = {
(data: Data?, channel: UInt?) -> () in
if let data = data {
print(String(format: "Received data: %@", data.hexString))
let file = Storage.storage().reference().child(data.hexString)
file.getData(maxSize: 1 * 1024 * 2048) { imageData, error in
if let error = error {
print(error.localizedDescription)
}
}
} else {
print("Decode failed");
}
return;
}

And there you have it, a simple application that allows you to send and receive any kind of data with sound.

For further documentation for iOS and other platforms including Android, Python and JavaScript, please visit developers.chirp.io.

Joe Todd

joe@chirp.io


Chirp is a technology company enabling a seamless transfer of digital information via soundwaves, using a device’s loudspeaker and microphone only. The transmission uses audible or inaudible ultrasound tones and takes place with no network connection. To learn more visit chirp.io

Chirp

Written by the team at Chirp, the world's most trusted data-over-sound technology used by the leading brands in over 90 countries. Find out more at www.chirp.io

Joe Todd

Written by

Joe Todd

Chirp

Chirp

Written by the team at Chirp, the world's most trusted data-over-sound technology used by the leading brands in over 90 countries. Find out more at www.chirp.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade