203 lines
6.2 KiB
C#
203 lines
6.2 KiB
C#
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Android;
|
|
|
|
namespace TruegearSdk
|
|
{
|
|
//#if !UNITY_EDITOR && UNITY_ANDROID
|
|
public class TruegearAndroidConnector
|
|
{
|
|
public static TruegearAndroidConnector Instance => _instance;
|
|
private static TruegearAndroidConnector _instance;
|
|
|
|
private const string TruegearShell_JAVA_CLASS = "com.truegear.sdk.TruegearShell";
|
|
protected static AndroidJavaObject shellJavaObject;
|
|
public TruegearAndroidConnector()
|
|
{
|
|
if (_instance != null)
|
|
{
|
|
Debug.Log("Only one TruegearAndroidConnector instance allowed!");
|
|
return;
|
|
}
|
|
|
|
Debug.Log("TruegearAndroidConnector instance create!");
|
|
_instance = this;
|
|
}
|
|
|
|
public void InitShellJavaObject()
|
|
{
|
|
try
|
|
{
|
|
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
|
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
|
|
|
|
Debug.Log("TruegearAndroidConnector init ");
|
|
using (AndroidJavaClass ajc = new AndroidJavaClass(TruegearShell_JAVA_CLASS))
|
|
{
|
|
shellJavaObject = ajc.CallStatic<AndroidJavaObject>("getInstance");
|
|
AndroidJavaObject conntext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
|
|
|
|
currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
|
|
{
|
|
shellJavaObject.Call("unityInitialized", conntext);
|
|
}));
|
|
}
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.Log("Error: " + e.Message);
|
|
}
|
|
}
|
|
|
|
public void RequestPermission()
|
|
{
|
|
if (shellJavaObject == null)
|
|
{
|
|
return ;
|
|
}
|
|
Debug.Log("TruegearAndroidConnector RequestBluetoothPermission ");
|
|
try
|
|
{
|
|
if (!Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_CONNECT") || !Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_SCAN"))
|
|
{
|
|
string[] permission = { "android.permission.BLUETOOTH_CONNECT", "android.permission.BLUETOOTH_SCAN" };
|
|
Permission.RequestUserPermissions(permission);
|
|
}
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.Log("Error: " + e.Message);
|
|
}
|
|
}
|
|
|
|
public void StartScan()
|
|
{
|
|
if (shellJavaObject != null)
|
|
{
|
|
int t = shellJavaObject.Call<int>("startScanning");
|
|
Debug.Log("StartScan: " + t);
|
|
}
|
|
}
|
|
|
|
public List<DeviceData> GetScanedDevices()
|
|
{
|
|
if (shellJavaObject == null)
|
|
{
|
|
return new List<DeviceData>();
|
|
}
|
|
|
|
List<DeviceData> devices = new List<DeviceData>();
|
|
string jsonStr = shellJavaObject.Call<string>("getDeviceList");
|
|
Debug.Log("GetScanedDevices: " + jsonStr);
|
|
|
|
JSONNode jsonObj = JSON.Parse(jsonStr);
|
|
JSONNode.Enumerator enumerator2 = jsonObj.GetEnumerator();
|
|
while (enumerator2.MoveNext())
|
|
{
|
|
JSONNode jsonObj1 = enumerator2.Current.Value.AsObject;
|
|
DeviceData item = new DeviceData();
|
|
item.name = jsonObj1.GetValueOrDefault("Name", "");
|
|
item.address = jsonObj1.GetValueOrDefault("Address", "");
|
|
|
|
devices.Add(item);
|
|
}
|
|
return devices;
|
|
}
|
|
|
|
public bool ConnectToDevice(string macAddress)
|
|
{
|
|
if (shellJavaObject == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
shellJavaObject.Call("connectToDevice", macAddress);
|
|
return true;
|
|
}
|
|
|
|
public void DisconnectFromDevice()
|
|
{
|
|
if (shellJavaObject != null)
|
|
{
|
|
shellJavaObject.Call("disconnectFromDevice");
|
|
}
|
|
}
|
|
public bool IsAvailable()
|
|
{
|
|
if (shellJavaObject == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
string str = shellJavaObject.Call<string>("isAvailable");
|
|
Debug.Log("IsAvailable: " + str);
|
|
|
|
return str == "ok";
|
|
}
|
|
public bool ModifyElectricalPercent(int p)
|
|
{
|
|
if (shellJavaObject == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
shellJavaObject.Call("ModifyElectricalPercent", p);
|
|
return true;
|
|
}
|
|
public void SendPlayByEventName(string eventName)
|
|
{
|
|
if (shellJavaObject != null)
|
|
{
|
|
shellJavaObject.Call("sendPlayByEventName", eventName);
|
|
}
|
|
}
|
|
|
|
public void SendPlayEffectByContent(string content)
|
|
{
|
|
if (content == null)
|
|
return;
|
|
|
|
if (shellJavaObject != null)
|
|
{
|
|
shellJavaObject.Call("sendPlayByContent", content);
|
|
}
|
|
}
|
|
|
|
public void RegisterEffect(string eventName, string content)
|
|
{
|
|
if (content == null || eventName == null)
|
|
return;
|
|
|
|
if (shellJavaObject != null)
|
|
{
|
|
shellJavaObject.Call("registerEffect", eventName, content);
|
|
}
|
|
}
|
|
|
|
public void RegisterEffect_Eff(string eventName, byte[] content)
|
|
{
|
|
if (content == null || eventName == null)
|
|
return;
|
|
|
|
if (shellJavaObject != null)
|
|
{
|
|
string text = Convert.ToBase64String(content);
|
|
shellJavaObject.Call("registeEffect_Eff", eventName, text);
|
|
}
|
|
}
|
|
|
|
public void RegisterEffect_Eff_Base64String(string eventName, string base64String)
|
|
{
|
|
if (base64String == null || eventName == null)
|
|
return;
|
|
if (shellJavaObject != null)
|
|
{
|
|
shellJavaObject.Call("registeEffect_Eff", eventName, base64String);
|
|
}
|
|
}
|
|
}
|
|
//#endif
|
|
}
|