SmartBowHelper.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. using System;
  2. using SmartBowSDK.CMD;
  3. using UnityEngine;
  4. namespace SmartBowSDK
  5. {
  6. public class SmartBowHelper : MonoBehaviour
  7. {
  8. private static SmartBowHelper _Instance;
  9. internal SmartBowNetwork smartBowNetwork;
  10. internal BluetoothAim_SDK bluetoothAim;
  11. internal AimHandler_SDK aimHandler;
  12. internal ShootChecker_SDK shootChecker;
  13. public CalibrateDataStorageMode calibrateDataStorageMode = CalibrateDataStorageMode.Remote;
  14. void Awake()
  15. {
  16. DontDestroyOnLoad(gameObject);
  17. gameObject.AddComponent<AOTAdapter_SDK>();
  18. smartBowNetwork = gameObject.AddComponent<SmartBowNetwork>();
  19. bluetoothAim = gameObject.AddComponent<BluetoothAim_SDK>();
  20. aimHandler = gameObject.AddComponent<AimHandler_SDK>();
  21. shootChecker = gameObject.AddComponent<ShootChecker_SDK>();
  22. smartBowNetwork.smartBowHelper = this;
  23. bluetoothAim.smartBowHelper = this;
  24. aimHandler.smartBowHelper = this;
  25. shootChecker.smartBowHelper = this;
  26. //如果是编辑器运行。自动注册一个windos蓝牙关联
  27. if (BluetoothWindows.IsWindows())
  28. {
  29. BleWinHelper.RegisterTo(this, CreateBluetoothWindows());
  30. }
  31. }
  32. /// <summary>
  33. /// 获取唯一实例
  34. /// </summary>
  35. /// <returns></returns>
  36. public static SmartBowHelper GetInstance()
  37. {
  38. if (!_Instance) _Instance = new GameObject(typeof(SmartBowHelper).Name).AddComponent<SmartBowHelper>();
  39. return _Instance;
  40. }
  41. /// <summary>
  42. /// 创建新的实例
  43. /// </summary>
  44. /// <returns></returns>
  45. public static SmartBowHelper NewInstance()
  46. {
  47. return new GameObject(typeof(SmartBowHelper).Name).AddComponent<SmartBowHelper>();
  48. }
  49. /// <summary>
  50. /// 获取一个window 蓝牙用例
  51. /// </summary>
  52. /// <returns></returns>
  53. public BluetoothWindows CreateBluetoothWindows()
  54. {
  55. return bluetoothAim.CreateWindowBLE();
  56. }
  57. #region 手机验证
  58. /// <summary>
  59. /// 发送手机验证码
  60. /// </summary>
  61. public void SendPhoneValidateCode(string phone, int language, Action<RequestResult> callback)
  62. {
  63. smartBowNetwork.SendPhoneValidateCode(phone, language, callback);
  64. }
  65. /// <summary>
  66. /// 验证手机验证码
  67. /// </summary>
  68. public void ValidatePhone(string phone, int code, Action<RequestResult> callback)
  69. {
  70. smartBowNetwork.ValidatePhoneCode(phone, code, callback);
  71. }
  72. #endregion
  73. #region 邮箱验证
  74. /// <summary>
  75. /// 发送邮箱验证码
  76. /// </summary>
  77. public void SendEmailValidateCode(string email, int language, Action<RequestResult> callback)
  78. {
  79. smartBowNetwork.SendEmailValidateCode(email, language, callback);
  80. }
  81. /// <summary>
  82. /// 验证邮箱验证码
  83. /// </summary>
  84. public void ValidateEmail(string email, int code, Action<RequestResult> callback)
  85. {
  86. smartBowNetwork.SendEmailValidateCode(email, code, callback);
  87. }
  88. #endregion
  89. #region 注册
  90. /// <summary>
  91. /// 用户注册
  92. /// </summary>
  93. /// <param name="serverIndex">服务器索引</param>
  94. /// <param name="username">用户名</param>
  95. /// <param name="password">密码</param>
  96. /// <param name="email">邮箱</param>
  97. /// <param name="phone">手机号</param>
  98. /// <param name="code">当前接收的code,海外用邮箱 serverIndex = 1,国内 serverIndex = 0用手机号</param>
  99. /// <param name="callback">回调</param>
  100. public void Register(int serverIndex, string username, string password, string email, string phone,int code, Action<RequestResult> callback)
  101. {
  102. smartBowNetwork.RegisterByCode(serverIndex, username, password, email, phone, code, callback);
  103. }
  104. /// <summary>
  105. /// 重置密码(支持邮箱或手机号)
  106. /// </summary>
  107. /// <param name="serverIndex">服务器索引</param>
  108. /// <param name="account">账号(邮箱或手机号,现在国内用手机,海外用邮箱)</param>
  109. /// <param name="code">验证码</param>
  110. /// <param name="newPassword">新密码</param>
  111. /// <param name="callback">回调结果</param>
  112. public void ResetPassword(int serverIndex,string account, string codeAccount, int code, string newPassword, Action<RequestResult> callback)
  113. {
  114. smartBowNetwork.ResetPasswordByAccount(serverIndex, account, codeAccount, code, newPassword, callback);
  115. }
  116. #endregion
  117. #region 登录
  118. /// <summary>
  119. /// 用户登录
  120. /// </summary>
  121. /// <param name="gameID">游戏ID</param>
  122. /// <param name="channelID">渠道ID</param>
  123. /// <param name="username">账号</param>
  124. /// <param name="password">密码</param>
  125. /// <param name="callback">回调函数</param>
  126. public void Login(string gameID, string channelID, string username, string password, Action<LoginResult> callback)
  127. {
  128. smartBowNetwork.Login(gameID, channelID, username, password, callback);
  129. }
  130. /// <summary>
  131. /// 用户登出
  132. /// </summary>
  133. /// <param name="callback">回调函数</param>
  134. public void Logout(Action<bool> callback)
  135. {
  136. smartBowNetwork.Logout(callback);
  137. }
  138. /// <summary>
  139. /// 获取用户基本信息
  140. /// </summary>
  141. /// <param name="callback">回调函数</param>
  142. public void GetUserInfo(Action<GetUserInfoResult> callback)
  143. {
  144. smartBowNetwork.GetUserInfo(callback);
  145. }
  146. /// <summary>
  147. /// 设置用户名
  148. /// </summary>
  149. public void SaveUserName(string nickname, Action<RequestResult> callback)
  150. {
  151. smartBowNetwork.SaveUserName(nickname, callback);
  152. }
  153. /// <summary>
  154. /// 设置用户扩展信息
  155. /// </summary>
  156. public void SaveUserInfo(UserInfoExtended userInfo, Action<RequestResult> callback)
  157. {
  158. smartBowNetwork.SaveUserInfo(userInfo, callback);
  159. }
  160. #endregion
  161. #region 分数上传
  162. /// <summary>
  163. /// 上传单人赛季成绩
  164. /// </summary>
  165. public void UploadSeasonSinglePlayerGameRes(string secret, int score, Action<RequestResult> callback)
  166. {
  167. smartBowNetwork.UploadSeasonSinglePlayerGameRes(secret, score, callback);
  168. }
  169. /// <summary>
  170. /// 上传PK赛季成绩
  171. /// </summary>
  172. public void UploadSeasonPKGameRes(string roomKey, string secret, int p2ID, int gameRes, Action<RequestResult> callback)
  173. {
  174. smartBowNetwork.UploadSeasonPKGameRes(roomKey, secret, p2ID, gameRes, callback);
  175. }
  176. #endregion
  177. #region 排行榜
  178. /// <summary>
  179. /// 获取排行榜(按游戏类型)
  180. /// </summary>
  181. public void GetSeasonRankListByGameType(string secret, int rankArea, string country, string state, string city, Action<RequestResult> callback)
  182. {
  183. smartBowNetwork.GetSeasonRankListByGameType(secret, rankArea, country, state, city, callback);
  184. }
  185. /// <summary>
  186. /// 获取自定义排行榜
  187. /// </summary>
  188. public void GetSeasonRankListByCustom(string secret, int rankArea, string country, string state, string city, int rankType, int count, Action<RequestResult> callback)
  189. {
  190. smartBowNetwork.GetSeasonRankListByCustom(secret, rankArea, country, state, city, rankType, count, callback);
  191. }
  192. /// <summary>
  193. /// 获取当前用户的排名信息
  194. /// </summary>
  195. public void GetUserRankByGameType(string secret, int rankArea, string country, string state, string city, int rankType, int count, Action<RequestResult> callback)
  196. {
  197. smartBowNetwork.GetUserRankByGameType(secret, rankArea, country, state, city, rankType, count, callback);
  198. }
  199. #endregion
  200. #region 定制游戏上传接口
  201. /// <summary>
  202. /// 上传自定义排行榜分数
  203. /// </summary>
  204. public void UploadCustomLeaderboardScore(string secret, int taskId, int difficultType, int modeType, int deviceType, int score, Action<RequestResult> callback)
  205. {
  206. smartBowNetwork.UploadCustomLeaderboardScore(secret, taskId, difficultType, modeType, deviceType, score, callback);
  207. }
  208. /// <summary>
  209. /// 获取自定义排行榜数据
  210. /// </summary>
  211. public void GetCustomLeaderboard(string secret, int taskId, int difficultType, int modeType, int deviceType, int timeType, Action<RequestResult> callback)
  212. {
  213. smartBowNetwork.GetCustomLeaderboard(secret, taskId, difficultType, modeType, deviceType, timeType, callback);
  214. }
  215. #endregion
  216. #region 蓝牙:蓝牙部分新增修改,添加当前判断设备mac
  217. ///// <summary>
  218. ///// 连接蓝牙模块
  219. ///// </summary>
  220. //public void Connect()
  221. //{
  222. // //之前的默认不用判断mac
  223. // //当前连接不需要需要检测设备,设置为null
  224. // bluetoothAim.ResetAimDeviceInfoToNull();
  225. // bluetoothAim.Connect();
  226. //}
  227. /// <summary>
  228. /// 设置蓝牙过滤名称
  229. /// </summary>
  230. /// <param name="name">不传参数时候,设置值 ""</param>
  231. public void SetFilters(string name = "") {
  232. if (!string.IsNullOrWhiteSpace(name))
  233. {
  234. bluetoothAim.filters = name;
  235. }
  236. else {
  237. bluetoothAim.filters = "";
  238. }
  239. }
  240. /// <summary>
  241. /// 当前过滤的设备名称
  242. /// </summary>
  243. /// <returns></returns>
  244. public string GetFilters() {
  245. return bluetoothAim.filters;
  246. }
  247. /// <summary>
  248. /// 设置是否使用名称连接
  249. /// </summary>
  250. /// <param name="useName">是否使用名称连接</param>
  251. public void SetIsConnectName(bool useName)
  252. {
  253. bluetoothAim.isConnectName = useName;
  254. }
  255. /// <summary>
  256. /// 获取使用名称连接
  257. /// </summary>
  258. /// <returns>是否使用名称连接</returns>
  259. public bool GetIsConnectName() {
  260. return bluetoothAim.isConnectName;
  261. }
  262. /// <summary>
  263. /// 设置连接的MAC地址
  264. /// </summary>
  265. /// <param name="macStr">MAC地址字符串</param>
  266. public void SetConnectMacStr(string macStr)
  267. {
  268. if (!string.IsNullOrWhiteSpace(macStr))
  269. {
  270. bluetoothAim.connectMacStr = macStr;
  271. }
  272. }
  273. /// <summary>
  274. /// 获取需要连接的MAC地址
  275. /// </summary>
  276. /// <returns>MAC地址字符串</returns>
  277. public string GetConnectMacStr() {
  278. return bluetoothAim.connectMacStr;
  279. }
  280. /// <summary>
  281. /// 设置传感器类型,用来区分解析
  282. /// </summary>
  283. /// <param name="sensorAxisType"></param>
  284. public void SetSensorAxisType(SensorAxisType sensorAxisType)
  285. {
  286. bluetoothAim.sensorAxisType = sensorAxisType;
  287. SmartBowLogger.Log(this, "SetSensorAxisType:" + bluetoothAim.sensorAxisType);
  288. }
  289. /// <summary>
  290. /// 设置cmd超时扫描时间
  291. /// </summary>
  292. /// <param name="timeout"></param>
  293. //public void SetCompanionDeviceManagerScanTimeout(int timeout = 10) {
  294. // bluetoothAim.SetCompanionDeviceManagerScanTimeout(timeout);
  295. //}
  296. /// <summary>
  297. /// 初始化 使用CompanionDeviceManager api 管理连接
  298. /// 切换是否使用 CompanionDeviceManager 模式。
  299. /// </summary>
  300. /// <param name="isCMD">是否使用CompanionDeviceManager连接</param>
  301. public bool UseCompanionDeviceManager(bool isUseCompanionDeviceManager = true)
  302. {
  303. if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.None)
  304. {
  305. SmartBowLogger.LogError(this,
  306. "[SetUseCompanionDeviceManager] 只能在断开状态下调用!");
  307. return bluetoothAim.isUseCompanionDeviceManager;
  308. }
  309. bluetoothAim.isUseCompanionDeviceManager = isUseCompanionDeviceManager;
  310. if (isUseCompanionDeviceManager)
  311. bluetoothAim.InitCMDManager();
  312. else
  313. bluetoothAim.ClearCMDManager();
  314. return isUseCompanionDeviceManager;
  315. }
  316. /// <summary>
  317. /// 获取 CompanionDeviceManager api 管理 状态
  318. /// </summary>
  319. public bool GetUseCompanionDeviceManagerStatus()
  320. {
  321. return bluetoothAim.isUseCompanionDeviceManager;
  322. }
  323. /// <summary>
  324. /// 连接蓝牙模块
  325. /// 新增连接操作,需要用户标签和设备id
  326. /// userTags: 存储的信息标签,按实际用户区分
  327. /// deviceId:对应设备id,同一个用户下的不同设备
  328. /// bRecreateDeviceInfo: 重新创建设备信息。false 的情况下默认限制mac
  329. /// </summary>
  330. public void Connect(string userTags,int deviceId, bool bRecreateDeviceInfo = false)
  331. {
  332. if (userTags == null || userTags.Length == 0)
  333. {
  334. SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
  335. return;
  336. }
  337. if (deviceId <= 0)
  338. {
  339. SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
  340. return;
  341. }
  342. //判断mac需要创建一个连接信息
  343. bluetoothAim.initAimDeviceInfo(userTags,deviceId,bRecreateDeviceInfo);
  344. //进行蓝牙连接
  345. bluetoothAim.ConnectFormType();
  346. }
  347. /// <summary>
  348. /// 清空建立的蓝牙判断数据
  349. /// userTags: 存储的信息标签,按实际用户区分
  350. /// deviceId:对应设备id,同一个用户下的不同设备
  351. /// </summary>
  352. public void ClearDeviceInfo(string userTags,int deviceId)
  353. {
  354. if (userTags == null || userTags.Length == 0)
  355. {
  356. SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
  357. return;
  358. }
  359. if (deviceId <= 0)
  360. {
  361. SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
  362. return ;
  363. }
  364. bluetoothAim.onClearAimDeviceInfoByDeviceId(userTags, deviceId);
  365. }
  366. /// <summary>
  367. /// 获取存储的数据
  368. /// userTags: 存储的信息标签,按实际用户区分
  369. /// deviceId:对应设备id,同一个用户下的不同设备
  370. /// </summary>
  371. public AimDeviceInfo GetDeviceInfo(string userTags, int deviceId) {
  372. if (userTags == null || userTags.Length == 0)
  373. {
  374. SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
  375. return null;
  376. }
  377. if (deviceId <= 0)
  378. {
  379. SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
  380. return null;
  381. }
  382. return bluetoothAim.onGetAimDeviceInfos(userTags, deviceId);
  383. }
  384. /// <summary>
  385. /// 断开蓝牙模块
  386. /// </summary>
  387. public void Disconnect()
  388. {
  389. bluetoothAim.Disconnect();
  390. }
  391. /// <summary>
  392. /// 获取蓝牙状态
  393. /// </summary>
  394. /// <returns>蓝牙状态</returns>
  395. public BluetoothStatusEnum GetBluetoothStatus()
  396. {
  397. return bluetoothAim.bluetoothStatus;
  398. }
  399. /// <summary>
  400. /// 监听蓝牙状态变化
  401. /// </summary>
  402. public event BluetoothStatusChangedEvent OnBluetoothStatusChanged;
  403. /// <summary>
  404. /// 判断蓝牙模块是否初始化完成
  405. /// </summary>
  406. /// <returns>蓝牙模块是否初始化完成</returns>
  407. public bool IsBluetoothModuleInited()
  408. {
  409. return bluetoothAim.moduleInited;
  410. }
  411. /// <summary>
  412. /// 监听蓝牙模块初始化完成
  413. /// Tip:连接蓝牙模块成功后会有几秒的初始化过程,初始化完成后才能进行传感器相关的函数调用。
  414. /// </summary>
  415. public Action OnBluetoothModuleInited;
  416. /// <summary>
  417. /// 监听蓝牙错误
  418. /// </summary>
  419. public event BluetoothErrorEvent OnBluetoothError;
  420. /// <summary>
  421. /// 监听CMD状态
  422. /// </summary>
  423. public event CMDStateEvent OnCMDState;
  424. /// <summary>
  425. /// 获取Mac地址
  426. /// Tip:需要模块初始化完成才能获取
  427. /// </summary>
  428. /// <returns>如果返回null则是获取失败</returns>
  429. public string GetMacAddress()
  430. {
  431. return bluetoothAim.macAddress;
  432. }
  433. /// <summary>
  434. /// 获取硬件固件版本
  435. /// Tip:需要模块初始化完成才能获取
  436. /// </summary>
  437. /// <returns>如果返回null则是获取失败</returns>
  438. public string GetDeviceVersion()
  439. {
  440. return bluetoothAim.deviceVersion;
  441. }
  442. /// <summary>
  443. /// 获取电量
  444. /// Tip:需要模块初始化完成才能获取
  445. /// </summary>
  446. /// <returns>电量值(范围0~100),值为0时也代表获取失败</returns>
  447. public int GetBattery()
  448. {
  449. return bluetoothAim.battery;
  450. }
  451. /// <summary>
  452. /// 设置目标设备名称(用于搜索和连接)
  453. /// </summary>
  454. /// <param name="deviceName">设备名称</param>
  455. public void SetTargetDeviceName(string deviceName)
  456. {
  457. bluetoothAim.deviceConfig.deviceName = deviceName;
  458. }
  459. #endregion
  460. #region 传感器
  461. /// <summary>
  462. /// 开启九轴传感
  463. /// </summary>
  464. /// <returns>请求是否发送成功</returns>
  465. public bool StartRotationSensor()
  466. {
  467. return bluetoothAim.RequestOpen9Axis();
  468. }
  469. /// <summary>
  470. /// 停止九轴传感
  471. /// </summary>
  472. /// <returns>请求是否发送成功</returns>
  473. public bool StopRotationSensor()
  474. {
  475. return bluetoothAim.RequestClose9Axis();
  476. }
  477. /// <summary>
  478. /// 监听姿态角更新
  479. /// </summary>
  480. public event RotationUpdateEvent OnRotationUpdate;
  481. /// <summary>
  482. /// 开启射箭传感
  483. /// </summary>
  484. /// <returns>请求是否发送成功</returns>
  485. public bool StartShootingSensor()
  486. {
  487. return bluetoothAim.RequestOpenInfrared();
  488. }
  489. /// <summary>
  490. /// 停止射箭传感
  491. /// </summary>
  492. /// <returns>请求是否发送成功</returns>
  493. public bool StopShootingSensor()
  494. {
  495. return bluetoothAim.RequestCloseInfrared();
  496. }
  497. /// <summary>
  498. /// 监听射箭
  499. /// </summary>
  500. public ShootingEvent OnShooting;
  501. /// <summary>
  502. /// 开始陀螺仪校准
  503. /// </summary>
  504. public void StartGyrCalibration()
  505. {
  506. aimHandler.StartGyrCalibration();
  507. }
  508. /// <summary>
  509. /// 停止陀螺仪校准
  510. /// </summary>
  511. public void StopGyrCalibration()
  512. {
  513. aimHandler.StopGyrCalibration();
  514. }
  515. /// <summary>
  516. /// 判断陀螺仪是否正在校准
  517. /// </summary>
  518. /// <returns>陀螺仪是否正在校准</returns>
  519. public bool IsGyrCalibrating()
  520. {
  521. return aimHandler.IsGyrCalibrating();
  522. }
  523. /// <summary>
  524. /// 获取陀螺仪校准进度
  525. /// </summary>
  526. /// <returns>陀螺仪校准进度</returns>
  527. public float GetGyrProgress()
  528. {
  529. return aimHandler.GetGyrProgress();
  530. }
  531. /// <summary>
  532. /// 判断陀螺仪是否校准完成
  533. /// </summary>
  534. /// <returns>陀螺仪是否校准完成</returns>
  535. public bool IsGyrCompleted()
  536. {
  537. return aimHandler.IsGyrCompleted();
  538. }
  539. /// <summary>
  540. /// 开始地磁计校准
  541. /// </summary>
  542. public void StartMagCalibration()
  543. {
  544. aimHandler.StartMagCalibration();
  545. }
  546. /// <summary>
  547. /// 判断地磁计是否校准完成
  548. /// </summary>
  549. /// <returns>地磁计是否校准完成</returns>
  550. public bool IsMagCompleted()
  551. {
  552. return aimHandler.IsMagCompleted();
  553. }
  554. /// <summary>
  555. /// 视角归位
  556. /// </summary>
  557. public void ResetAim()
  558. {
  559. aimHandler.DoIdentity();
  560. }
  561. /// <summary>
  562. /// 监听模块的功能键短按
  563. /// </summary>
  564. public Action OnFunctionKeyPress;
  565. /// <summary>
  566. /// 监听模块的功能键长按
  567. /// </summary>
  568. public Action OnFunctionKeyLongPress;
  569. #endregion
  570. /// <summary>
  571. /// 蓝牙状态变化事件
  572. /// </summary>
  573. /// <param name="oldStatus">旧状态</param>
  574. /// <param name="newStatus">新状态</param>
  575. public delegate void BluetoothStatusChangedEvent(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus);
  576. internal void InvokeOnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
  577. {
  578. try
  579. {
  580. OnBluetoothStatusChanged?.Invoke(oldStatus, newStatus);
  581. }
  582. catch (Exception e)
  583. {
  584. Debug.LogError(e);
  585. }
  586. }
  587. /// <summary>
  588. /// 蓝牙错误事件
  589. /// </summary>
  590. /// <param name="error">错误类型</param>
  591. /// <param name="message">消息描述</param>
  592. public delegate void BluetoothErrorEvent(BluetoothError error, string message);
  593. internal void InvokeOnBluetoothError(BluetoothError error, string message)
  594. {
  595. try
  596. {
  597. OnBluetoothError?.Invoke(error, message);
  598. }
  599. catch (Exception e)
  600. {
  601. Debug.LogError(e);
  602. }
  603. }
  604. /// <summary>
  605. /// CMD状态回调
  606. /// </summary>
  607. /// <param name="state"></param>
  608. /// <param name="message"></param>
  609. public delegate void CMDStateEvent(CMDScanState state, string message);
  610. internal void InvokeOnCMDState(CMDScanState state, string message)
  611. {
  612. try
  613. {
  614. OnCMDState?.Invoke(state, message);
  615. }
  616. catch (Exception e)
  617. {
  618. Debug.LogError(e);
  619. }
  620. }
  621. internal void InvokeOnFunctionKeyPress()
  622. {
  623. try
  624. {
  625. OnFunctionKeyPress?.Invoke();
  626. }
  627. catch (Exception e)
  628. {
  629. Debug.LogError(e);
  630. }
  631. }
  632. internal void InvokeOnFunctionKeyLongPress()
  633. {
  634. try
  635. {
  636. OnFunctionKeyLongPress?.Invoke();
  637. }
  638. catch (Exception e)
  639. {
  640. Debug.LogError(e);
  641. }
  642. }
  643. /// <summary>
  644. /// 姿态角更新事件
  645. /// </summary>
  646. /// <param name="rotation">姿态角</param>
  647. public delegate void RotationUpdateEvent(Quaternion rotation);
  648. internal void InvokeOnRotationUpdate(Quaternion rotation)
  649. {
  650. if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
  651. try
  652. {
  653. OnRotationUpdate?.Invoke(rotation);
  654. }
  655. catch (Exception e)
  656. {
  657. Debug.LogError(e);
  658. }
  659. }
  660. /// <summary>
  661. /// 射箭事件
  662. /// </summary>
  663. /// <param name="speed">速度(m/s)</param>
  664. public delegate void ShootingEvent(float speed);
  665. internal void InvokeOnShooting(float speed)
  666. {
  667. try
  668. {
  669. OnShooting?.Invoke(speed);
  670. }
  671. catch (Exception e)
  672. {
  673. Debug.LogError(e);
  674. }
  675. }
  676. internal void InvokeOnBluetoothModuleInited()
  677. {
  678. try
  679. {
  680. OnBluetoothModuleInited?.Invoke();
  681. }
  682. catch (Exception e)
  683. {
  684. Debug.LogError(e);
  685. }
  686. }
  687. /// <summary>
  688. /// 监听枪状态
  689. /// </summary>
  690. public BleDeviceEvent OnBleDeviceState;
  691. /// <summary>
  692. /// 硬件对应的事件
  693. /// </summary>
  694. public delegate void BleDeviceEvent(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum);
  695. internal void InvokeOnBleDevice(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum)
  696. {
  697. try
  698. {
  699. OnBleDeviceState?.Invoke(bleDeviceType,gunStatusEnum);
  700. }
  701. catch (Exception e)
  702. {
  703. Debug.LogError(e);
  704. }
  705. }
  706. /// <summary>
  707. /// 监听设备状态和连接的设备平台信息
  708. /// </summary>
  709. public DeviceAndSystemInfoEvent OnDeviceAndSystemInfoEvent;
  710. /// <summary>
  711. /// 硬件对应的事件初始化回调
  712. /// </summary>
  713. public delegate void DeviceAndSystemInfoEvent(ConnectPlatform connectPlatform,BluetoothDeviceType bleDeviceType);
  714. internal void InvokeOnDeviceAndSystemInfoEvent(ConnectPlatform connectPlatform, BluetoothDeviceType bleDeviceType)
  715. {
  716. try
  717. {
  718. OnDeviceAndSystemInfoEvent?.Invoke(connectPlatform, bleDeviceType);
  719. }
  720. catch (Exception e)
  721. {
  722. Debug.LogError(e);
  723. }
  724. }
  725. #region 6轴部分接口
  726. /// <summary>
  727. /// 6轴数据重置初始位置数据
  728. /// </summary>
  729. public void ResetSixAxisIdentity()
  730. {
  731. bluetoothAim.ResetSixAxisFusion();
  732. }
  733. /// <summary>
  734. /// 监听姿态角更新
  735. /// </summary>
  736. public event SixAxisRotationUpdateEvent OnSixAxisRotationUpdate;
  737. /// <summary>
  738. /// 姿态角更新事件
  739. /// </summary>
  740. /// <param name="rotation">姿态角</param>
  741. public delegate void SixAxisRotationUpdateEvent(Vector3 eulerAngles);
  742. internal void InvokeOnSixAxisRotationUpdate(Vector3 eulerAngles)
  743. {
  744. if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
  745. try
  746. {
  747. OnSixAxisRotationUpdate?.Invoke(eulerAngles);
  748. }
  749. catch (Exception e)
  750. {
  751. Debug.LogError(e);
  752. }
  753. }
  754. ///// <summary>
  755. ///// 芯片数据更新事件
  756. ///// </summary>
  757. ///// <param name="rotation">姿态角</param>
  758. //public delegate void AxisDataUpdateEvent(byte[] value);
  759. //internal void InvokeOnAxisDataUpdateEvent(byte[] value)
  760. //{
  761. // if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
  762. // try
  763. // {
  764. // OnAxisDataUpdate?.Invoke(value);
  765. // }
  766. // catch (Exception e)
  767. // {
  768. // Debug.LogError(e);
  769. // }
  770. //}
  771. ///// <summary>
  772. ///// 芯片数据更新
  773. ///// </summary>
  774. //public event AxisDataUpdateEvent OnAxisDataUpdate;
  775. #endregion
  776. }
  777. }