SmartBowHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. using System;
  2. using UnityEngine;
  3. namespace SmartBowSDK
  4. {
  5. public class SmartBowHelper : MonoBehaviour
  6. {
  7. private static SmartBowHelper _Instance;
  8. internal SmartBowNetwork smartBowNetwork;
  9. internal BluetoothAim_SDK bluetoothAim;
  10. internal AimHandler_SDK aimHandler;
  11. internal ShootChecker_SDK shootChecker;
  12. public CalibrateDataStorageMode calibrateDataStorageMode = CalibrateDataStorageMode.Remote;
  13. void Awake()
  14. {
  15. DontDestroyOnLoad(gameObject);
  16. gameObject.AddComponent<AOTAdapter_SDK>();
  17. smartBowNetwork = gameObject.AddComponent<SmartBowNetwork>();
  18. bluetoothAim = gameObject.AddComponent<BluetoothAim_SDK>();
  19. aimHandler = gameObject.AddComponent<AimHandler_SDK>();
  20. shootChecker = gameObject.AddComponent<ShootChecker_SDK>();
  21. smartBowNetwork.smartBowHelper = this;
  22. bluetoothAim.smartBowHelper = this;
  23. aimHandler.smartBowHelper = this;
  24. shootChecker.smartBowHelper = this;
  25. }
  26. /// <summary>
  27. /// 获取唯一实例
  28. /// </summary>
  29. /// <returns></returns>
  30. public static SmartBowHelper GetInstance()
  31. {
  32. if (!_Instance) _Instance = new GameObject(typeof(SmartBowHelper).Name).AddComponent<SmartBowHelper>();
  33. return _Instance;
  34. }
  35. /// <summary>
  36. /// 创建新的实例
  37. /// </summary>
  38. /// <returns></returns>
  39. public static SmartBowHelper NewInstance()
  40. {
  41. return new GameObject(typeof(SmartBowHelper).Name).AddComponent<SmartBowHelper>();
  42. }
  43. /// <summary>
  44. /// 获取一个window 蓝牙用例
  45. /// </summary>
  46. /// <returns></returns>
  47. public BluetoothWindows CreateBluetoothWindows()
  48. {
  49. return bluetoothAim.CreateWindowBLE();
  50. }
  51. #region 登录
  52. /// <summary>
  53. /// 用户登录
  54. /// </summary>
  55. /// <param name="gameID">游戏ID</param>
  56. /// <param name="channelID">渠道ID</param>
  57. /// <param name="username">账号</param>
  58. /// <param name="password">密码</param>
  59. /// <param name="callback">回调函数</param>
  60. public void Login(string gameID, string channelID, string username, string password, Action<LoginResult> callback)
  61. {
  62. smartBowNetwork.Login(gameID, channelID, username, password, callback);
  63. }
  64. /// <summary>
  65. /// 用户登出
  66. /// </summary>
  67. /// <param name="callback">回调函数</param>
  68. public void Logout(Action<bool> callback)
  69. {
  70. smartBowNetwork.Logout(callback);
  71. }
  72. /// <summary>
  73. /// 获取用户基本信息
  74. /// </summary>
  75. /// <param name="callback">回调函数</param>
  76. public void GetUserInfo(Action<GetUserInfoResult> callback)
  77. {
  78. smartBowNetwork.GetUserInfo(callback);
  79. }
  80. #endregion
  81. #region 蓝牙:蓝牙部分新增修改,添加当前判断设备mac
  82. ///// <summary>
  83. ///// 连接蓝牙模块
  84. ///// </summary>
  85. //public void Connect()
  86. //{
  87. // //之前的默认不用判断mac
  88. // //当前连接不需要需要检测设备,设置为null
  89. // bluetoothAim.ResetAimDeviceInfoToNull();
  90. // bluetoothAim.Connect();
  91. //}
  92. /// <summary>
  93. /// 设置蓝牙过滤名称
  94. /// </summary>
  95. /// <param name="name"></param>
  96. public void SetFilters(string name) {
  97. if (!string.IsNullOrWhiteSpace(name))
  98. {
  99. bluetoothAim.filters = name;
  100. }
  101. }
  102. /// <summary>
  103. /// 设置传感器类型,用来区分解析
  104. /// </summary>
  105. /// <param name="sensorAxisType"></param>
  106. public void SetSensorAxisType(SensorAxisType sensorAxisType)
  107. {
  108. bluetoothAim.sensorAxisType = sensorAxisType;
  109. SmartBowLogger.Log(this, "SetSensorAxisType:" + bluetoothAim.sensorAxisType);
  110. }
  111. /// <summary>
  112. /// 连接蓝牙模块
  113. /// 新增连接操作,需要用户标签和设备id
  114. /// userTags: 存储的信息标签,按实际用户区分
  115. /// deviceId:对应设备id,同一个用户下的不同设备
  116. /// bRecreateDeviceInfo: 重新创建设备信息。false 的情况下默认限制mac
  117. /// </summary>
  118. public void Connect(string userTags,int deviceId, bool bRecreateDeviceInfo = false)
  119. {
  120. if (userTags == null || userTags.Length == 0)
  121. {
  122. SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
  123. return;
  124. }
  125. if (deviceId <= 0)
  126. {
  127. SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
  128. return;
  129. }
  130. //判断mac需要创建一个连接信息
  131. bluetoothAim.initAimDeviceInfo(userTags,deviceId,bRecreateDeviceInfo);
  132. //进行蓝牙连接
  133. bluetoothAim.Connect();
  134. }
  135. /// <summary>
  136. /// 清空建立的蓝牙判断数据
  137. /// userTags: 存储的信息标签,按实际用户区分
  138. /// deviceId:对应设备id,同一个用户下的不同设备
  139. /// </summary>
  140. public void ClearDeviceInfo(string userTags,int deviceId)
  141. {
  142. if (userTags == null || userTags.Length == 0)
  143. {
  144. SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
  145. return;
  146. }
  147. if (deviceId <= 0)
  148. {
  149. SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
  150. return ;
  151. }
  152. bluetoothAim.onClearAimDeviceInfoByDeviceId(userTags, deviceId);
  153. }
  154. /// <summary>
  155. /// 获取存储的数据
  156. /// userTags: 存储的信息标签,按实际用户区分
  157. /// deviceId:对应设备id,同一个用户下的不同设备
  158. /// </summary>
  159. public AimDeviceInfo GetDeviceInfo(string userTags, int deviceId) {
  160. if (userTags == null || userTags.Length == 0)
  161. {
  162. SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
  163. return null;
  164. }
  165. if (deviceId <= 0)
  166. {
  167. SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
  168. return null;
  169. }
  170. return bluetoothAim.onGetAimDeviceInfos(userTags, deviceId);
  171. }
  172. /// <summary>
  173. /// 断开蓝牙模块
  174. /// </summary>
  175. public void Disconnect()
  176. {
  177. bluetoothAim.Disconnect();
  178. }
  179. /// <summary>
  180. /// 获取蓝牙状态
  181. /// </summary>
  182. /// <returns>蓝牙状态</returns>
  183. public BluetoothStatusEnum GetBluetoothStatus()
  184. {
  185. return bluetoothAim.bluetoothStatus;
  186. }
  187. /// <summary>
  188. /// 监听蓝牙状态变化
  189. /// </summary>
  190. public event BluetoothStatusChangedEvent OnBluetoothStatusChanged;
  191. /// <summary>
  192. /// 判断蓝牙模块是否初始化完成
  193. /// </summary>
  194. /// <returns>蓝牙模块是否初始化完成</returns>
  195. public bool IsBluetoothModuleInited()
  196. {
  197. return bluetoothAim.moduleInited;
  198. }
  199. /// <summary>
  200. /// 监听蓝牙模块初始化完成
  201. /// Tip:连接蓝牙模块成功后会有几秒的初始化过程,初始化完成后才能进行传感器相关的函数调用。
  202. /// </summary>
  203. public Action OnBluetoothModuleInited;
  204. /// <summary>
  205. /// 监听蓝牙错误
  206. /// </summary>
  207. public event BluetoothErrorEvent OnBluetoothError;
  208. /// <summary>
  209. /// 获取Mac地址
  210. /// Tip:需要模块初始化完成才能获取
  211. /// </summary>
  212. /// <returns>如果返回null则是获取失败</returns>
  213. public string GetMacAddress()
  214. {
  215. return bluetoothAim.macAddress;
  216. }
  217. /// <summary>
  218. /// 获取电量
  219. /// Tip:需要模块初始化完成才能获取
  220. /// </summary>
  221. /// <returns>电量值(范围0~100),值为0时也代表获取失败</returns>
  222. public int GetBattery()
  223. {
  224. return bluetoothAim.battery;
  225. }
  226. /// <summary>
  227. /// 设置目标设备名称(用于搜索和连接)
  228. /// </summary>
  229. /// <param name="deviceName">设备名称</param>
  230. public void SetTargetDeviceName(string deviceName)
  231. {
  232. bluetoothAim.deviceConfig.deviceName = deviceName;
  233. }
  234. #endregion
  235. #region 传感器
  236. /// <summary>
  237. /// 开启九轴传感
  238. /// </summary>
  239. /// <returns>请求是否发送成功</returns>
  240. public bool StartRotationSensor()
  241. {
  242. return bluetoothAim.RequestOpen9Axis();
  243. }
  244. /// <summary>
  245. /// 停止九轴传感
  246. /// </summary>
  247. /// <returns>请求是否发送成功</returns>
  248. public bool StopRotationSensor()
  249. {
  250. return bluetoothAim.RequestClose9Axis();
  251. }
  252. /// <summary>
  253. /// 监听姿态角更新
  254. /// </summary>
  255. public event RotationUpdateEvent OnRotationUpdate;
  256. /// <summary>
  257. /// 开启射箭传感
  258. /// </summary>
  259. /// <returns>请求是否发送成功</returns>
  260. public bool StartShootingSensor()
  261. {
  262. return bluetoothAim.RequestOpenInfrared();
  263. }
  264. /// <summary>
  265. /// 停止射箭传感
  266. /// </summary>
  267. /// <returns>请求是否发送成功</returns>
  268. public bool StopShootingSensor()
  269. {
  270. return bluetoothAim.RequestCloseInfrared();
  271. }
  272. /// <summary>
  273. /// 监听射箭
  274. /// </summary>
  275. public ShootingEvent OnShooting;
  276. /// <summary>
  277. /// 开始陀螺仪校准
  278. /// </summary>
  279. public void StartGyrCalibration()
  280. {
  281. aimHandler.StartGyrCalibration();
  282. }
  283. /// <summary>
  284. /// 停止陀螺仪校准
  285. /// </summary>
  286. public void StopGyrCalibration()
  287. {
  288. aimHandler.StopGyrCalibration();
  289. }
  290. /// <summary>
  291. /// 判断陀螺仪是否正在校准
  292. /// </summary>
  293. /// <returns>陀螺仪是否正在校准</returns>
  294. public bool IsGyrCalibrating()
  295. {
  296. return aimHandler.IsGyrCalibrating();
  297. }
  298. /// <summary>
  299. /// 获取陀螺仪校准进度
  300. /// </summary>
  301. /// <returns>陀螺仪校准进度</returns>
  302. public float GetGyrProgress()
  303. {
  304. return aimHandler.GetGyrProgress();
  305. }
  306. /// <summary>
  307. /// 判断陀螺仪是否校准完成
  308. /// </summary>
  309. /// <returns>陀螺仪是否校准完成</returns>
  310. public bool IsGyrCompleted()
  311. {
  312. return aimHandler.IsGyrCompleted();
  313. }
  314. /// <summary>
  315. /// 开始地磁计校准
  316. /// </summary>
  317. public void StartMagCalibration()
  318. {
  319. aimHandler.StartMagCalibration();
  320. }
  321. /// <summary>
  322. /// 判断地磁计是否校准完成
  323. /// </summary>
  324. /// <returns>地磁计是否校准完成</returns>
  325. public bool IsMagCompleted()
  326. {
  327. return aimHandler.IsMagCompleted();
  328. }
  329. /// <summary>
  330. /// 视角归位
  331. /// </summary>
  332. public void ResetAim()
  333. {
  334. aimHandler.DoIdentity();
  335. }
  336. /// <summary>
  337. /// 监听模块的功能键短按
  338. /// </summary>
  339. public Action OnFunctionKeyPress;
  340. /// <summary>
  341. /// 监听模块的功能键长按
  342. /// </summary>
  343. public Action OnFunctionKeyLongPress;
  344. #endregion
  345. /// <summary>
  346. /// 蓝牙状态变化事件
  347. /// </summary>
  348. /// <param name="oldStatus">旧状态</param>
  349. /// <param name="newStatus">新状态</param>
  350. public delegate void BluetoothStatusChangedEvent(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus);
  351. internal void InvokeOnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
  352. {
  353. try
  354. {
  355. OnBluetoothStatusChanged?.Invoke(oldStatus, newStatus);
  356. }
  357. catch (Exception e)
  358. {
  359. Debug.LogError(e);
  360. }
  361. }
  362. /// <summary>
  363. /// 蓝牙错误事件
  364. /// </summary>
  365. /// <param name="error">错误类型</param>
  366. /// <param name="message">消息描述</param>
  367. public delegate void BluetoothErrorEvent(BluetoothError error, string message);
  368. internal void InvokeOnBluetoothError(BluetoothError error, string message)
  369. {
  370. try
  371. {
  372. OnBluetoothError?.Invoke(error, message);
  373. }
  374. catch (Exception e)
  375. {
  376. Debug.LogError(e);
  377. }
  378. }
  379. internal void InvokeOnFunctionKeyPress()
  380. {
  381. try
  382. {
  383. OnFunctionKeyPress?.Invoke();
  384. }
  385. catch (Exception e)
  386. {
  387. Debug.LogError(e);
  388. }
  389. }
  390. internal void InvokeOnFunctionKeyLongPress()
  391. {
  392. try
  393. {
  394. OnFunctionKeyLongPress?.Invoke();
  395. }
  396. catch (Exception e)
  397. {
  398. Debug.LogError(e);
  399. }
  400. }
  401. /// <summary>
  402. /// 姿态角更新事件
  403. /// </summary>
  404. /// <param name="rotation">姿态角</param>
  405. public delegate void RotationUpdateEvent(Quaternion rotation);
  406. internal void InvokeOnRotationUpdate(Quaternion rotation)
  407. {
  408. if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
  409. try
  410. {
  411. OnRotationUpdate?.Invoke(rotation);
  412. }
  413. catch (Exception e)
  414. {
  415. Debug.LogError(e);
  416. }
  417. }
  418. /// <summary>
  419. /// 射箭事件
  420. /// </summary>
  421. /// <param name="speed">速度(m/s)</param>
  422. public delegate void ShootingEvent(float speed);
  423. internal void InvokeOnShooting(float speed)
  424. {
  425. try
  426. {
  427. OnShooting?.Invoke(speed);
  428. }
  429. catch (Exception e)
  430. {
  431. Debug.LogError(e);
  432. }
  433. }
  434. internal void InvokeOnBluetoothModuleInited()
  435. {
  436. try
  437. {
  438. OnBluetoothModuleInited?.Invoke();
  439. }
  440. catch (Exception e)
  441. {
  442. Debug.LogError(e);
  443. }
  444. }
  445. /// <summary>
  446. /// 监听枪状态
  447. /// </summary>
  448. public BleDeviceEvent OnBleDeviceState;
  449. /// <summary>
  450. /// 硬件对应的事件
  451. /// </summary>
  452. public delegate void BleDeviceEvent(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum);
  453. internal void InvokeOnBleDevice(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum)
  454. {
  455. try
  456. {
  457. OnBleDeviceState?.Invoke(bleDeviceType,gunStatusEnum);
  458. }
  459. catch (Exception e)
  460. {
  461. Debug.LogError(e);
  462. }
  463. }
  464. #region 6轴部分接口
  465. /// <summary>
  466. /// 6轴数据重置初始位置数据
  467. /// </summary>
  468. public void ResetSixAxisIdentity()
  469. {
  470. bluetoothAim.ResetSixAxisFusion();
  471. }
  472. /// <summary>
  473. /// 监听姿态角更新
  474. /// </summary>
  475. public event SixAxisRotationUpdateEvent OnSixAxisRotationUpdate;
  476. /// <summary>
  477. /// 姿态角更新事件
  478. /// </summary>
  479. /// <param name="rotation">姿态角</param>
  480. public delegate void SixAxisRotationUpdateEvent(Vector3 eulerAngles);
  481. internal void InvokeOnSixAxisRotationUpdate(Vector3 eulerAngles)
  482. {
  483. if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
  484. try
  485. {
  486. OnSixAxisRotationUpdate?.Invoke(eulerAngles);
  487. }
  488. catch (Exception e)
  489. {
  490. Debug.LogError(e);
  491. }
  492. }
  493. ///// <summary>
  494. ///// 芯片数据更新事件
  495. ///// </summary>
  496. ///// <param name="rotation">姿态角</param>
  497. //public delegate void AxisDataUpdateEvent(byte[] value);
  498. //internal void InvokeOnAxisDataUpdateEvent(byte[] value)
  499. //{
  500. // if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
  501. // try
  502. // {
  503. // OnAxisDataUpdate?.Invoke(value);
  504. // }
  505. // catch (Exception e)
  506. // {
  507. // Debug.LogError(e);
  508. // }
  509. //}
  510. ///// <summary>
  511. ///// 芯片数据更新
  512. ///// </summary>
  513. //public event AxisDataUpdateEvent OnAxisDataUpdate;
  514. #endregion
  515. }
  516. }