Getting Started with Chirp(Android)

Shrey Sindher
ACM VIT
Published in
5 min readFeb 1, 2020

Chirp SDKs take data, convert it into sound, which nearby devices decode seamlessly back into data. It is robust to background noises, easy to integrate, consumes even less power than Bluetooth LE and is cross-platform.

The most common app which uses audio QR is the Google Pay app.

Chirp is like an audio QR code.

It sends data seamlessly over sound waves to enhance end-user experiences and adds value to existing hardware. It uses any existing speaker or microphone, and is configurable to use audible or inaudible near-ultrasonic frequencies.

Chirp is designed for simplicity, eliminating connectivity headaches and simplifying everyday tasks like connecting to Wi-Fi networks, sharing contact details, and making peer-to-peer payments.

Before we dive-in

This tutorial is divided into two sections. First, we’ll see how to encode and send data. And then, we’ll see how to receive and decode it.

Set up our Project

  1. Open Android Studio and click on ‘Start a new Android Studio project’.

2. Select Empty Activity.

3. Name your application. In this example, I’ve decided to go with ‘ChirpTut’. Please ensure that you use “Use AndroidX artifacts” feature and it has been enabled. Click on Finish.

4. Once the project is completely loaded, go to project level Gradle file and under repositories, add the maven repo and do a Gradle sync.

repositories {
google()
jcenter()
maven {
url "https://maven.chirp.io/release"
}
}

5. After the sync is finished, go to app-level Gradle file and under dependencies, add

implementation "io.chirp:chirpsdk:3.10.+"

At the time of writing the latest version of Chirp is ‘3.10.0’. After adding, again sync the Gradle files.

6. In your ‘AndroidManifest.xml’, add the following permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

7. Modify your ‘styles.xml’ file like this:

Getting Credentials from Chirp.io

While using Chirp is free, signup is necessary.

  1. Go to the developers page on their website and sign up.
  2. After logging in, go to ‘Applications’ where you can see your credentials.
  3. Select configuration as ‘ultrasonic’ and then click on SAVE.
  4. The credentials will be generated. Copy and paste them in Constants.java.

Now, that we have set all this, we are ready to start coding 😉.

Let’s Code

Our activity_main.xml will be used as a landing screen to allow users to either send or receive data.

Here is the code for the layout of activity_main.xml and MainActivity.xml

Create two new empty activities: SendActivity and ReceiveActivity.

First, let’s start off with the SendActivity:

Here’s how our activity_send.xml will look like. Nothing fancy.

For our java code, we first initialize the Chirp SDK. Then we define the ChirpEventListener which listens to the changes and provides various callbacks.

  • onReceived is called when data is received by the SDK. Here we write code to decode the received data.
  • onReceiving is called when the SDK has started to receive some data.
  • onSending is called when the SDK starts sending some data.
  • onSent is called when the SDK has sent some data.
  • onSystemVolumeChanged is called only on mobile platforms when the volume is changed.
  • onStateChanged is called when the SDK’s state is changing.

To learn more about these callbacks, visit https://developers.chirp.io/docs/using-chirp/callbacks#callbacks .

There are 5states which are defined inside onStateChanged method.

  • CHIRP_SDK_STATE_NOT_CREATED the SDK has not yet been initialised
  • CHIRP_SDK_STATE_STOPPED the SDK is not running or processing audio
  • CHIRP_SDK_STATE_RUNNING the SDK is running and processing audio
  • CHIRP_SDK_STATE_SENDING the SDK is sending data
  • CHIRP_SDK_STATE_RECEIVING the SDK is receiving data

After that, we write methods to start the Chirp SDK, namely startSdk() . Then, we override onResume(), onPause(), onDestroy() methods.

  • Inside onResume() method, we check if the microphone permission has been granted. If it is granted, we call startSdk(). Else, we request it from the user and override onRequestPermissionsResult() method.
  • Inside onPause() method, we write code to stop the ChirpSDK.
  • Inside onDestroy() method, we write code to close the ChirpSDK.

Once all this is set up, we set an OnClickListener on our “SEND” button. We take the user input, convert it into a byte array and then send it. For this app, I have chosen to show all the messages in a snackbar. The method for it is written in showConfirmationSnackbar() .

At this point, we have our complete activity to send any key. Now it is time to receive the data from another device.

Now on to the ReceiveActivity:

This activity will listen for any incoming data, and receive and display it. Here’s how our activity_receive.xml will look like.

First, we initialize the ChirpSDK in the onCreate() method. Then we write code for the ChirpEventListener. As stated earlier, this time, we will write code in the onReceived() method.

Here, we decode the incoming byte array and convert it into a string. We display this received string in a TextView. Now, remember, we can not just use the #SetText method of the TextView to set the text. It will cause an app crash since the view hierarchy is only accessible to the thread which created it. Chirp works on a separate thread. So, we have to call the #SetText method from outside the listener. That’s why we run it on the UI thread (See the updateStatus() method).

Make sure to handle the permission for audio in this activity as well.

If all goes well, you should be able to send and receive text using the app. Install the app on two different devices to send key from one device, and receive on the other.

Here is the code for ReceiveActivity.java:

For the complete project files, visit:

Thank You for reading :) I would love to discuss and solve any of your queries.

--

--