Skip to main content
Version: 1.2.4

Unity

Android

Integration Steps

Download alg-unity-android-v1.0.zip

alg.aar // Include jar and check list
(cpu-type)/libnative-alg.so // Algorithm file

Class Name on Bridge File

#if UNITY_ANDROID
private static AndroidJavaClass _helper;
public static AndroidJavaClass helper
{
get
{
if (_helper != null) return _helper;
_helper = new AndroidJavaClass("com.deepbaysz.alg.BLHelper");
return _helper;
}
}

Send data to Unity,receiver = "BLHelper", method = "onMsg"

    /**
* Send data to Unity
*
* @param fmt formatted data
*/
private static void sendToApp(String fmt) {
Log.d(TAG, "sendToApp: " + fmt);
UnityPlayer.UnitySendMessage(receiver, method, fmt);
}

Scan device

    JSONObject obj = new JSONObject();
try {
obj.put("type", TYPE.FIND_DEVICE.ordinal());
obj.put("name", device.getName());
obj.put("address", device.getAddress());
obj.put("state", btManager.getConnectionState(device, BluetoothProfile.GATT)); //0=disconnected 1=connecting 2=connected 3=disconnecting
sendToApp(obj.toString());
} catch (JSONException e) {
e.printStackTrace();
}

Methods Overview

  1. init(String param) For init data and request BLE permissions
  2. scanDevice For scanning device
  3. scanStop For stop scanning
  4. connectDevice(String address) For connectting device
  5. disconnectDevice(String address) For disconnectting device
  6. getVersion(String address) For getting firmware version
  7. getBattery(String address) For getting battery information

Unity Methods to Get Data

    enum TYPE {
none = 0,
data = 1,// BLE data
find_device = 2,// Scan device callback
connect_succeed = 3,// Callback when success
connect_failed = 4,// Callback when fail
disconnected = 5,// Callback when disconnect device
device = 6 // Get device info (battery and firmware version)
};


BLE Data(signal description: 0 stands for good signal quality,1 stands for poor signal quality)

{
"address":"D6:5E:71:12:99:0C",
"type":1,
"mindfulness":0,
"stress":0,
"fatigue":0,
"emotion":0,
"focus":0,
"energy":{
"alpha":0,
"beta":0,
"theta":0,
"delta":0,
"gamma":0
},
"signal":1
}

More about data specification, please refer to Data Specification

  1. Scan Device (state description: 0 stands for has not connect yet, 1 stands for connectting, 2 stands for connectted)
{
"type":2,
"name":"Dbay-EEG2-283D",
"address":"D2:F7:33:DA:C8:C6",
"state":0
}
  1. Device Info
{
"type":3,
"address":"DF:EB:68:47:64:30",
"state":2
}
  1. Connect Unsuccessfully Parameters
{
"type":4,
"address":"DF:EB:68:47:64:30",
"state":0
}
  1. Disconnect Parameters
{
"type":5,
"address":"DF:EB:68:47:64:30",
"state":0
}
  1. Get Device Info
{
"type":6,
"address":"DF:EB:68:47:64:30",
"battery":10
}

{
"type":6,
"address":"DF:EB:68:47:64:30",
"version":"29-xx"
}

Unity get data from scanned device

    void onMsg(string json)
{
Debug.Log("---onMsg---" + json);
BLData data = JsonUtility.FromJson<BLData>(json);
if (data.type == (int)TYPE.find_device)
{
if (!blDevice.ContainsKey(data.address))
{
blDeviceList.Add(data);
blDevice.Add(data.address, data);
}
else
blDevice[data.address] = data;
refreshDevice();
#if USE_MESSENGER
Messenger.Broadcast("type_find_device", data);
#endif
}

}

iOS

Integration Steps

Download alg-unity-ios-v1.0.zip

DBAlgorithm.framework // Include algorithm file
BLHelper.h BLHelper.m // Bridge file to connect SDK
README.md

Class Name on Bridge File

Send data to Unity,receiver = "BLHelper", method = "onMsg"

/// Send all data to Unity
/// @param json formatted data
-(void)sendToApp:(NSString*)json
{
UnitySendMessage(getChar(receiver), getChar(method), getChar(json));
}

Scan Device

NSDictionary * deviceDict = @{
@"address": peripheral.identifier.UUIDString,
@"name": peripheral.name,
@"state": [NSNumber numberWithInt:(int)peripheral.state],
@"type":[NSNumber numberWithInt:type_connect_succeed],
};
[self sendToApp:[self objectToJson:deviceDict]];

Methods Overview

  1. init(String param) For init data and request BLE permissions
  2. scanDevice For scanning device
  3. scanStop For stop scanning
  4. connectDevice(String address) For connectting device
  5. disconnectDevice(String address) For disconnectting device
  6. getVersion(String address) For getting firmware version
  7. getBattery(String address) For getting battery information

Get Data in Unity

enum TYPE {
none = 0,
data = 1,// BLE data
find_device = 2,// Scan device callback
connect_succeed = 3,// Callback when success
connect_failed = 4,// Callback when fail
disconnected = 5,// Callback when disconnect device
device = 6 // Get device info (battery and firmware version)
};
  1. BLE Data(signal description: 0 stands for good signal quality,1 stands for poor signal quality)
{
"address":"D6:5E:71:12:99:0C",
"type":1,
"mindfulness":0,
"stress":0,
"fatigue":0,
"emotion":0,
"focus":0,
"energy":{
"alpha":0,
"beta":0,
"theta":0,
"delta":0,
"gamma":0
},
"signal":1
}
  1. Device Info (state description; 0 stands for has not connect yet, 1 stands for connecting, 2 stands for connected)
{
"type": 2,
"name": "Dbay-EEG2-283D",
"address": "D2:F7:33:DA:C8:C6",
"state": 0
}
  1. Connect Successfully Parameters
{
"type": 3,
"address": "DF:EB:68:47:64:30",
"name": "Dbay-EEG2-283D",
"state": 2
}
  1. Connect Unsuccessfully Parameters
{
"type": ,
"address": "DF:EB:68:47:64:30",
"name": "Dbay-EEG2-283D",
"state": 0
}
  1. Disconnect Parameters
{
"type": 5,
"address": "DF:EB:68:47:64:30",
"name": "Dbay-EEG2-283D",
"state": 0
}
  1. Battery Info
{
"type": 6,
"address": "DF:EB:68:47:64:30",
"name": "Dbay-EEG2-283D",
"battery": 60
}
  1. Firmware Info
{
"type": 6,
"address": "DF:EB:68:47:64:30",
"name": "Dbay-EEG2-283D",
"version": "0.2.9"
}

Unity Methods to Get Data

void onMsg(string json)
{
Debug.Log("---onMsg---" + json);
BLData data = JsonUtility.FromJson<BLData>(json);
if (data.type == (int)TYPE.find_device)
{
if (!blDevice.ContainsKey(data.address))
{
blDeviceList.Add(data);
blDevice.Add(data.address, data);
}
else
blDevice[data.address] = data;
refreshDevice();
#if USE_MESSENGER
Messenger.Broadcast("type_find_device", data);
#endif
}
}