BLEManager.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using SmartBowSDK;
  5. using UnityEngine.UI;
  6. using System;
  7. using static ScreenLocate;
  8. namespace InfraredDLLManager
  9. {
  10. public class BLEManager : MonoBehaviour
  11. {
  12. #region 线上默认自动连接扫描
  13. //默认自动连接扫描
  14. [Header("连接扫描属性")]
  15. [Tooltip("是否自动连接扫描")]
  16. [SerializeField]
  17. bool autoConnectAndScan = false;
  18. bool isConnectName = true;//默认用名字连接
  19. string connectMacStr;//当前需要连接的mac
  20. private float notUserDoConnectTime = 0f;
  21. private float autoConnectInterval = 5f; // 自动连接间隔时间,单位秒
  22. #endregion
  23. //数据存储标识
  24. string userTags = "test";
  25. //建立的设备信息id.
  26. int deviceId = 1;
  27. public Text fpsText;
  28. public float shootSpeed = 0;
  29. #region log输出
  30. public Action<string> OnLogAction;
  31. void logOutBow(string text)
  32. {
  33. Debug.Log("logOutBow:" + text);
  34. OnLogAction?.Invoke(text);
  35. }
  36. #endregion
  37. // Start is called before the first frame update
  38. void Start()
  39. {
  40. InitBluetoothObj();
  41. InitBluetoothObj2();
  42. }
  43. // Update is called once per frame
  44. void Update()
  45. {
  46. }
  47. public void SetFPS(float fps) {
  48. if(fpsText) fpsText.text = "FPS:"+ fps.ToString("F2");
  49. }
  50. //弓箭蓝牙
  51. SmartBowHelper BowSmartBowHelper;
  52. public SmartBowHelper MyBowSmartBowHelper => BowSmartBowHelper;
  53. void InitBluetoothObj()
  54. {
  55. SmartBowLogger.isDebug = true;
  56. BowSmartBowHelper = SmartBowHelper.NewInstance();
  57. BowSmartBowHelper.OnBluetoothModuleInited += OnBluetoothModuleInited;
  58. BowSmartBowHelper.OnBluetoothError += OnBluetoothError;
  59. BowSmartBowHelper.OnShooting += OnShooting1P;
  60. BowSmartBowHelper.OnBleDeviceState += OnBleDeviceState;
  61. BowSmartBowHelper.OnDeviceAndSystemInfoEvent += OnDeviceAndSystemInfoEvent;
  62. BowSmartBowHelper.OnFunctionKeyPress += OnFunctionKeyPress;
  63. BowSmartBowHelper.OnFunctionKeyLongPress += OnFunctionKeyLongPress;
  64. //进去开启自动搜索连接
  65. //autoConnectAndScan = true;
  66. //buttonScanType = ButtonScanType.AutoConnectBtn;
  67. //Debug.Log("开启自动搜索连接 ButtonScanType:" + buttonScanType);
  68. }
  69. SmartBowHelper BowSmartBowHelper2;
  70. public SmartBowHelper MyBowSmartBowHelper2 => BowSmartBowHelper2;
  71. void InitBluetoothObj2()
  72. {
  73. BowSmartBowHelper2 = SmartBowHelper.NewInstance();
  74. BowSmartBowHelper2.OnBluetoothModuleInited =()=> {
  75. Debug.Log("OnBluetoothModuleInited2");
  76. logOutBow("2P 设备 Mac:" + BowSmartBowHelper2.GetMacAddress() + "\n电量:" + BowSmartBowHelper2.GetBattery());
  77. };
  78. BowSmartBowHelper2.OnBluetoothError += OnBluetoothError;
  79. BowSmartBowHelper2.OnShooting += OnShooting2P;
  80. BowSmartBowHelper2.OnFunctionKeyPress += OnFunctionKeyPress;
  81. BowSmartBowHelper2.OnFunctionKeyLongPress += OnFunctionKeyLongPress;
  82. }
  83. void OnBluetoothModuleInited()
  84. {
  85. Debug.Log("OnBluetoothModuleInited:");
  86. BowSmartBowHelper.StartRotationSensor();
  87. BowSmartBowHelper.StartShootingSensor();
  88. logOutBow("1P 设备 Mac:" + BowSmartBowHelper.GetMacAddress() +"\n电量:"+ BowSmartBowHelper.GetBattery());
  89. }
  90. void OnBluetoothError(BluetoothError error, string message)
  91. {
  92. Debug.Log("OnBluetoothError:" + error);
  93. logOutBow("连接失败:" + error);
  94. if (error == BluetoothError.ScanNotFoundTargetDevice)
  95. {
  96. TipText.Show("连接失败,未发现目标设备!");
  97. return;
  98. }
  99. TipText.Show(message);
  100. }
  101. //加一个枪射击的触发时间
  102. float _lastShootTime = 0;
  103. void OnShooting1P(float speed)
  104. {
  105. //Bow.Instance.Shoot();
  106. //流程触发红外描点
  107. InfraredScreenPositioningView infraredScreenPositioningView = FindAnyObjectByType<InfraredScreenPositioningView>();
  108. if (infraredScreenPositioningView)
  109. {
  110. if (Time.realtimeSinceStartup - _lastShootTime < 1) return;
  111. _lastShootTime = Time.realtimeSinceStartup;
  112. InvokeOnCrossBtnEvent();
  113. }
  114. else
  115. {
  116. shootSpeed = speed;
  117. Debug.Log("1P射击速度:" + shootSpeed);
  118. }
  119. logOutBow("1P射击速度:" + shootSpeed);
  120. }
  121. void OnShooting2P(float speed)
  122. {
  123. logOutBow("2P射击速度:" + shootSpeed);
  124. }
  125. void OnFunctionKeyPress()
  126. {
  127. Debug.Log("功能键短按");
  128. InfraredScreenPositioningView infraredScreenPositioningView = FindAnyObjectByType<InfraredScreenPositioningView>();
  129. if (infraredScreenPositioningView)
  130. {
  131. InvokeOnCrossBtnEvent();
  132. }
  133. else
  134. {
  135. //显示控制准心按钮
  136. }
  137. }
  138. void OnFunctionKeyLongPress()
  139. {
  140. Debug.Log("HOUYI Pro使用长按视角归位键;在ARTEMIS Pro使用快速双击按键");
  141. }
  142. /// <summary>
  143. /// 设备类型和设备操作,比如上膛,换弹夹等
  144. /// </summary>
  145. /// <param name="bluetoothDeviceType"></param>
  146. /// <param name="bluetoothDeviceStatus"></param>
  147. void OnBleDeviceState(BluetoothDeviceType bluetoothDeviceType, BluetoothDeviceStatus bluetoothDeviceStatus)
  148. {
  149. logOutBow("设备类型:" + bluetoothDeviceType + ",状态:" + bluetoothDeviceStatus);
  150. }
  151. /// <summary>
  152. /// 设备类型和设备信息
  153. /// </summary>
  154. /// <param name="connectPlatform"></param>
  155. /// <param name="bluetoothDeviceType"></param>
  156. void OnDeviceAndSystemInfoEvent(ConnectPlatform connectPlatform, BluetoothDeviceType bluetoothDeviceType)
  157. {
  158. logOutBow("当前设备平台:"+ connectPlatform+",设备类型:" + bluetoothDeviceType);
  159. }
  160. /// <summary>
  161. /// 根据名字搜索
  162. /// </summary>
  163. public void ConnectBluetoothDevice(SmartBowHelper _BowSmartBowHelper)
  164. {
  165. connectMacStr = "";
  166. _BowSmartBowHelper.SetFilters("HOUYI Pro | ARTEMIS Pro | Pistol | Pistol M9");
  167. connectBLEDevice(_BowSmartBowHelper);
  168. }
  169. //根据mac匹配
  170. public void ConnectBluetoothDeviceByMac(SmartBowHelper _BowSmartBowHelper,string mac)
  171. {
  172. connectMacStr = mac;
  173. connectBLEDevice(_BowSmartBowHelper);
  174. }
  175. void connectBLEDevice(SmartBowHelper _BowSmartBowHelper)
  176. {
  177. _BowSmartBowHelper.SetIsConnectName(isConnectName);
  178. _BowSmartBowHelper.SetConnectMacStr(connectMacStr);
  179. _BowSmartBowHelper.Connect(userTags, deviceId, true);
  180. }
  181. /// <summary>
  182. /// 自动连接设备,根据是否使用名称或MAC地址连接
  183. /// </summary>
  184. private void AutoConnectDevice()
  185. {
  186. if (isConnectName)
  187. {
  188. Debug.Log("自动根据名称连接设备...");
  189. ConnectBluetoothDevice(BowSmartBowHelper);
  190. }
  191. //else if (!string.IsNullOrEmpty(connectMacStr))
  192. //{
  193. // Debug.Log($"自动根据MAC连接设备: {connectMacStr}");
  194. // ConnectBluetoothDeviceByMac(connectMacStr);
  195. //}
  196. }
  197. public void DisConnectBLEDevice()
  198. {
  199. logOutBow("DisConnectBLEDevice");
  200. BluetoothStatusEnum _currentStatus = BowSmartBowHelper.GetBluetoothStatus();
  201. if (_currentStatus == BluetoothStatusEnum.Connected)
  202. {
  203. BowSmartBowHelper.Disconnect();
  204. }
  205. }
  206. public OnCrossBtnEventEvent OnCrossBtnEvent;
  207. public delegate void OnCrossBtnEventEvent();
  208. /// <summary>
  209. /// 准心事件
  210. /// </summary>
  211. public void InvokeOnCrossBtnEvent()
  212. {
  213. try
  214. {
  215. OnCrossBtnEvent?.Invoke();
  216. }
  217. catch (System.Exception e)
  218. {
  219. Debug.LogError(e);
  220. }
  221. }
  222. public void ButtonShoot()
  223. {
  224. //posAction?.Invoke(new Vector3(1 * Screen.width, 1 * Screen.height, 0));
  225. ////调用游戏中的射箭接口
  226. //if (ArmBow.ins)
  227. //{
  228. // ArmBow.ins.ADS_fire(true);
  229. //}
  230. //else
  231. //{
  232. // OnGameShoot?.Invoke(10);
  233. //}
  234. }
  235. }
  236. }