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
init(String param)
For init data and request BLE permissionsscanDevice
For scanning devicescanStop
For stop scanningconnectDevice(String address)
For connectting devicedisconnectDevice(String address)
For disconnectting devicegetVersion(String address)
For getting firmware versiongetBattery(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
- 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
}
- Device Info
{
"type":3,
"address":"DF:EB:68:47:64:30",
"state":2
}
- Connect Unsuccessfully Parameters
{
"type":4,
"address":"DF:EB:68:47:64:30",
"state":0
}
- Disconnect Parameters
{
"type":5,
"address":"DF:EB:68:47:64:30",
"state":0
}
- 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
init(String param)
For init data and request BLE permissionsscanDevice
For scanning devicescanStop
For stop scanningconnectDevice(String address)
For connectting devicedisconnectDevice(String address)
For disconnectting devicegetVersion(String address)
For getting firmware versiongetBattery(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)
};
- 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
}
- 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
}
- Connect Successfully Parameters
{
"type": 3,
"address": "DF:EB:68:47:64:30",
"name": "Dbay-EEG2-283D",
"state": 2
}
- Connect Unsuccessfully Parameters
{
"type": ,
"address": "DF:EB:68:47:64:30",
"name": "Dbay-EEG2-283D",
"state": 0
}
- Disconnect Parameters
{
"type": 5,
"address": "DF:EB:68:47:64:30",
"name": "Dbay-EEG2-283D",
"state": 0
}
- Battery Info
{
"type": 6,
"address": "DF:EB:68:47:64:30",
"name": "Dbay-EEG2-283D",
"battery": 60
}
- 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
}
}