BleWinHelper.cs 14 KB

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