BleWinHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 | Pistol M9";
  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. bluetoothWindows.WriteByte = bleWinHelper.WriteByte;
  76. bleWinHelper.OnConnected = () => bluetoothWindows.OnConnected?.Invoke();
  77. bleWinHelper.OnConnectionFailed = () => bluetoothWindows.OnConnectionFailed?.Invoke();
  78. //多个定义共用一个OnCharacteristicChanged
  79. OnCharacteristicChanged += (deviceID,bytes) => {
  80. if (deviceID == bleWinHelper.selectedDeviceId) {
  81. bluetoothWindows.OnCharacteristicChanged?.Invoke(deviceID, bytes);
  82. }
  83. };
  84. return bleWinHelper;
  85. }
  86. /// <summary>
  87. /// 设置心跳检测
  88. /// 1.每次收到的蓝牙数据都视为心跳
  89. /// 2.帮助触发蓝牙断开监听
  90. /// </summary>
  91. /// <param name="interval">心跳检测间隔</param>
  92. public void SetHeartBeat(float interval)
  93. {
  94. _heartBeatInterval = interval;
  95. }
  96. //private static BleWinHelper _Instance;
  97. void Awake()
  98. {
  99. // _Instance = this;
  100. }
  101. void OnDestroy()
  102. {
  103. // if (_Instance == this) _Instance = null;
  104. BleApi.Quit();
  105. }
  106. void Update()
  107. {
  108. BleApi.ScanStatus status;
  109. if (isScanningDevices)
  110. {
  111. BleApi.DeviceUpdate res = new BleApi.DeviceUpdate();
  112. do
  113. {
  114. status = BleApi.PollDevice(ref res, false);
  115. if (status == BleApi.ScanStatus.AVAILABLE)
  116. {
  117. if (!deviceList.ContainsKey(res.id))
  118. deviceList[res.id] = new Dictionary<string, string>() {
  119. { "name", "" },
  120. { "isConnectable", "False" }
  121. };
  122. if (res.nameUpdated)
  123. deviceList[res.id]["name"] = res.name;
  124. if (res.isConnectableUpdated)
  125. deviceList[res.id]["isConnectable"] = res.isConnectable.ToString();
  126. //deviceList[res.id]["name"] == targetDeviceName
  127. if (targetDeviceName.Contains(deviceList[res.id]["name"]) && deviceList[res.id]["isConnectable"] == "True")
  128. {
  129. selectedDeviceId = res.id;
  130. StopDeviceScan();
  131. }
  132. }
  133. else if (status == BleApi.ScanStatus.FINISHED)
  134. {
  135. StartCoroutine(ScanServiceAndCharacteristicsToSubscribe());
  136. }
  137. } while (status == BleApi.ScanStatus.AVAILABLE);
  138. }
  139. if (isScanningServices)
  140. {
  141. BleApi.Service res;
  142. do
  143. {
  144. status = BleApi.PollService(out res, false);
  145. if (status == BleApi.ScanStatus.AVAILABLE)
  146. {
  147. serviceList.Add(res.uuid);
  148. }
  149. else if (status == BleApi.ScanStatus.FINISHED)
  150. {
  151. isScanningServices = false;
  152. }
  153. } while (status == BleApi.ScanStatus.AVAILABLE);
  154. }
  155. if (isScanningCharacteristics)
  156. {
  157. BleApi.Characteristic res;
  158. do
  159. {
  160. status = BleApi.PollCharacteristic(out res, false);
  161. if (status == BleApi.ScanStatus.AVAILABLE)
  162. {
  163. Log("res.userDescription:"+ res.userDescription+ ",res.uuid:" + res.uuid);
  164. if (res.userDescription == "no description available" || //旧设备
  165. res.userDescription == "SPP Write Channel" || //新设备
  166. res.userDescription == "SPP Read Channel")
  167. {
  168. characteristicsList.Add(res.uuid);
  169. }
  170. else {
  171. //跟着之前代码
  172. characteristicsList.Add(res.userDescription);
  173. }
  174. //string name = res.userDescription != "no description available" ? res.userDescription : res.uuid;
  175. //characteristicsList.Add(name);
  176. }
  177. else if (status == BleApi.ScanStatus.FINISHED)
  178. {
  179. isScanningCharacteristics = false;
  180. }
  181. } while (status == BleApi.ScanStatus.AVAILABLE);
  182. }
  183. if (isSubscribed)
  184. {
  185. BleApi.BLEData res;
  186. while (BleApi.PollData(out res, false))
  187. {
  188. //string text = BitConverter.ToString(res.buf, 0, res.size);
  189. // string text = Encoding.ASCII.GetString(res.buf, 0, res.size);
  190. byte[] bytes = new byte[res.size];
  191. Array.Copy(res.buf, bytes, res.size);
  192. _receiveDataTime = Time.realtimeSinceStartup;
  193. OnCharacteristicChanged?.Invoke(res.deviceId, bytes);
  194. }
  195. }
  196. {
  197. BleApi.ErrorMessage res;
  198. BleApi.GetError(out res);
  199. if (lastError != res.msg)
  200. {
  201. Error(res.msg);
  202. lastError = res.msg;
  203. //对应设备才断开
  204. if (lastError.Contains("SendDataAsync") && lastError.Contains(selectedDeviceId) && isSubscribed)
  205. {
  206. HandleConnectFail();
  207. }
  208. }
  209. }
  210. }
  211. void LateUpdate()
  212. {
  213. if (
  214. _heartBeatInterval > 0 &&
  215. isSubscribed &&
  216. Time.realtimeSinceStartup - _connectedTime >= _heartBeatInterval &&
  217. Time.realtimeSinceStartup - _receiveDataTime >= _heartBeatInterval
  218. )
  219. {
  220. HandleConnectFail();
  221. }
  222. }
  223. private bool Connect()
  224. {
  225. if (isConnectLocking || isScanningDevices)
  226. {
  227. Warn("Connect Invalid, because is in connect.");
  228. return false;
  229. }
  230. BleApi.StartDeviceScan();
  231. isConnectLocking = true;
  232. isScanningDevices = true;
  233. Log("Start Connect");
  234. return true;
  235. }
  236. private void StopDeviceScan()
  237. {
  238. if (!isScanningDevices) return;
  239. BleApi.StopDeviceScan();
  240. isScanningDevices = false;
  241. }
  242. private IEnumerator ScanServiceAndCharacteristicsToSubscribe()
  243. {
  244. isSubscribing = true;
  245. if (selectedDeviceId == null)
  246. {
  247. HandleConnectFail();
  248. yield break;
  249. }
  250. Log("SelectedDeviceId OK");
  251. BleApi.ScanServices(selectedDeviceId);
  252. isScanningServices = true;
  253. serviceList.Clear();
  254. while (isScanningServices) yield return null;
  255. bool findTargetService = false;
  256. foreach (string service in serviceList)
  257. {
  258. if (service == targetService)
  259. {
  260. findTargetService = true;
  261. Log("FindTargetService OK");
  262. break;
  263. }
  264. }
  265. if (!findTargetService)
  266. {
  267. HandleConnectFail();
  268. yield break;
  269. }
  270. BleApi.ScanCharacteristics(selectedDeviceId, targetService);
  271. isScanningCharacteristics = true;
  272. characteristicsList.Clear();
  273. while (isScanningCharacteristics) yield return null;
  274. bool findTargetCharacteristicsNotify = false;
  275. bool findTargetCharacteristicsWrite = false;
  276. foreach (string characteristics in characteristicsList)
  277. {
  278. if (characteristics == targetCharacteristicsNotify)
  279. {
  280. findTargetCharacteristicsNotify = true;
  281. Log("FindTargetCharacteristicsNotify OK");
  282. }
  283. else if (characteristics == targetCharacteristicsWrite)
  284. {
  285. findTargetCharacteristicsWrite = true;
  286. Log("FindTargetCharacteristicsWrite OK");
  287. }
  288. }
  289. if (!findTargetCharacteristicsNotify || !findTargetCharacteristicsWrite)
  290. {
  291. HandleConnectFail();
  292. yield break;
  293. }
  294. BleApi.SubscribeCharacteristic(selectedDeviceId, targetService, targetCharacteristicsNotify, false);
  295. isSubscribed = true;
  296. isSubscribing = false;
  297. Log("SubscribeCharacteristicNotify OK");
  298. _connectedTime = Time.realtimeSinceStartup;
  299. OnConnected?.Invoke();
  300. }
  301. private void HandleConnectFail()
  302. {
  303. if (selectedDeviceId != null) BleApi.Disconnect(selectedDeviceId);
  304. bool isLockBefore = isConnectLocking;
  305. ReinitAfterConnectFail();
  306. if (isLockBefore) OnConnectionFailed?.Invoke();
  307. }
  308. private void ReinitAfterConnectFail()
  309. {
  310. isConnectLocking = false;
  311. isScanningDevices = false;
  312. isScanningServices = false;
  313. isScanningCharacteristics = false;
  314. isSubscribed = false;
  315. isSubscribing = false;
  316. selectedDeviceId = null;
  317. deviceList.Clear();
  318. serviceList.Clear();
  319. characteristicsList.Clear();
  320. }
  321. private bool Write(string text)
  322. {
  323. if (!isSubscribed) return false;
  324. byte[] payload = Encoding.ASCII.GetBytes(text);
  325. BleApi.BLEData data = new BleApi.BLEData();
  326. data.buf = new byte[512];
  327. data.size = (short)payload.Length;
  328. data.deviceId = selectedDeviceId;
  329. data.serviceUuid = targetService;
  330. data.characteristicUuid = targetCharacteristicsWrite;
  331. for (int i = 0; i < payload.Length; i++)
  332. data.buf[i] = payload[i];
  333. // no error code available in non-blocking mode
  334. BleApi.SendData(in data, false);
  335. Log("Write(" + text + ")");
  336. return true;
  337. }
  338. private bool WriteByte(byte[] payload)
  339. {
  340. if (!isSubscribed) return false;
  341. BleApi.BLEData data = new BleApi.BLEData();
  342. data.buf = new byte[512];
  343. data.size = (short)payload.Length;
  344. data.deviceId = selectedDeviceId;
  345. data.serviceUuid = targetService;
  346. data.characteristicUuid = targetCharacteristicsWrite;
  347. for (int i = 0; i < payload.Length; i++)
  348. data.buf[i] = payload[i];
  349. // no error code available in non-blocking mode
  350. BleApi.SendData(in data, false);
  351. Log("Write(byte[])");
  352. return true;
  353. }
  354. private bool Disconnect()
  355. {
  356. if (!isConnectLocking)
  357. {
  358. Warn("Disconnect Invalid, because not in connect.");
  359. return false;
  360. }
  361. if (isSubscribing)
  362. {
  363. Warn("Disconnect Invalid, because is subscribing.");
  364. return false;
  365. }
  366. if (isScanningDevices) StopDeviceScan();
  367. if (selectedDeviceId != null) BleApi.Disconnect(selectedDeviceId);
  368. ReinitAfterConnectFail();
  369. Log("Disconnect OK");
  370. return true;
  371. }
  372. }
  373. }