In this tutorial, you’ll connect your Android phone or tablet to Aidlab using Unity. If you aren’t familiar with Unity, it’s a cross-platform game engine with a powerful GUI editor and robust asset store.
Note: This tutorial assumes you are familiar with Unity. If you are not, we encourage you to read Getting Started with Unity at learn.unity.com.
By the time you’re finished, your app will present a simple cube that you’ll be able to control with your breathing.
1. Let's start by creating a NEW, 2D Unity project:
2. Create an empty scene, then add it to your build settings. Also set Android as the desired build platform:
3. Download the Aidlab SDK, and import it to your project (Assets -> Import Package -> Custom Package...
).
4. Create a Cube object in the Unity editor, and attach the Example.cs
to the Main Camera
object. Now, the scene should look as follows:
5. A very important step is to set the minimum Android API level to 7.1:
6. Plug your Android device to your computer, and Build and Run your project. Wait a few seconds and press ALLOW
to enable Location service, required for Bluetooth connectivity:
7. Turn Aidlab on and wear it. It should automatically pair with your Android device. Now you can control your Cube
only by controlling your breathing pattern.
At this moment you should start modifying Example.cs
and your project as you like.
How it works
Our main Example class (Example.cs
) receives events from Aidlab directly. We achieved that simply by implementing the IAidlab
interface. Then, we passed a reference of Example
straight to the constructor of the Aidlab
class here:
void Start() {
enableBluetooth();
setLocationPermission();
aidlab = new Aidlab(this);
}
Aidlab
is a core class of the Aidlab SDK for Unity. It is responsible for connecting to your Aidlab Chest Strap, and for listening to any upcoming events from it. You don't have to modify that class.
There are two necessary actions you need to perform in order to pair with Aidlab over Bluetooth. Android requires location permission, and Bluetooth adapter to be enabled. See definition of setLocationPermission()
and enableBluetooth()
if you are curious how they work.
From IAidlab
interface you can lookup for events that are coming from Aidlab Chest Strap. Aidlab SDK allows you to several events like motion activity, respiration rate, skin temperature, ECG, just to name a few.
In this tutorial we are mostly interested in raw respiration signal, representing the ventilation level:
public void didReceiveRespiration(IAidlab aidlab, Int64 timestamp, float[] values)
Respiration is a raw, electrical signal measured in volts coming straight from our lungs, so we need to normalize it and clamp to reasonable values:
private float normalizeRespiration(float sample) {
return Mathf.Clamp(sample * 1000000, -4, 4);
}
We switched from volts to microvolts and clamped value to [-4, 4] so our Cube will not leave the main camera's viewport. At each update we modify Cube
object to mitigate chest movements:
void Update() {
var cube = GameObject.Find("Cube");
Vector3 position = cube.transform.position;
position.y = normalizeRespiration(currentRespirationSample);
cube.transform.position = position;
}
That's it. Just imagine what amazing effects you can now achieve:
Dealing with Unity and Aidlab using Aidlab SDK at the same time is an easy task, but let us know in case of any troubles.