LISNR Radius Android SDK 2.0.0
Code Examples

Introduction

This page contains links to four Android applications integrated with the Radius SDK; one for a unidirectional implementation, two for a bidirectional implementation, along with the Radius Sample Application. It also contains development information that is common when integrating the Radius SDK into an Android application such as how to Add Radius to Existing Application and Convert Payload Data.

Code Examples

Requirements

The following items are required for running the Hello World Applications or the samples:

Hello World Applications

Radius Sample Application

Along with the Hello World Applications, the LISNR team has developed the Radius Sample Application available for download from the LISNR Portal that demonstrates almost all transmitting , receiving, and transceiving features of the Radius SDK in the context of a fully functional application.

To Setup the Radius Sample Application:

  • Go to the LISNR Portal, navigate to the Help Center tab > Developer Resources > Choose "Android SDK" Section > Click the red "Download Sample App" button
  • Unzip the resulting downloaded lisnr-radius-sdk-android-<VERSION>.zip file
  • Open the sample > RadiusSampleApp folder application in Android Studio

NOTE: The Radius Sample App has the ability to record audio to a file on the device. This capability exists only at the application level and it is not a part of the LISNR Radius SDK itself. The LISNR Radius SDK does not have the ability to record audio.

When using the Radius Sample App for Android or iOS from the LISNR Portal, the SDK will automatically force the audio stream to input/output from the mobile device's speaker and mic even if the device is connected to a bluetooth speaker or regular speaker (3.5 mm aux). This is intentional and can be changed programmatically when developing with the LISNR Android and iOS SDKs. To test with an external speaker connected to a mobile app you can utilize our Web SDK sample app on a mobile device browser located at: http://websdksample.lisnr.com/. On this website you can connect your mobile device to the external speaker and the audio should play through the external speaker properly.


Add Radius to Existing Application

Download the SDK

  1. Login to the LISNR Portal
  2. Navigate to Help Center and click on Developer Resources
  3. Select the "Android SDK" section
  4. Click the red "Download SDK" button

Include the SDK

  1. Unzip the previously downloaded lisnr-radius-sdk-android-<VERSION>.zip file, copy the lisnr-radius-sdk-android-<VERSION>.aar, and put it into the "app" > "libs" folder
  2. In the app-level build.gradle file add the following to the dependencies section:
    implementation files('libs/lisnr-radius-sdk-android-<VERSION>.aar')
  3. If minifyEnabled and/or shrinkResources are set to true in the proguard-rules.pro file, add the following additional lines to the proguard-rules.pro file to ensure the Radius and Androidx Lifecycle objects are retained:
    -keep class com.lisnr.** { *; }
    -keep class androidx.lifecycle.** { *; }
  4. (In Android Studio) Click File->Sync Project with Gradle Files

Now you should be able to use the LISNR Radius Android SDK within the application.

Convert Payload Data

By default, the payload contents of a Tone (getData()) are a byte array. If you need to convert your payload from ASCII to a Byte Array for transmission or convert the byte array to ASCII when a Tone is received, see the sections below. To view a working application using these functions to transmit and receive Tones that contain ASCII data, see the Hello World - Unidirectional Transmitter and Receiver example.

ASCII to Byte Array

This code snippet shows how to create a LISNR Radius Tone with ASCII as the payload contents. This Tone object can then be transmitted using com.lisnr.radius.Transmitter.transmit() or com.lisnr.radius.Transmitter.beacon() methods.

...
String textToSend = "HelloWorld";
com.lisnr.radius.Tone toneToTransmit = new com.lisnr.radius.Tone(textToSend.getBytes()); //result of getBytes() is a byte array
...

Byte Array to ASCII

...
//'tone' variable is expected to be a Radius Tone object that was detected by a Receiver in the onToneReceived callback
String payloadAscii = bytesToPayloadString(tone.getData());
...
private String bytesToPayloadString(byte[] data) {
StringBuilder dataString = new StringBuilder();
for (int byteOffset = 0; byteOffset < data.length; byteOffset++) {
byte dataNybbleLow = (byte) (data[byteOffset] & 0x0F);
byte dataNybbleHigh = (byte) ((data[byteOffset] & 0xF0) >> 4);
dataString.append(nybbleToChar(dataNybbleHigh));
dataString.append(nybbleToChar(dataNybbleLow));
// Spaces for readability
dataString.append(" ");
}
final String[] strSplit = dataString.toString().split(" ");
dataString = new StringBuilder();
for (final String str : strSplit)
{
final char chr = (char) Integer.parseInt(str, 16);
if (chr < 0x20 || chr > 0x7E) {
// Display non-printable ASCII as a unicode '?'
dataString.append('\uFFFD');
} else {
dataString.append(chr);
}
}
return dataString.toString();
}
private char nybbleToChar(byte data) {
char dataChar;
if(data <= 9) {
dataChar = (char)('0' + data);
}
else {
dataChar = (char)('A' + data - 0x0A);
}
return dataChar;
}
...