using System;
using System.Runtime.InteropServices;
using AOT;
using Rokid.UXR.Utility;
using UnityEngine;
namespace Rokid.UXR.Native
{
public partial class NativeInterface
{
public partial class NativeAPI
{
///
/// usb connection callback successful
///
public static event Action OnUSBConnect;
///
/// usb disconnect callback
///
public static event Action OnUSBDisConnect;
///
/// On glass bright update callback
///
public static event Action OnGlassBrightUpdate;
private static string UXR_SERVICE_USBDEVICE = "UXRUSBDevice.";
///
/// Get glasses name
///
///
public static string GetGlassName()
{
#if UNITY_EDITOR
return "PC";
#else
IntPtr namePtr = Api.getGlassName();
string name = Marshal.PtrToStringAnsi(namePtr);
return name;
#endif
}
///
/// Get the glasspid of the glasses
///
///
public static int GetGlassPID()
{
if (!Utils.IsAndroidPlatfrom())
{
return 0;
}
return Api.getGlassProductId();
}
///
/// Get glasses sn
///
///
public static string GetGlassSN()
{
if (!Utils.IsAndroidPlatfrom())
{
return null;
}
IntPtr snPtr = Api.getGlassSn();
string sn = Marshal.PtrToStringAnsi(snPtr);
return sn;
}
///
/// Get the glasses type ids
///
///
public static string GetGlassTypeId()
{
if (!Utils.IsAndroidPlatfrom())
{
return null;
}
IntPtr typeIdPtr = Api.getGlassTypeId();
string typeId = Marshal.PtrToStringAnsi(typeIdPtr);
return typeId;
}
///
/// Get the glasses firmware version
///
///
public static string GetGlassFirmwareVersion()
{
if (!Utils.IsAndroidPlatfrom())
{
return null;
}
IntPtr versionPtr = Api.getGlassFirmwareVersion();
string version = Marshal.PtrToStringAnsi(versionPtr);
return version;
}
///
/// Get glasses brightness
///
///
public static int GetGlassBrightness()
{
if (!Utils.IsAndroidPlatfrom())
{
return 0;
}
return Api.getGlassBrightness();
}
///
/// Set the brightness range of the glasses to 1-100
///
///
public static void SetGlassBrightness(int value)
{
if (!Utils.IsAndroidPlatfrom())
{
return;
}
Api.setGlassBrightness(value);
}
///
/// Set the glasses status callback
///
[Obsolete("Use RegisterUSBStatusCallback instead")]
public static void setUSBStatusCallback()
{
if (!Utils.IsAndroidPlatfrom())
{
return;
}
setOnUsbStatusUpdate(OnUsbStatusUpdateCallByC);//注册
}
///
/// Register usb status event
///
public static void RegisterUSBStatusCallback()
{
if (!Utils.IsAndroidPlatfrom())
{
return;
}
setOnUsbStatusUpdate(OnUsbStatusUpdateCallByC);//注册
}
///
/// Unregister usb status event
///
public static void UnRegisterUSBStatusCallback()
{
if (!Utils.IsAndroidPlatfrom())
{
return;
}
clearOnUsbStatusUpdate();//注销
}
delegate void OnUsbStatusUpdate(bool isConnect);
[DllImport(ApiConstants.UXR_GFX_PLUGIN)]
static extern void setOnUsbStatusUpdate(OnUsbStatusUpdate cb);
[DllImport(ApiConstants.UXR_GFX_PLUGIN)]
static extern void clearOnUsbStatusUpdate();
[MonoPInvokeCallback(typeof(OnUsbStatusUpdate))]
static void OnUsbStatusUpdateCallByC(bool isConnect)
{
if (isConnect)
{
OnUSBConnect?.Invoke();
}
else
{
OnUSBDisConnect?.Invoke();
}
}
///
/// Kill the uxr process
///
///
public static bool KillProcess()
{
if (!Utils.IsAndroidPlatfrom())
{
return false;
}
AndroidJavaObject obj = CallBridge.CallAndroid(Request.Build()
.Name(UXR_SERVICE_USBDEVICE + "killProcess"));
return CallBridge.CovertBool(obj);
}
///
/// Finish the uxr activity
///
///
public static bool FinishActivity()
{
if (!Utils.IsAndroidPlatfrom())
{
return false;
}
AndroidJavaObject obj = CallBridge.CallAndroid(Request.Build()
.Name(UXR_SERVICE_USBDEVICE + "activityFinish"));
return CallBridge.CovertBool(obj);
}
///
/// Whether the usb is successfully connected
///
///
public static bool IsUSBConnect()
{
if (!Utils.IsAndroidPlatfrom())
{
return false;
}
return Api.isUsbConnect();
}
///
/// Register glasses bright events
///
public static void RegisterGlassBrightUpdate()
{
if (!Utils.IsAndroidPlatfrom())
{
return;
}
setOnGlassBrightUpdate(OnGlassBrightUpdateCallByC);
}
///
/// UnRegister glasses bright events
///
public static void UnregisterOnGlassBrightUpdate()
{
if (!Utils.IsAndroidPlatfrom())
{
return;
}
clearOnGlassBrightUpdate();//注销
}
///
/// Get persist value
///
public static string GetPersistValue(string key)
{
if (Utils.IsAndroidPlatfrom())
{
IntPtr value = getPropertiesValue(key);
return Marshal.PtrToStringAnsi(value);
}
return null;
}
///
/// Set persist value
///
public static void SetPersistValue(string key, string value)
{
if (Utils.IsAndroidPlatfrom())
setPropertiesValue(key, value);
}
delegate void OnGlassBrightUpdateC(int brightness);
[DllImport(ApiConstants.UXR_GFX_PLUGIN)]
static extern void setOnGlassBrightUpdate(OnGlassBrightUpdateC cb);
[DllImport(ApiConstants.UXR_GFX_PLUGIN)]
static extern void clearOnGlassBrightUpdate();
[MonoPInvokeCallback(typeof(OnGlassBrightUpdateC))]
static void OnGlassBrightUpdateCallByC(int brightness)
{
OnGlassBrightUpdate?.Invoke(brightness);
}
[DllImport(ApiConstants.UXR_GFX_PLUGIN, CharSet = CharSet.Ansi)]
static extern void setPropertiesValue(string key, string value);
[DllImport(ApiConstants.UXR_GFX_PLUGIN, CharSet = CharSet.Ansi)]
static extern IntPtr getPropertiesValue(string key);
}
}
}