BleWinHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using UnityEngine;
  6. using System.Runtime.InteropServices;
  7. namespace SmartBowSDK
  8. {
  9. /// <summary>
  10. /// Windows连接BluetoothLE
  11. /// 我的扫描逻辑默认了读写特征都在同一服务下
  12. /// </summary>
  13. public class BleWinHelper : MonoBehaviour
  14. {
  15. public string LogTag = "BleWinHelper-Log: ";
  16. public bool bDebug = false;
  17. private void Log(string text)
  18. {
  19. if (bDebug)Debug.Log(LogTag + text);
  20. }
  21. private void Warn(string text)
  22. {
  23. if (bDebug)Debug.LogWarning(LogTag + text);
  24. }
  25. private void Error(string text)
  26. {
  27. if (bDebug)Debug.Log(LogTag + text);
  28. }
  29. private string targetDeviceName = "Bbow_20210501 | ARTEMIS Pro";
  30. private string targetDeviceNameHOUYIPro = "HOUYI Pro | Bbow_20210501";
  31. private string targetDeviceNameGun = "Pistol | Pistol M9 | Bbow_20210501";
  32. private string targetService = "{0000fff0-0000-1000-8000-00805f9b34fb}";
  33. private string targetCharacteristicsNotify = "{0000fff1-0000-1000-8000-00805f9b34fb}";
  34. private string targetCharacteristicsWrite = "{0000fff2-0000-1000-8000-00805f9b34fb}";
  35. private bool isConnectLocking = false;
  36. private bool isScanningDevices = false;
  37. private bool isScanningServices = false;
  38. private bool isScanningCharacteristics = false;
  39. private bool isSubscribed = false;
  40. private bool isSubscribing = false;
  41. private string lastError = null;
  42. [SerializeField]
  43. private string selectedDeviceId = null;
  44. private Dictionary<string, Dictionary<string, string>> deviceList = new Dictionary<string, Dictionary<string, string>>();
  45. private List<string> serviceList = new List<string>();
  46. private List<string> characteristicsList = new List<string>();
  47. private float _connectedTime = 0;
  48. private float _receiveDataTime = 0;
  49. private float _heartBeatInterval = 0;
  50. private Action OnConnected;
  51. /// <summary>
  52. /// 主动调用Disconnect()不会触发该委托
  53. /// </summary>
  54. private Action OnConnectionFailed;
  55. private static Action<string, byte[]> OnCharacteristicChanged;
  56. /// <summary>
  57. /// 注册window对象
  58. /// </summary>
  59. /// <param name="o">挂载对象</param>
  60. /// <param name="bluetoothWindows">关联的BluetoothWindows</param>
  61. /// <param name="logTip">提示的log标签</param>
  62. /// <returns></returns>
  63. public static BleWinHelper RegisterTo(GameObject o,BluetoothWindows bluetoothWindows,string logTip = "first")
  64. {
  65. //if (_Instance)
  66. //{
  67. // Error("Register fail, because only one can be registered.");
  68. // return null;
  69. //}
  70. GameObject obj = new GameObject("BleWinHelper"+ logTip);
  71. obj.transform.SetParent(o.transform);
  72. BleWinHelper bleWinHelper = obj.AddComponent<BleWinHelper>();
  73. //日志名字
  74. bleWinHelper.LogTag = "BleWinHelper-" + logTip + ": ";
  75. bluetoothWindows.Connect = bleWinHelper.Connect;
  76. bluetoothWindows.Disconnect = bleWinHelper.Disconnect;
  77. bluetoothWindows.Write = bleWinHelper.Write;
  78. bluetoothWindows.WriteByte = bleWinHelper.WriteByte;
  79. bleWinHelper.OnConnected = () => bluetoothWindows.OnConnected?.Invoke();
  80. bleWinHelper.OnConnectionFailed = () => bluetoothWindows.OnConnectionFailed?.Invoke();
  81. //多个定义共用一个OnCharacteristicChanged
  82. OnCharacteristicChanged += (deviceID,bytes) => {
  83. if (deviceID == bleWinHelper.selectedDeviceId) {
  84. bluetoothWindows.OnCharacteristicChanged?.Invoke(deviceID, bytes);
  85. }
  86. };
  87. return bleWinHelper;
  88. }
  89. /// <summary>
  90. /// 设置心跳检测
  91. /// 1.每次收到的蓝牙数据都视为心跳
  92. /// 2.帮助触发蓝牙断开监听
  93. /// </summary>
  94. /// <param name="interval">心跳检测间隔</param>
  95. public void SetHeartBeat(float interval)
  96. {
  97. _heartBeatInterval = interval;
  98. }
  99. //private static BleWinHelper _Instance;
  100. void Awake()
  101. {
  102. // _Instance = this;
  103. }
  104. void OnDestroy()
  105. {
  106. // if (_Instance == this) _Instance = null;
  107. BleApi.Quit();
  108. }
  109. void Update()
  110. {
  111. BleApi.ScanStatus status;
  112. if (isScanningDevices)
  113. {
  114. BleApi.DeviceUpdate res = new BleApi.DeviceUpdate();
  115. do
  116. {
  117. status = BleApi.PollDevice(ref res, false);
  118. if (status == BleApi.ScanStatus.AVAILABLE)
  119. {
  120. if (!deviceList.ContainsKey(res.id))
  121. deviceList[res.id] = new Dictionary<string, string>() {
  122. { "name", "" },
  123. { "isConnectable", "False" }
  124. };
  125. if (res.nameUpdated)
  126. deviceList[res.id]["name"] = res.name;
  127. if (res.isConnectableUpdated)
  128. deviceList[res.id]["isConnectable"] = res.isConnectable.ToString();
  129. //deviceList[res.id]["name"] == targetDeviceName
  130. //if (targetDeviceName.Contains(deviceList[res.id]["name"]) && deviceList[res.id]["isConnectable"] == "True")
  131. //{
  132. // selectedDeviceId = res.id;
  133. // StopDeviceScan();
  134. //}
  135. if (AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.HOUYIPRO)
  136. { //需要判断是否是红外弓箭
  137. if (targetDeviceNameHOUYIPro.Contains(deviceList[res.id]["name"]) && deviceList[res.id]["isConnectable"] == "True")
  138. {
  139. selectedDeviceId = res.id;
  140. StopDeviceScan();
  141. }
  142. }
  143. else if (AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.ARTEMISPRO)
  144. { //需要判断是否是ARTEMISPRO弓箭
  145. if (targetDeviceName.Contains(deviceList[res.id]["name"]) && deviceList[res.id]["isConnectable"] == "True")
  146. {
  147. selectedDeviceId = res.id;
  148. StopDeviceScan();
  149. }
  150. }
  151. else if (AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.Gun)
  152. {
  153. //需要判断是否是枪
  154. if (targetDeviceNameGun.Contains(deviceList[res.id]["name"]) && deviceList[res.id]["isConnectable"] == "True")
  155. {
  156. selectedDeviceId = res.id;
  157. StopDeviceScan();
  158. }
  159. }
  160. else
  161. { //其余的九轴连接
  162. if (targetDeviceName.Contains(deviceList[res.id]["name"]) && deviceList[res.id]["isConnectable"] == "True")
  163. {
  164. selectedDeviceId = res.id;
  165. StopDeviceScan();
  166. }
  167. }
  168. }
  169. else if (status == BleApi.ScanStatus.FINISHED)
  170. {
  171. StartCoroutine(ScanServiceAndCharacteristicsToSubscribe());
  172. }
  173. } while (status == BleApi.ScanStatus.AVAILABLE);
  174. }
  175. if (isScanningServices)
  176. {
  177. BleApi.Service res;
  178. do
  179. {
  180. status = BleApi.PollService(out res, false);
  181. if (status == BleApi.ScanStatus.AVAILABLE)
  182. {
  183. serviceList.Add(res.uuid);
  184. }
  185. else if (status == BleApi.ScanStatus.FINISHED)
  186. {
  187. isScanningServices = false;
  188. }
  189. } while (status == BleApi.ScanStatus.AVAILABLE);
  190. }
  191. if (isScanningCharacteristics)
  192. {
  193. BleApi.Characteristic res;
  194. do
  195. {
  196. status = BleApi.PollCharacteristic(out res, false);
  197. if (status == BleApi.ScanStatus.AVAILABLE)
  198. {
  199. Log("res.userDescription:"+ res.userDescription+ ",res.uuid:" + res.uuid);
  200. if (res.userDescription == "no description available" || //旧设备
  201. res.userDescription == "SPP Write Channel" || //新设备
  202. res.userDescription == "SPP Read Channel")
  203. {
  204. characteristicsList.Add(res.uuid);
  205. }
  206. else {
  207. //跟着之前代码
  208. characteristicsList.Add(res.userDescription);
  209. }
  210. //string name = res.userDescription != "no description available" ? res.userDescription : res.uuid;
  211. //characteristicsList.Add(name);
  212. }
  213. else if (status == BleApi.ScanStatus.FINISHED)
  214. {
  215. isScanningCharacteristics = false;
  216. }
  217. } while (status == BleApi.ScanStatus.AVAILABLE);
  218. }
  219. if (isSubscribed)
  220. {
  221. BleApi.BLEData res;
  222. while (BleApi.PollData(out res, false))
  223. {
  224. //string text = BitConverter.ToString(res.buf, 0, res.size);
  225. // string text = Encoding.ASCII.GetString(res.buf, 0, res.size);
  226. byte[] bytes = new byte[res.size];
  227. Array.Copy(res.buf, bytes, res.size);
  228. _receiveDataTime = Time.realtimeSinceStartup;
  229. OnCharacteristicChanged?.Invoke(res.deviceId, bytes);
  230. }
  231. }
  232. {
  233. BleApi.ErrorMessage res;
  234. BleApi.GetError(out res);
  235. if (lastError != res.msg)
  236. {
  237. Error(res.msg);
  238. lastError = res.msg;
  239. //对应设备才断开
  240. if (lastError.Contains("SendDataAsync") && lastError.Contains(selectedDeviceId) && isSubscribed)
  241. {
  242. HandleConnectFail();
  243. }
  244. }
  245. }
  246. }
  247. void LateUpdate()
  248. {
  249. if (
  250. _heartBeatInterval > 0 &&
  251. isSubscribed &&
  252. Time.realtimeSinceStartup - _connectedTime >= _heartBeatInterval &&
  253. Time.realtimeSinceStartup - _receiveDataTime >= _heartBeatInterval
  254. )
  255. {
  256. HandleConnectFail();
  257. }
  258. }
  259. private bool Connect()
  260. {
  261. if (isConnectLocking || isScanningDevices)
  262. {
  263. Warn("Connect Invalid, because is in connect.");
  264. return false;
  265. }
  266. BleApi.StartDeviceScan();
  267. isConnectLocking = true;
  268. isScanningDevices = true;
  269. Log("Start Connect");
  270. return true;
  271. }
  272. private void StopDeviceScan()
  273. {
  274. if (!isScanningDevices) return;
  275. BleApi.StopDeviceScan();
  276. isScanningDevices = false;
  277. }
  278. private IEnumerator ScanServiceAndCharacteristicsToSubscribe()
  279. {
  280. isSubscribing = true;
  281. if (selectedDeviceId == null)
  282. {
  283. HandleConnectFail();
  284. yield break;
  285. }
  286. Log("SelectedDeviceId OK");
  287. BleApi.ScanServices(selectedDeviceId);
  288. isScanningServices = true;
  289. serviceList.Clear();
  290. while (isScanningServices) yield return null;
  291. bool findTargetService = false;
  292. foreach (string service in serviceList)
  293. {
  294. if (service == targetService)
  295. {
  296. findTargetService = true;
  297. Log("FindTargetService OK");
  298. break;
  299. }
  300. }
  301. if (!findTargetService)
  302. {
  303. HandleConnectFail();
  304. yield break;
  305. }
  306. BleApi.ScanCharacteristics(selectedDeviceId, targetService);
  307. isScanningCharacteristics = true;
  308. characteristicsList.Clear();
  309. while (isScanningCharacteristics) yield return null;
  310. bool findTargetCharacteristicsNotify = false;
  311. bool findTargetCharacteristicsWrite = false;
  312. foreach (string characteristics in characteristicsList)
  313. {
  314. if (characteristics == targetCharacteristicsNotify)
  315. {
  316. findTargetCharacteristicsNotify = true;
  317. Log("FindTargetCharacteristicsNotify OK");
  318. }
  319. else if (characteristics == targetCharacteristicsWrite)
  320. {
  321. findTargetCharacteristicsWrite = true;
  322. Log("FindTargetCharacteristicsWrite OK");
  323. }
  324. }
  325. if (!findTargetCharacteristicsNotify || !findTargetCharacteristicsWrite)
  326. {
  327. HandleConnectFail();
  328. yield break;
  329. }
  330. BleApi.SubscribeCharacteristic(selectedDeviceId, targetService, targetCharacteristicsNotify, false);
  331. isSubscribed = true;
  332. isSubscribing = false;
  333. Log("SubscribeCharacteristicNotify OK");
  334. _connectedTime = Time.realtimeSinceStartup;
  335. OnConnected?.Invoke();
  336. }
  337. private void HandleConnectFail()
  338. {
  339. if (selectedDeviceId != null) BleApi.Disconnect(selectedDeviceId);
  340. bool isLockBefore = isConnectLocking;
  341. ReinitAfterConnectFail();
  342. if (isLockBefore) OnConnectionFailed?.Invoke();
  343. }
  344. private void ReinitAfterConnectFail()
  345. {
  346. isConnectLocking = false;
  347. isScanningDevices = false;
  348. isScanningServices = false;
  349. isScanningCharacteristics = false;
  350. isSubscribed = false;
  351. isSubscribing = false;
  352. selectedDeviceId = null;
  353. deviceList.Clear();
  354. serviceList.Clear();
  355. characteristicsList.Clear();
  356. }
  357. private bool Write(string text)
  358. {
  359. if (!isSubscribed) return false;
  360. byte[] payload = Encoding.ASCII.GetBytes(text);
  361. BleApi.BLEData data = new BleApi.BLEData();
  362. data.buf = new byte[512];
  363. data.size = (short)payload.Length;
  364. data.deviceId = selectedDeviceId;
  365. data.serviceUuid = targetService;
  366. data.characteristicUuid = targetCharacteristicsWrite;
  367. for (int i = 0; i < payload.Length; i++)
  368. data.buf[i] = payload[i];
  369. // no error code available in non-blocking mode
  370. BleApi.SendData(in data, false);
  371. Log("Write(" + text + ")");
  372. return true;
  373. }
  374. private bool WriteByte(byte[] payload)
  375. {
  376. if (!isSubscribed) return false;
  377. BleApi.BLEData data = new BleApi.BLEData();
  378. data.buf = new byte[512];
  379. data.size = (short)payload.Length;
  380. data.deviceId = selectedDeviceId;
  381. data.serviceUuid = targetService;
  382. data.characteristicUuid = targetCharacteristicsWrite;
  383. for (int i = 0; i < payload.Length; i++)
  384. data.buf[i] = payload[i];
  385. // no error code available in non-blocking mode
  386. BleApi.SendData(in data, false);
  387. Log("Write(byte[])");
  388. return true;
  389. }
  390. private bool Disconnect()
  391. {
  392. if (!isConnectLocking)
  393. {
  394. Warn("Disconnect Invalid, because not in connect.");
  395. return false;
  396. }
  397. if (isSubscribing)
  398. {
  399. Warn("Disconnect Invalid, because is subscribing.");
  400. return false;
  401. }
  402. if (isScanningDevices) StopDeviceScan();
  403. if (selectedDeviceId != null) BleApi.Disconnect(selectedDeviceId);
  404. ReinitAfterConnectFail();
  405. Log("Disconnect OK");
  406. return true;
  407. }
  408. }
  409. }