SmartBowHelper.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. else {
  102. bluetoothAim.filters = "";
  103. }
  104. }
  105. /// <summary>
  106. /// 设置是否使用名称连接
  107. /// </summary>
  108. /// <param name="useName">是否使用名称连接</param>
  109. public void SetIsConnectName(bool useName)
  110. {
  111. bluetoothAim.isConnectName = useName;
  112. }
  113. /// <summary>
  114. /// 设置连接的MAC地址
  115. /// </summary>
  116. /// <param name="macStr">MAC地址字符串</param>
  117. public void SetConnectMacStr(string macStr)
  118. {
  119. if (!string.IsNullOrWhiteSpace(macStr))
  120. {
  121. bluetoothAim.connectMacStr = macStr;
  122. }
  123. }
  124. /// <summary>
  125. /// 设置传感器类型,用来区分解析
  126. /// </summary>
  127. /// <param name="sensorAxisType"></param>
  128. public void SetSensorAxisType(SensorAxisType sensorAxisType)
  129. {
  130. bluetoothAim.sensorAxisType = sensorAxisType;
  131. SmartBowLogger.Log(this, "SetSensorAxisType:" + bluetoothAim.sensorAxisType);
  132. }
  133. /// <summary>
  134. /// 连接蓝牙模块
  135. /// 新增连接操作,需要用户标签和设备id
  136. /// userTags: 存储的信息标签,按实际用户区分
  137. /// deviceId:对应设备id,同一个用户下的不同设备
  138. /// bRecreateDeviceInfo: 重新创建设备信息。false 的情况下默认限制mac
  139. /// </summary>
  140. public void Connect(string userTags,int deviceId, bool bRecreateDeviceInfo = false)
  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. //判断mac需要创建一个连接信息
  153. bluetoothAim.initAimDeviceInfo(userTags,deviceId,bRecreateDeviceInfo);
  154. //进行蓝牙连接
  155. bluetoothAim.Connect();
  156. }
  157. /// <summary>
  158. /// 清空建立的蓝牙判断数据
  159. /// userTags: 存储的信息标签,按实际用户区分
  160. /// deviceId:对应设备id,同一个用户下的不同设备
  161. /// </summary>
  162. public void ClearDeviceInfo(string userTags,int deviceId)
  163. {
  164. if (userTags == null || userTags.Length == 0)
  165. {
  166. SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
  167. return;
  168. }
  169. if (deviceId <= 0)
  170. {
  171. SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
  172. return ;
  173. }
  174. bluetoothAim.onClearAimDeviceInfoByDeviceId(userTags, deviceId);
  175. }
  176. /// <summary>
  177. /// 获取存储的数据
  178. /// userTags: 存储的信息标签,按实际用户区分
  179. /// deviceId:对应设备id,同一个用户下的不同设备
  180. /// </summary>
  181. public AimDeviceInfo GetDeviceInfo(string userTags, int deviceId) {
  182. if (userTags == null || userTags.Length == 0)
  183. {
  184. SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
  185. return null;
  186. }
  187. if (deviceId <= 0)
  188. {
  189. SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
  190. return null;
  191. }
  192. return bluetoothAim.onGetAimDeviceInfos(userTags, deviceId);
  193. }
  194. /// <summary>
  195. /// 断开蓝牙模块
  196. /// </summary>
  197. public void Disconnect()
  198. {
  199. bluetoothAim.Disconnect();
  200. }
  201. /// <summary>
  202. /// 获取蓝牙状态
  203. /// </summary>
  204. /// <returns>蓝牙状态</returns>
  205. public BluetoothStatusEnum GetBluetoothStatus()
  206. {
  207. return bluetoothAim.bluetoothStatus;
  208. }
  209. /// <summary>
  210. /// 监听蓝牙状态变化
  211. /// </summary>
  212. public event BluetoothStatusChangedEvent OnBluetoothStatusChanged;
  213. /// <summary>
  214. /// 判断蓝牙模块是否初始化完成
  215. /// </summary>
  216. /// <returns>蓝牙模块是否初始化完成</returns>
  217. public bool IsBluetoothModuleInited()
  218. {
  219. return bluetoothAim.moduleInited;
  220. }
  221. /// <summary>
  222. /// 监听蓝牙模块初始化完成
  223. /// Tip:连接蓝牙模块成功后会有几秒的初始化过程,初始化完成后才能进行传感器相关的函数调用。
  224. /// </summary>
  225. public Action OnBluetoothModuleInited;
  226. /// <summary>
  227. /// 监听蓝牙错误
  228. /// </summary>
  229. public event BluetoothErrorEvent OnBluetoothError;
  230. /// <summary>
  231. /// 获取Mac地址
  232. /// Tip:需要模块初始化完成才能获取
  233. /// </summary>
  234. /// <returns>如果返回null则是获取失败</returns>
  235. public string GetMacAddress()
  236. {
  237. return bluetoothAim.macAddress;
  238. }
  239. /// <summary>
  240. /// 获取电量
  241. /// Tip:需要模块初始化完成才能获取
  242. /// </summary>
  243. /// <returns>电量值(范围0~100),值为0时也代表获取失败</returns>
  244. public int GetBattery()
  245. {
  246. return bluetoothAim.battery;
  247. }
  248. /// <summary>
  249. /// 设置目标设备名称(用于搜索和连接)
  250. /// </summary>
  251. /// <param name="deviceName">设备名称</param>
  252. public void SetTargetDeviceName(string deviceName)
  253. {
  254. bluetoothAim.deviceConfig.deviceName = deviceName;
  255. }
  256. #endregion
  257. #region 传感器
  258. /// <summary>
  259. /// 开启九轴传感
  260. /// </summary>
  261. /// <returns>请求是否发送成功</returns>
  262. public bool StartRotationSensor()
  263. {
  264. return bluetoothAim.RequestOpen9Axis();
  265. }
  266. /// <summary>
  267. /// 停止九轴传感
  268. /// </summary>
  269. /// <returns>请求是否发送成功</returns>
  270. public bool StopRotationSensor()
  271. {
  272. return bluetoothAim.RequestClose9Axis();
  273. }
  274. /// <summary>
  275. /// 监听姿态角更新
  276. /// </summary>
  277. public event RotationUpdateEvent OnRotationUpdate;
  278. /// <summary>
  279. /// 开启射箭传感
  280. /// </summary>
  281. /// <returns>请求是否发送成功</returns>
  282. public bool StartShootingSensor()
  283. {
  284. return bluetoothAim.RequestOpenInfrared();
  285. }
  286. /// <summary>
  287. /// 停止射箭传感
  288. /// </summary>
  289. /// <returns>请求是否发送成功</returns>
  290. public bool StopShootingSensor()
  291. {
  292. return bluetoothAim.RequestCloseInfrared();
  293. }
  294. /// <summary>
  295. /// 监听射箭
  296. /// </summary>
  297. public ShootingEvent OnShooting;
  298. /// <summary>
  299. /// 开始陀螺仪校准
  300. /// </summary>
  301. public void StartGyrCalibration()
  302. {
  303. aimHandler.StartGyrCalibration();
  304. }
  305. /// <summary>
  306. /// 停止陀螺仪校准
  307. /// </summary>
  308. public void StopGyrCalibration()
  309. {
  310. aimHandler.StopGyrCalibration();
  311. }
  312. /// <summary>
  313. /// 判断陀螺仪是否正在校准
  314. /// </summary>
  315. /// <returns>陀螺仪是否正在校准</returns>
  316. public bool IsGyrCalibrating()
  317. {
  318. return aimHandler.IsGyrCalibrating();
  319. }
  320. /// <summary>
  321. /// 获取陀螺仪校准进度
  322. /// </summary>
  323. /// <returns>陀螺仪校准进度</returns>
  324. public float GetGyrProgress()
  325. {
  326. return aimHandler.GetGyrProgress();
  327. }
  328. /// <summary>
  329. /// 判断陀螺仪是否校准完成
  330. /// </summary>
  331. /// <returns>陀螺仪是否校准完成</returns>
  332. public bool IsGyrCompleted()
  333. {
  334. return aimHandler.IsGyrCompleted();
  335. }
  336. /// <summary>
  337. /// 开始地磁计校准
  338. /// </summary>
  339. public void StartMagCalibration()
  340. {
  341. aimHandler.StartMagCalibration();
  342. }
  343. /// <summary>
  344. /// 判断地磁计是否校准完成
  345. /// </summary>
  346. /// <returns>地磁计是否校准完成</returns>
  347. public bool IsMagCompleted()
  348. {
  349. return aimHandler.IsMagCompleted();
  350. }
  351. /// <summary>
  352. /// 视角归位
  353. /// </summary>
  354. public void ResetAim()
  355. {
  356. aimHandler.DoIdentity();
  357. }
  358. /// <summary>
  359. /// 监听模块的功能键短按
  360. /// </summary>
  361. public Action OnFunctionKeyPress;
  362. /// <summary>
  363. /// 监听模块的功能键长按
  364. /// </summary>
  365. public Action OnFunctionKeyLongPress;
  366. #endregion
  367. /// <summary>
  368. /// 蓝牙状态变化事件
  369. /// </summary>
  370. /// <param name="oldStatus">旧状态</param>
  371. /// <param name="newStatus">新状态</param>
  372. public delegate void BluetoothStatusChangedEvent(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus);
  373. internal void InvokeOnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
  374. {
  375. try
  376. {
  377. OnBluetoothStatusChanged?.Invoke(oldStatus, newStatus);
  378. }
  379. catch (Exception e)
  380. {
  381. Debug.LogError(e);
  382. }
  383. }
  384. /// <summary>
  385. /// 蓝牙错误事件
  386. /// </summary>
  387. /// <param name="error">错误类型</param>
  388. /// <param name="message">消息描述</param>
  389. public delegate void BluetoothErrorEvent(BluetoothError error, string message);
  390. internal void InvokeOnBluetoothError(BluetoothError error, string message)
  391. {
  392. try
  393. {
  394. OnBluetoothError?.Invoke(error, message);
  395. }
  396. catch (Exception e)
  397. {
  398. Debug.LogError(e);
  399. }
  400. }
  401. internal void InvokeOnFunctionKeyPress()
  402. {
  403. try
  404. {
  405. OnFunctionKeyPress?.Invoke();
  406. }
  407. catch (Exception e)
  408. {
  409. Debug.LogError(e);
  410. }
  411. }
  412. internal void InvokeOnFunctionKeyLongPress()
  413. {
  414. try
  415. {
  416. OnFunctionKeyLongPress?.Invoke();
  417. }
  418. catch (Exception e)
  419. {
  420. Debug.LogError(e);
  421. }
  422. }
  423. /// <summary>
  424. /// 姿态角更新事件
  425. /// </summary>
  426. /// <param name="rotation">姿态角</param>
  427. public delegate void RotationUpdateEvent(Quaternion rotation);
  428. internal void InvokeOnRotationUpdate(Quaternion rotation)
  429. {
  430. if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
  431. try
  432. {
  433. OnRotationUpdate?.Invoke(rotation);
  434. }
  435. catch (Exception e)
  436. {
  437. Debug.LogError(e);
  438. }
  439. }
  440. /// <summary>
  441. /// 射箭事件
  442. /// </summary>
  443. /// <param name="speed">速度(m/s)</param>
  444. public delegate void ShootingEvent(float speed);
  445. internal void InvokeOnShooting(float speed)
  446. {
  447. try
  448. {
  449. OnShooting?.Invoke(speed);
  450. }
  451. catch (Exception e)
  452. {
  453. Debug.LogError(e);
  454. }
  455. }
  456. internal void InvokeOnBluetoothModuleInited()
  457. {
  458. try
  459. {
  460. OnBluetoothModuleInited?.Invoke();
  461. }
  462. catch (Exception e)
  463. {
  464. Debug.LogError(e);
  465. }
  466. }
  467. /// <summary>
  468. /// 监听枪状态
  469. /// </summary>
  470. public BleDeviceEvent OnBleDeviceState;
  471. /// <summary>
  472. /// 硬件对应的事件
  473. /// </summary>
  474. public delegate void BleDeviceEvent(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum);
  475. internal void InvokeOnBleDevice(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum)
  476. {
  477. try
  478. {
  479. OnBleDeviceState?.Invoke(bleDeviceType,gunStatusEnum);
  480. }
  481. catch (Exception e)
  482. {
  483. Debug.LogError(e);
  484. }
  485. }
  486. #region 6轴部分接口
  487. /// <summary>
  488. /// 6轴数据重置初始位置数据
  489. /// </summary>
  490. public void ResetSixAxisIdentity()
  491. {
  492. bluetoothAim.ResetSixAxisFusion();
  493. }
  494. /// <summary>
  495. /// 监听姿态角更新
  496. /// </summary>
  497. public event SixAxisRotationUpdateEvent OnSixAxisRotationUpdate;
  498. /// <summary>
  499. /// 姿态角更新事件
  500. /// </summary>
  501. /// <param name="rotation">姿态角</param>
  502. public delegate void SixAxisRotationUpdateEvent(Vector3 eulerAngles);
  503. internal void InvokeOnSixAxisRotationUpdate(Vector3 eulerAngles)
  504. {
  505. if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
  506. try
  507. {
  508. OnSixAxisRotationUpdate?.Invoke(eulerAngles);
  509. }
  510. catch (Exception e)
  511. {
  512. Debug.LogError(e);
  513. }
  514. }
  515. ///// <summary>
  516. ///// 芯片数据更新事件
  517. ///// </summary>
  518. ///// <param name="rotation">姿态角</param>
  519. //public delegate void AxisDataUpdateEvent(byte[] value);
  520. //internal void InvokeOnAxisDataUpdateEvent(byte[] value)
  521. //{
  522. // if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
  523. // try
  524. // {
  525. // OnAxisDataUpdate?.Invoke(value);
  526. // }
  527. // catch (Exception e)
  528. // {
  529. // Debug.LogError(e);
  530. // }
  531. //}
  532. ///// <summary>
  533. ///// 芯片数据更新
  534. ///// </summary>
  535. //public event AxisDataUpdateEvent OnAxisDataUpdate;
  536. #endregion
  537. }
  538. }