SDK quickstart
The Internet of things (IoT) connects physical objects with sensors, processors, and software to enable data exchange with other devices. In related applications, such as apps for smart door bells and remote monitoring, IoT devices send or receive audio and video streams to perform their function. Agora IoT SDK gives you the ability to enable live voice and video streams on IoT devices on a variety of platforms and for many use cases, while also offering a compact SDK size, low memory usage, and low power consumption. Some typical application scenarios for IoT SDK include:
-
Real-time monitoring: Integration into smart cameras and smart doorbells enables mobile device users to receive video feed from one or more cameras.
-
Two-way audio communication between mobile devices: With IoT SDK integrated into smart watches, users perform two-way audio communication between mobile devices.
This page shows the minimum code you need to integrate audio and video communication features into your IoT devices using IoT SDK.
Understand the tech
The lightweight IoT SDK is ideal for IoT applications due to its low power consumption. The SDK provides the following performance benefits:
- Small SDK size: The increase in app size is less than 400 KB after integration.
- Low memory usage: When the SDK is simultaneously sending and receiving 320x240
H.264
video data, memory usage is less than 2 MB. - High connectivity: Under normal conditions, connectivity is greater than 99%.
- Data transfer speed: A maximum of 50 Mbps for a user in a channel.
- Network adaptability: Non-aware recovery from up to 50% packet loss.
The following figure shows the workflow you need to integrate IoT SDK into your app:
Prerequisites
In order to follow this procedure you must have:
To test the code used in this page you need to have:
- Implemented either of the following:
-
A computer with Internet access.
Ensure that no firewall is blocking your network communication.
-
IoT SDK for Android supports the following ABIs:
- armeabi-v7a
- arm64-v8a
- x86
- x86-64
Project setup
To integrate IoT SDK into your app, do the following:
-
In Android Studio, create a new Phone and Tablet, Java Android project with an Empty Activity.
After creating the project, Android Studio automatically starts gradle sync. Ensure that the sync succeeds before you continue.
-
Add the IoT SDK to your Android project. To do this:
-
Download the IoT SDK and extract the archive to a temporary folder
<unzipped_package>
. -
Copy the following files and folders from
<unzipped_package>/agora_rtc_example_android/app/libs
to your project:File or folder Path in your project Files: agora-rtc-sdk.jar
/app/libs/
Folders: arm64-v8a
/app/src/main/jniLibs/
armeabi-v7a
/app/src/main/jniLibs/
x86
/app/src/main/jniLibs/
x86_64
/app/src/main/jniLibs/
-
In Android Studio, right-click the
/app/libs/agora-rtc-sdk.jar
file on the navigation bar and then select Add as a library.
-
-
Add permissions for network and device access.
In
/app/Manifests/AndroidManifest.xml
, add the following permissions after</application>
: -
Add sample audio and video files to the project resources folder:
- In Android Studio, right-click the
res
folder and choose New > Android Resource Directory. - Under Resource Type select raw and click OK.
- In the
/app/src/main/res/raw
folder, copy the filessend_audio_16k_1ch.pcm
andsend_video.h264
from the<unzipped_package>/agora_rtc_example_android/app/src/main/res/raw
folder.
- In Android Studio, right-click the
You are ready to add audio and video communication features to your IoT app.
Implement audio and video communication
This section shows how to use the IoT SDK to implement audio and video communication into your IoT app, step-by-step.
At startup, you initialize the engine. When a user taps a button, the app joins a channel and sends audio and video data to remote users.
Implement the user interface
In this simple interface, you create two buttons to join and leave a channel. In /app/res/layout/activity_main.xml
, replace the contents of the file with the following:
You see errors in your IDE. This is because the layout refers to methods that you create later.
Handle the system logic
Import the necessary Android classes and handle Android permissions.
-
Import the Android classes
In
/app/java/com.example.<projectname>/MainActivity
, add the following lines afterpackage com.example.<project name>
: -
Handle Android permissions
Ensure that the permissions necessary to use audio and video features are granted. If the permissions are not granted, use the built-in Android feature to request them; if they are granted, return
true
.In
/app/java/com.example.<projectname>/MainActivity
, add the following lines before theonCreate
method: -
Show status updates to your users
In
/app/java/com.example.<projectname>/MainActivity
, add the following lines before theonCreate
method:
Implement the channel logic
To start Agora engine and join a channel, take the following steps:
-
Import the IoT SDK classes
In
/app/java/com.example.<projectname>/MainActivity
, add the following lines after the lastimport
statement: -
Declare the variables that you use to join a channel and stream Audio/Video
In
/app/java/com.example.<projectname>/MainActivity
, add the following lines to theMainActivity
class: -
Setup Agora engine
To use IoT features, you use the IoT SDK to create an
AgoraRtcService
instance. You then use this instance to open a connection. In/app/java/com.example.<projectname>/MainActivity
, add the following code before theonCreate
method: -
At startup, ensure necessary permissions and initialize the engine
In order to send video and audio streams, you need to ensure that the local user gives permission to access the camera and microphone at startup. After the permissions are granted, you setup the IoT SDK engine.
In
/app/java/com.example.<projectname>/MainActivity
, replaceonCreate
with the following code: -
Receive and handle engine events
The
AgoraRtcService
provides a number of callbacks that enable you to respond to important events. To add an event handler to your app, in/app/java/com.example.<projectname>/MainActivity
, add the following lines beforesetupAgoraEngine
: -
Join a channel
When a local user initiates a connection, call
joinChannel
. This method securely connects the local user to a channel using the authentication token. In/app/java/com.example.<projectname>/MainActivity
, add the following code aftersetupAgoraEngine
:After joining an RTC channel, you can:
- Listen to the
onAudioData
callback to receive audio from users in the channel. - Listen to the
onVideoData
callback to receive video from users in the channel. - Call the
sendAudioData
method to send audio data. - Call the
sendVideoData
method to send video data.
- Listen to the
Send audio and video data
You send audio and video data in separate threads. The sending interval should be consistent with the video frame rate and audio frame length.
To stream audio and video data:
-
Add a
StreamFile
class for managing media filesYou use
StreamFile
to open, read, and close audio and video files. In Android Studio, select File > New > Java Class. Name the classStreamFile
and replace the contents of the file with the following code: -
Add a thread class to send audio
In Android Studio, select File > New > Java Class. Name the class
AudioSendThread
and replace the contents of the file with the following code: -
Add a thread class to send video
In Android Studio, select File > New > Java Class. Name the class
VideoSendThread
and replace the contents of the file with the following code:
Stop your app
In this implementation, you initiate and destroy the engine when the app opens and closes. The local user joins and leaves a channel using the same engine instance. To elegantly exit your app:
-
Leave the channel when a user ends the call
When a user presses the Leave button, use
leaveChannel
to exit the channel. In/app/java/com.example.<projectname>/MainActivity
, addleaveChannel
afterjoinChannel
: -
Release the resources used by your app
When a user closes the app, use
onDestroy
to release all the resources in use. In/app/java/com.example.<projectname>/MainActivity
, addonDestroy
afteronCreate
:
Test your implementation
To test your implementation, take the following steps:
-
Generate a temporary token in Agora Console.
-
In your browser, navigate to the Agora web demo and update App ID, Channel, and Token with the values for your temporary token, then click Join.
-
In Android Studio, in
app/java/com.example.<projectname>/MainActivity
, updateappId
,channelName
, andtoken
with the values for your temporary token. -
Connect a physical Android device to your development device.
-
In Android Studio, click Run app. A moment later you see the project installed on your device.
If this is the first time you run the project, you need to grant microphone and camera access to your app.
-
Click Join to join a channel.
You hear audio and see a video playing in the web demo app, pushed from the IoT SDK demo project.
-
Click Leave.
Audio and video streaming stops and you exit the channel.
Reference
This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.