NativeInterface_Device.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using AOT;
  4. using Rokid.UXR.Utility;
  5. using UnityEngine;
  6. namespace Rokid.UXR.Native
  7. {
  8. public partial class NativeInterface
  9. {
  10. public partial class NativeAPI
  11. {
  12. /// <summary>
  13. /// usb connection callback successful
  14. /// </summary>
  15. public static event Action OnUSBConnect;
  16. /// <summary>
  17. /// usb disconnect callback
  18. /// </summary>
  19. public static event Action OnUSBDisConnect;
  20. /// <summary>
  21. /// On glass bright update callback
  22. /// </summary>
  23. public static event Action<int> OnGlassBrightUpdate;
  24. private static string UXR_SERVICE_USBDEVICE = "UXRUSBDevice.";
  25. /// <summary>
  26. /// Get glasses name
  27. /// </summary>
  28. /// <returns></returns>
  29. public static string GetGlassName()
  30. {
  31. #if UNITY_EDITOR
  32. return "PC";
  33. #else
  34. IntPtr namePtr = Api.getGlassName();
  35. string name = Marshal.PtrToStringAnsi(namePtr);
  36. return name;
  37. #endif
  38. }
  39. /// <summary>
  40. /// Get the glasspid of the glasses
  41. /// </summary>
  42. /// <returns></returns>
  43. public static int GetGlassPID()
  44. {
  45. if (!Utils.IsAndroidPlatfrom())
  46. {
  47. return 0;
  48. }
  49. return Api.getGlassProductId();
  50. }
  51. /// <summary>
  52. /// Get glasses sn
  53. /// </summary>
  54. /// <returns></returns>
  55. public static string GetGlassSN()
  56. {
  57. if (!Utils.IsAndroidPlatfrom())
  58. {
  59. return null;
  60. }
  61. IntPtr snPtr = Api.getGlassSn();
  62. string sn = Marshal.PtrToStringAnsi(snPtr);
  63. return sn;
  64. }
  65. /// <summary>
  66. /// Get the glasses type ids
  67. /// </summary>
  68. /// <returns></returns>
  69. public static string GetGlassTypeId()
  70. {
  71. if (!Utils.IsAndroidPlatfrom())
  72. {
  73. return null;
  74. }
  75. IntPtr typeIdPtr = Api.getGlassTypeId();
  76. string typeId = Marshal.PtrToStringAnsi(typeIdPtr);
  77. return typeId;
  78. }
  79. /// <summary>
  80. /// Get the glasses firmware version
  81. /// </summary>
  82. /// <returns></returns>
  83. public static string GetGlassFirmwareVersion()
  84. {
  85. if (!Utils.IsAndroidPlatfrom())
  86. {
  87. return null;
  88. }
  89. IntPtr versionPtr = Api.getGlassFirmwareVersion();
  90. string version = Marshal.PtrToStringAnsi(versionPtr);
  91. return version;
  92. }
  93. /// <summary>
  94. /// Get glasses brightness
  95. /// </summary>
  96. /// <returns></returns>
  97. public static int GetGlassBrightness()
  98. {
  99. if (!Utils.IsAndroidPlatfrom())
  100. {
  101. return 0;
  102. }
  103. return Api.getGlassBrightness();
  104. }
  105. /// <summary>
  106. /// Set the brightness range of the glasses to 1-100
  107. /// </summary>
  108. /// <param name="value"></param>
  109. public static void SetGlassBrightness(int value)
  110. {
  111. if (!Utils.IsAndroidPlatfrom())
  112. {
  113. return;
  114. }
  115. Api.setGlassBrightness(value);
  116. }
  117. /// <summary>
  118. /// Set the glasses status callback
  119. /// </summary>
  120. [Obsolete("Use RegisterUSBStatusCallback instead")]
  121. public static void setUSBStatusCallback()
  122. {
  123. if (!Utils.IsAndroidPlatfrom())
  124. {
  125. return;
  126. }
  127. setOnUsbStatusUpdate(OnUsbStatusUpdateCallByC);//注册
  128. }
  129. /// <summary>
  130. /// Register usb status event
  131. /// </summary>
  132. public static void RegisterUSBStatusCallback()
  133. {
  134. if (!Utils.IsAndroidPlatfrom())
  135. {
  136. return;
  137. }
  138. setOnUsbStatusUpdate(OnUsbStatusUpdateCallByC);//注册
  139. }
  140. /// <summary>
  141. /// Unregister usb status event
  142. /// </summary>
  143. public static void UnRegisterUSBStatusCallback()
  144. {
  145. if (!Utils.IsAndroidPlatfrom())
  146. {
  147. return;
  148. }
  149. clearOnUsbStatusUpdate();//注销
  150. }
  151. delegate void OnUsbStatusUpdate(bool isConnect);
  152. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  153. static extern void setOnUsbStatusUpdate(OnUsbStatusUpdate cb);
  154. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  155. static extern void clearOnUsbStatusUpdate();
  156. [MonoPInvokeCallback(typeof(OnUsbStatusUpdate))]
  157. static void OnUsbStatusUpdateCallByC(bool isConnect)
  158. {
  159. if (isConnect)
  160. {
  161. OnUSBConnect?.Invoke();
  162. }
  163. else
  164. {
  165. OnUSBDisConnect?.Invoke();
  166. }
  167. }
  168. /// <summary>
  169. /// Kill the uxr process
  170. /// </summary>
  171. /// <returns></returns>
  172. public static bool KillProcess()
  173. {
  174. if (!Utils.IsAndroidPlatfrom())
  175. {
  176. return false;
  177. }
  178. AndroidJavaObject obj = CallBridge.CallAndroid(Request.Build()
  179. .Name(UXR_SERVICE_USBDEVICE + "killProcess"));
  180. return CallBridge.CovertBool(obj);
  181. }
  182. /// <summary>
  183. /// Finish the uxr activity
  184. /// </summary>
  185. /// <returns></returns>
  186. public static bool FinishActivity()
  187. {
  188. if (!Utils.IsAndroidPlatfrom())
  189. {
  190. return false;
  191. }
  192. AndroidJavaObject obj = CallBridge.CallAndroid(Request.Build()
  193. .Name(UXR_SERVICE_USBDEVICE + "activityFinish"));
  194. return CallBridge.CovertBool(obj);
  195. }
  196. /// <summary>
  197. /// Whether the usb is successfully connected
  198. /// </summary>
  199. /// <returns></returns>
  200. public static bool IsUSBConnect()
  201. {
  202. if (!Utils.IsAndroidPlatfrom())
  203. {
  204. return false;
  205. }
  206. return Api.isUsbConnect();
  207. }
  208. /// <summary>
  209. /// Register glasses bright events
  210. /// </summary>
  211. public static void RegisterGlassBrightUpdate()
  212. {
  213. if (!Utils.IsAndroidPlatfrom())
  214. {
  215. return;
  216. }
  217. setOnGlassBrightUpdate(OnGlassBrightUpdateCallByC);
  218. }
  219. /// <summary>
  220. /// UnRegister glasses bright events
  221. /// </summary>
  222. public static void UnregisterOnGlassBrightUpdate()
  223. {
  224. if (!Utils.IsAndroidPlatfrom())
  225. {
  226. return;
  227. }
  228. clearOnGlassBrightUpdate();//注销
  229. }
  230. /// <summary>
  231. /// Get persist value
  232. /// </summary>
  233. public static string GetPersistValue(string key)
  234. {
  235. if (Utils.IsAndroidPlatfrom())
  236. {
  237. IntPtr value = getPropertiesValue(key);
  238. return Marshal.PtrToStringAnsi(value);
  239. }
  240. return null;
  241. }
  242. /// <summary>
  243. /// Set persist value
  244. /// </summary>
  245. public static void SetPersistValue(string key, string value)
  246. {
  247. if (Utils.IsAndroidPlatfrom())
  248. setPropertiesValue(key, value);
  249. }
  250. delegate void OnGlassBrightUpdateC(int brightness);
  251. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  252. static extern void setOnGlassBrightUpdate(OnGlassBrightUpdateC cb);
  253. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  254. static extern void clearOnGlassBrightUpdate();
  255. [MonoPInvokeCallback(typeof(OnGlassBrightUpdateC))]
  256. static void OnGlassBrightUpdateCallByC(int brightness)
  257. {
  258. OnGlassBrightUpdate?.Invoke(brightness);
  259. }
  260. [DllImport(ApiConstants.UXR_GFX_PLUGIN, CharSet = CharSet.Ansi)]
  261. static extern void setPropertiesValue(string key, string value);
  262. [DllImport(ApiConstants.UXR_GFX_PLUGIN, CharSet = CharSet.Ansi)]
  263. static extern IntPtr getPropertiesValue(string key);
  264. }
  265. }
  266. }