diff --git a/unity/Assets/Plugins/AppConfig.cs b/unity/Assets/Plugins/AppConfig.cs deleted file mode 100644 index fc533d4..0000000 --- a/unity/Assets/Plugins/AppConfig.cs +++ /dev/null @@ -1,26 +0,0 @@ -/// -/// Contain configuration of a specific App. -/// -public static class AppConfig -{ - /* - * Enter your application Client ID and Client Secret below. - * You can obtain these credentials after registering your App ID with the Cortex SDK for development. - * For instructions, visit: https://emotiv.gitbook.io/cortex-api#create-a-cortex-app - */ - public static string ClientId = "put_your_client_id_here"; - public static string ClientSecret = "put_your_client_secret_here"; - - public static string AppVersion = "1.1.0"; - public static string AppName = "UnityApp"; - public static bool IsDataBufferUsing = false; // Set false if you want to display data directly to MessageLog without storing in Data Buffer - public static bool AllowSaveLogToFile = false; // Set true to save log to file and cortex token to local file for next time use - - #if !USE_EMBEDDED_LIB && !UNITY_ANDROID && !UNITY_IOS - // only for desktop without embedded cortex - public static string AppUrl = "wss://localhost:6868"; // for desktop without embedded cortex - #else - public static string AppUrl = ""; // Don't need AppUrl for mobile and embedded cortex - #endif - -} \ No newline at end of file diff --git a/unity/Assets/Plugins/AppConfig.cs.meta b/unity/Assets/Plugins/AppConfig.cs.meta deleted file mode 100644 index d102c30..0000000 --- a/unity/Assets/Plugins/AppConfig.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0c0b098639498b842a79231b54993cdd -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/unity/Assets/Plugins/Emotiv-Unity-Plugin b/unity/Assets/Plugins/Emotiv-Unity-Plugin index 245f4e0..61d5fab 160000 --- a/unity/Assets/Plugins/Emotiv-Unity-Plugin +++ b/unity/Assets/Plugins/Emotiv-Unity-Plugin @@ -1 +1 @@ -Subproject commit 245f4e0b1a75c19fb70cc3d93846152fb1d5fb33 +Subproject commit 61d5fab7b0764d2f4fe0fa30fc9c01350addf10c diff --git a/unity/Assets/SimpleExample.cs b/unity/Assets/SimpleExample.cs index 53ed419..728106f 100644 --- a/unity/Assets/SimpleExample.cs +++ b/unity/Assets/SimpleExample.cs @@ -47,107 +47,9 @@ private void Awake() } } - // for android - #if UNITY_ANDROID - private const string FineLocationPermission = "android.permission.ACCESS_FINE_LOCATION"; - private const string BluetoothScanPermission = "android.permission.BLUETOOTH_SCAN"; - private const string BluetoothConnectPermission = "android.permission.BLUETOOTH_CONNECT"; - private const string BluetoothPermission = "android.permission.BLUETOOTH"; - - private bool HasAllPermissions() - { - // check location permission - if (!HasPermission(FineLocationPermission)) - { - return false; - } - - // check bluetooth permission - AndroidJavaClass jc = new AndroidJavaClass("android.os.Build$VERSION"); - int androidVersion = jc.GetStatic("SDK_INT"); - - if (androidVersion >= 31) - { - // Android 12 or higher. need bluetooth scan and connect permission - if (!HasPermission(BluetoothScanPermission) || !HasPermission(BluetoothConnectPermission)) - { - return false; - } - } - else - { - // Android 11 or lower. need bluetooth permission - if (!HasPermission(BluetoothPermission)) - { - return false; - } - } - return true; - } - - private IEnumerator RequestPermissions() - { - // check android version . if android version >= 31, need bluetooth scan and connect permission otherwise need bluetooth permission only. the location permission is always needed - AndroidJavaClass jc = new AndroidJavaClass("android.os.Build$VERSION"); - int androidVersion = jc.GetStatic("SDK_INT"); - string[] _permissions; - - if (androidVersion >= 30) - { - _permissions = new string[] { FineLocationPermission, BluetoothScanPermission, BluetoothConnectPermission }; - } - else - { - _permissions = new string[] { FineLocationPermission, BluetoothPermission }; - } - - foreach (var permission in _permissions) - { - if (!HasPermission(permission)) - { - RequestPermission(permission); - yield return new WaitUntil(() => HasPermission(permission)); - } - } - } - - // start EmotivUnityItf for android - private void StartEmotivUnityItfForAndroid() { - if (HasAllPermissions()) { - AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); - AndroidJavaObject currentActivity = unityPlayer.GetStatic("currentActivity"); - _eItf.Init(AppConfig.ClientId, AppConfig.ClientSecret, AppConfig.AppName, AppConfig.AllowSaveLogToFile, AppConfig.IsDataBufferUsing); - _eItf.Start(currentActivity); - _isEmotivUnityItfInitialized = true; - } - else { - StartCoroutine(RequestPermissions()); - } - } - - private static bool HasPermission(string permissionName) - { - return Permission.HasUserAuthorizedPermission(permissionName); - } - - private static void RequestPermission(string permissionName) { - if (Permission.HasUserAuthorizedPermission(permissionName)) - { - Debug.Log("Permission " + permissionName + " is authorized"); - } - else - { - // We do not have permission to use the microphone. - // Ask for permission or proceed without the functionality enabled. - Permission.RequestUserPermission(permissionName); - } - } - #endif - void Start() { - - #if USE_EMBEDDED_LIB && UNITY_STANDALONE_WIN && !UNITY_EDITOR +#if USE_EMBEDDED_LIB && UNITY_STANDALONE_WIN && !UNITY_EDITOR string[] args = Environment.GetCommandLineArgs(); if (args.Length > 1) { @@ -156,18 +58,9 @@ void Start() } #endif -#if UNITY_ANDROID - StartEmotivUnityItfForAndroid(); -#elif UNITY_IOS - UnityEngine.Debug.Log("SimpleExp: Start EmotivUnityItf for ios"); - _eItf.Init(AppConfig.ClientId, AppConfig.ClientSecret, AppConfig.AppName, AppConfig.AllowSaveLogToFile, AppConfig.IsDataBufferUsing); - _eItf.Start(); -#else - UnityEngine.Debug.Log("SimpleExp: Start EmotivUnityItf for desktop " + AppConfig.AppUrl); - _eItf.Init(AppConfig.ClientId, AppConfig.ClientSecret, AppConfig.AppName, AppConfig.AllowSaveLogToFile, AppConfig.IsDataBufferUsing, AppConfig.AppUrl); - _eItf.Start(); - _isEmotivUnityItfInitialized = true; - #endif + // Initialize and start Emotiv Unity Interface + // Configuration is read from EmotivCortexSDKWindow (Tools > Emotiv Cortex SDK) + _isEmotivUnityItfInitialized = _eItf.InitializeAndStart(); } // Update is called once per frame @@ -178,12 +71,11 @@ void Update() return; _timerDataUpdate -= TIME_UPDATE_DATA; - #if UNITY_ANDROID - if (!_isEmotivUnityItfInitialized) { - // start EmotivUnityItf for android if not started - StartEmotivUnityItfForAndroid(); + // Retry initialization if not yet initialized (e.g., waiting for Android permissions) + if (!_isEmotivUnityItfInitialized) + { + _isEmotivUnityItfInitialized = _eItf.InitializeAndStart(); } - #endif // Check buttons interactable CheckButtonsInteractable(); @@ -195,7 +87,8 @@ void Update() // List detectedHeadsets = _eItf.GetDetectedHeadsets(); // Demo how to get subcribed data if use Data Buffer - if (AppConfig.IsDataBufferUsing) { + // Note: IsDataBufferUsing is configured in EmotivCortexSDKWindow + if (_eItf.IsAuthorizedOK) { // eeg data // if (_eItf.GetNumberEEGSamples() > 0) { // string eegHeaderStr = "EEG Header: ";