Flutter is Google's new cross platform mobile framework, built to capture the native performance of both iOS and Android devices. Applications are compiled ahead-of-time into a native Arm library, so there is no JavaScript bridge in the middle (unlike React Native and other cross-platform frameworks).

Flutter's improved efficiency is one reason why it is gaining more popularity than other hybrid frameworks. Even though Flutter only came out of beta in December 2018, it has already been adopted by large organisations such as Alibaba, JD.com and Abbey Road Studios.

For these reasons, it seemed like a no-brainer to offer a Chirp plugin for the framework using our existing iOS/Android SDKs. You can use the plugin directly from Flutter's package manager - ChirpSDK.

TL;DR: Go straight to the example application here.


Setting up your Chirp Flutter application

To get started with Chirp in your own Flutter app, just add the plugin to your pubspec.yaml.

dependencies:
  flutter:
    sdk: flutter
  chirpsdk: ^0.1.0

and run flutter packages get to install ChirpSDK for your app.

Next, we need to configure the SDK. You will need to enter your Chirp application key and secret, and also a config string to configure the SDK's audio properties. These can be copied and pasted into your app from the Chirp Developer Hub.

String _appKey = '<APP_KEY>';
String _appSecret = '<APP_SECRET>';
String _appConfig = '<APP_CONFIG>';

We need to create some asynchronous functions to make calls to the SDK. ChirpSDK for Flutter uses channels to pass messages to the native iOS/Android SDKs. So that we don't block the UI passing these messages to the native libraries, we set them up to run in the event loop.

Future<void> _initChirp() async => await ChirpSDK.init(_appKey, _appSecret);
Future<void> _configChirp() async => await ChirpSDK.setConfig(_appConfig);
Future<void> _sendChirp(Uint8List data) async => ChirpSDK.send(data);
Future<void> _startAudioProcessing() async => await ChirpSDK.start();

Then we can configure the SDK and start the audio processing inside the initState method.

@override
void initState() {
  super.initState();
  _initChirp();
  _configChirp();
  _startAudioProcessing();
}

Permissions

ChirpSDK will require permission to use the device's microphone to receive data. A simple way to achieve this is to use the simple_permissions plugin. So just add this dependency to your pubspec.yaml as well.

dependencies:
  flutter:
    sdk: flutter
  chirpsdk: ^0.1.0
  simple_permissions: ^0.1.9

Then create a function to request for microphone permissions within the State. This should be called before initialising the SDK.

Future<void> _requestPermissions() async {
  bool permission = await SimplePermissions.checkPermission(Permission.RecordAudio);
  if (!permission) {
    await SimplePermissions.requestPermission(Permission.RecordAudio);
  }
}

Sending data

Chirp SDKs accept data as an array of bytes, creating a versatile interface for all kinds of data. However in most cases, Chirp is used to send a short identifier. Here is an example of how to send a short string.

String identifier = "hello";
var payload = new Uint8List.fromList(identifier.codeUnits);
_sendChirp(payload);

Receiving data

To receive data from the SDK, all you need to do is attach a listener to the onReceived event stream. This example shows how to convert the received payload from a Uint8List back to a String.

ChirpSDK.onReceived.listen((e) {
  String identifier = new String.fromCharCodes(e.payload);
});

Errors

Similarly, a stream of any errors that have occurred is available. During development it is useful to print these somewhere on the screen.

ChirpSDK.onError.listen((e) {
  setState(() {
    _chirpErrors = e.message;
  });
});

See the example code for a full list of available callbacks.

This project aims to be a community driven project and is open to contribution. Please file any issues and pull requests at the Chirp Flutter GitHub repo.


Chirp is distributed with a generous free plan for developers and start up businesses, so you can start using data-over-sound in your own projects right away.

Thanks!

joe@chirp.io