BleWinHelper.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. using System.Linq;
  8. namespace SmartBowSDK
  9. {
  10. /// <summary>
  11. /// Windows连接BluetoothLE
  12. /// 我的扫描逻辑默认了读写特征都在同一服务下
  13. /// </summary>
  14. public class BleWinHelper : MonoBehaviour
  15. {
  16. public string LogTag = "[BleWinHelper]";
  17. private void Log(string text)
  18. {
  19. //Debug.Log(LogTag + text);
  20. SmartBowLogger.Log(this, LogTag + text);
  21. }
  22. private void Warn(string text)
  23. {
  24. //Debug.LogWarning(LogTag + text);
  25. SmartBowLogger.Log(this, LogTag + text);
  26. }
  27. private void Error(string text)
  28. {
  29. //Debug.Log(LogTag + text);
  30. SmartBowLogger.Log(this, LogTag + text);
  31. }
  32. //private string targetDeviceName = "Bbow_20210501 | ARTEMIS Pro | HOUYI Pro | Pistol | Pistol M9 | BGBox_202012";
  33. //private string targetDeviceNameHOUYIPro = "HOUYI Pro";
  34. //private string targetDeviceNameGun = "Pistol | Pistol M9 | Bbow_20210501";
  35. private string targetService = "{0000fff0-0000-1000-8000-00805f9b34fb}";
  36. private string targetCharacteristicsNotify = "{0000fff1-0000-1000-8000-00805f9b34fb}";
  37. private string targetCharacteristicsWrite = "{0000fff2-0000-1000-8000-00805f9b34fb}";
  38. private bool isConnectLocking = false;
  39. private bool isScanningDevices = false;
  40. private bool isScanningServices = false;
  41. private bool isScanningCharacteristics = false;
  42. private bool isSubscribed = false;
  43. private bool isSubscribing = false;
  44. private string lastError = null;
  45. [SerializeField]
  46. private string selectedDeviceId = null;
  47. private Dictionary<string, Dictionary<string, string>> deviceList = new Dictionary<string, Dictionary<string, string>>();
  48. private List<string> serviceList = new List<string>();
  49. private List<string> characteristicsList = new List<string>();
  50. private float _connectedTime = 0;
  51. private float _receiveDataTime = 0;
  52. private float _heartBeatInterval = 0;
  53. private Action<bool> OnScanEnded;
  54. private Action OnConnected;
  55. /// <summary>
  56. /// 主动调用Disconnect()不会触发该委托
  57. /// </summary>
  58. private Action OnConnectionFailed;
  59. private static Action<string, byte[]> OnCharacteristicChanged;
  60. public SmartBowHelper smartBowHelper;
  61. public BluetoothAim_SDK bluetoothAim;
  62. /// <summary>
  63. /// 注册window对象
  64. /// </summary>
  65. /// <param name="o">挂载对象</param>
  66. /// <param name="bluetoothWindows">关联的BluetoothWindows</param>
  67. /// <param name="logTip">提示的log标签</param>
  68. /// <returns></returns>
  69. public static BleWinHelper RegisterTo(SmartBowHelper _smartBowHelper, BluetoothWindows bluetoothWindows,string logTip = "first")
  70. {
  71. //if (_Instance)
  72. //{
  73. // Error("Register fail, because only one can be registered.");
  74. // return null;
  75. //}
  76. string bleWinName = "BleWinHelper" + logTip;
  77. GameObject obj = new GameObject(bleWinName);
  78. obj.transform.SetParent(_smartBowHelper.transform);
  79. BleWinHelper bleWinHelper = obj.AddComponent<BleWinHelper>();
  80. bleWinHelper.smartBowHelper = _smartBowHelper;
  81. bleWinHelper.bluetoothAim = _smartBowHelper.bluetoothAim;
  82. //日志名字
  83. bleWinHelper.LogTag = logTip + ": ";
  84. bluetoothWindows.Connect = bleWinHelper.Connect;
  85. bluetoothWindows.Disconnect = bleWinHelper.Disconnect;
  86. bluetoothWindows.Write = bleWinHelper.Write;
  87. bluetoothWindows.WriteByte = bleWinHelper.WriteByte;
  88. // windos 通知 bluetoothAim
  89. bleWinHelper.OnScanEnded = (bool bSelectedDeviceId) => bluetoothWindows.OnScanEnded?.Invoke(bSelectedDeviceId);
  90. bleWinHelper.OnConnected = () => bluetoothWindows.OnConnected?.Invoke();
  91. bleWinHelper.OnConnectionFailed = () => bluetoothWindows.OnConnectionFailed?.Invoke();
  92. //多个定义共用一个OnCharacteristicChanged
  93. OnCharacteristicChanged += (deviceID,bytes) => {
  94. if (deviceID == bleWinHelper.selectedDeviceId) {
  95. bluetoothWindows.OnCharacteristicChanged?.Invoke(deviceID, bytes);
  96. }
  97. };
  98. return bleWinHelper;
  99. }
  100. /// <summary>
  101. /// 设置心跳检测
  102. /// 1.每次收到的蓝牙数据都视为心跳
  103. /// 2.帮助触发蓝牙断开监听
  104. /// </summary>
  105. /// <param name="interval">心跳检测间隔</param>
  106. public void SetHeartBeat(float interval)
  107. {
  108. _heartBeatInterval = interval;
  109. }
  110. //private static BleWinHelper _Instance;
  111. void Awake()
  112. {
  113. // _Instance = this;
  114. }
  115. void OnDestroy()
  116. {
  117. // if (_Instance == this) _Instance = null;
  118. BleApi.Quit();
  119. }
  120. void Update()
  121. {
  122. BleApi.ScanStatus status;
  123. if (isScanningDevices)
  124. {
  125. BleApi.DeviceUpdate res = new BleApi.DeviceUpdate();
  126. do
  127. {
  128. status = BleApi.PollDevice(ref res, false);
  129. if (status == BleApi.ScanStatus.AVAILABLE)
  130. {
  131. // 如果存在 selectedDeviceId,不要重复
  132. if (selectedDeviceId != null) {
  133. //Log($"当前的 selectedDeviceId: {selectedDeviceId}");
  134. continue; // 跳过后续 AVAILABLE 的处理,等待 FINISHED
  135. }
  136. if (!deviceList.ContainsKey(res.id))
  137. {
  138. deviceList[res.id] = new Dictionary<string, string>() {
  139. { "name", "" },
  140. { "isConnectable", "False" }
  141. };
  142. //这里参考手机蓝牙模式
  143. if (smartBowHelper.GetIsConnectName())
  144. {
  145. //后续匹配名字 可以是多个设备
  146. Log($"发现设备{res.name},is fileters empty:{ string.IsNullOrEmpty(smartBowHelper.GetFilters())},name:{smartBowHelper.GetFilters()}");
  147. }
  148. else
  149. {
  150. // 统一格式化:去掉非十六进制字符,转大写
  151. string FormatMac(string mac) =>
  152. string.Concat(mac.Where(c => Uri.IsHexDigit(c))).ToUpper();
  153. string searchMac = FormatMac(ExtractMacFromDeviceId(res.id));
  154. string connectMac = FormatMac(smartBowHelper.GetConnectMacStr());
  155. Log($"发现设备 {searchMac},is connectMacStr:{connectMac },DeviceAddress:{searchMac}");
  156. }
  157. }
  158. if (res.nameUpdated)
  159. deviceList[res.id]["name"] = res.name;
  160. if (res.isConnectableUpdated)
  161. deviceList[res.id]["isConnectable"] = res.isConnectable.ToString();
  162. //这里参考手机蓝牙模式
  163. if (smartBowHelper.GetIsConnectName())
  164. {
  165. //后续匹配名字 可以是多个设备
  166. //Log( $"发现设备{res.name},is fileters empty:{ string.IsNullOrEmpty(smartBowHelper.GetFilters())},name:{smartBowHelper.GetFilters()}");
  167. string _filters = string.IsNullOrEmpty(smartBowHelper.GetFilters())
  168. ? bluetoothAim.deviceConfig.deviceName
  169. : smartBowHelper.GetFilters();
  170. string[] filterArray = _filters.Split('|'); // 支持多个名字
  171. foreach (var f in filterArray)
  172. {
  173. string trimmedFilter = f.Trim();
  174. if (string.Equals(trimmedFilter, deviceList[res.id]["name"].Trim(), StringComparison.OrdinalIgnoreCase)
  175. && res.isConnectable)
  176. {
  177. selectedDeviceId = res.id;
  178. StopDeviceScan();
  179. Log($"Name匹配设备 {trimmedFilter}");
  180. break;
  181. }
  182. }
  183. }
  184. else
  185. {
  186. // 统一格式化:去掉非十六进制字符,转大写
  187. string FormatMac(string mac) =>
  188. string.Concat(mac.Where(c => Uri.IsHexDigit(c))).ToUpper();
  189. string searchMac = FormatMac(ExtractMacFromDeviceId(res.id));
  190. string connectMac = FormatMac(smartBowHelper.GetConnectMacStr());
  191. //Log($"发现设备 {searchMac},is connectMacStr:{connectMac },DeviceAddress:{searchMac}");
  192. //按mac地址匹配
  193. if (!string.IsNullOrEmpty(connectMac) && connectMac.Contains(searchMac) && res.isConnectable)
  194. {
  195. selectedDeviceId = res.id;
  196. StopDeviceScan();
  197. Log($"MAC匹配设备 {searchMac}");
  198. }
  199. }
  200. }
  201. else if (status == BleApi.ScanStatus.FINISHED)
  202. {
  203. StartCoroutine(ScanServiceAndCharacteristicsToSubscribe());
  204. Log($" ScanStatus FINISHED !");
  205. //停止扫描后通知?
  206. OnScanEnded?.Invoke(selectedDeviceId != null);
  207. }
  208. } while (status == BleApi.ScanStatus.AVAILABLE);
  209. }
  210. if (isScanningServices)
  211. {
  212. BleApi.Service res;
  213. do
  214. {
  215. status = BleApi.PollService(out res, false);
  216. if (status == BleApi.ScanStatus.AVAILABLE)
  217. {
  218. serviceList.Add(res.uuid);
  219. }
  220. else if (status == BleApi.ScanStatus.FINISHED)
  221. {
  222. isScanningServices = false;
  223. }
  224. } while (status == BleApi.ScanStatus.AVAILABLE);
  225. }
  226. if (isScanningCharacteristics)
  227. {
  228. BleApi.Characteristic res;
  229. do
  230. {
  231. status = BleApi.PollCharacteristic(out res, false);
  232. if (status == BleApi.ScanStatus.AVAILABLE)
  233. {
  234. Log("res.userDescription:"+ res.userDescription+ ",res.uuid:" + res.uuid);
  235. if (res.userDescription == "no description available" || //旧设备
  236. res.userDescription == "SPP Write Channel" || //新设备
  237. res.userDescription == "SPP Read Channel")
  238. {
  239. characteristicsList.Add(res.uuid);
  240. }
  241. else {
  242. //跟着之前代码
  243. characteristicsList.Add(res.userDescription);
  244. }
  245. //string name = res.userDescription != "no description available" ? res.userDescription : res.uuid;
  246. //characteristicsList.Add(name);
  247. }
  248. else if (status == BleApi.ScanStatus.FINISHED)
  249. {
  250. isScanningCharacteristics = false;
  251. }
  252. } while (status == BleApi.ScanStatus.AVAILABLE);
  253. }
  254. if (isSubscribed)
  255. {
  256. BleApi.BLEData res;
  257. while (BleApi.PollData(out res, false))
  258. {
  259. //string text = BitConverter.ToString(res.buf, 0, res.size);
  260. // string text = Encoding.ASCII.GetString(res.buf, 0, res.size);
  261. byte[] bytes = new byte[res.size];
  262. Array.Copy(res.buf, bytes, res.size);
  263. _receiveDataTime = Time.realtimeSinceStartup;
  264. OnCharacteristicChanged?.Invoke(res.deviceId, bytes);
  265. }
  266. }
  267. {
  268. BleApi.ErrorMessage res;
  269. BleApi.GetError(out res);
  270. if (lastError != res.msg)
  271. {
  272. Error(res.msg);
  273. lastError = res.msg;
  274. //对应设备才断开
  275. if (lastError.Contains("SendDataAsync") && lastError.Contains(selectedDeviceId) && isSubscribed)
  276. {
  277. HandleConnectFail();
  278. }
  279. }
  280. }
  281. }
  282. void LateUpdate()
  283. {
  284. if (
  285. _heartBeatInterval > 0 &&
  286. isSubscribed &&
  287. Time.realtimeSinceStartup - _connectedTime >= _heartBeatInterval &&
  288. Time.realtimeSinceStartup - _receiveDataTime >= _heartBeatInterval
  289. )
  290. {
  291. HandleConnectFail();
  292. }
  293. }
  294. private bool Connect()
  295. {
  296. if (isConnectLocking || isScanningDevices)
  297. {
  298. Warn("Connect Invalid, because is in connect.");
  299. return false;
  300. }
  301. //开始连接时候,重置一下参数
  302. ReinitAfterConnectFail();
  303. BleApi.StartDeviceScan();
  304. isConnectLocking = true;
  305. isScanningDevices = true;
  306. Log("Start Connect!");
  307. return true;
  308. }
  309. private void StopDeviceScan()
  310. {
  311. if (!isScanningDevices) return;
  312. BleApi.StopDeviceScan();
  313. isScanningDevices = false;
  314. Log($"Stop DeviceScan!");
  315. }
  316. private IEnumerator ScanServiceAndCharacteristicsToSubscribe()
  317. {
  318. isSubscribing = true;
  319. if (selectedDeviceId == null)
  320. {
  321. HandleConnectFail();
  322. smartBowHelper.InvokeOnBluetoothError(BluetoothError.Unknown, "选择设备 DeviceId 不存在!");
  323. yield break;
  324. }
  325. Log("SelectedDeviceId OK");
  326. BleApi.ScanServices(selectedDeviceId);
  327. isScanningServices = true;
  328. serviceList.Clear();
  329. while (isScanningServices) yield return null;
  330. bool findTargetService = false;
  331. foreach (string service in serviceList)
  332. {
  333. if (service == targetService)
  334. {
  335. findTargetService = true;
  336. Log("FindTargetService OK");
  337. break;
  338. }
  339. }
  340. if (!findTargetService)
  341. {
  342. HandleConnectFail();
  343. //一般都不会出现这个问题,直接使用Unknown
  344. smartBowHelper.InvokeOnBluetoothError(BluetoothError.Unknown, "目标设备服务 service 不存在!");
  345. yield break;
  346. }
  347. BleApi.ScanCharacteristics(selectedDeviceId, targetService);
  348. isScanningCharacteristics = true;
  349. characteristicsList.Clear();
  350. while (isScanningCharacteristics) yield return null;
  351. bool findTargetCharacteristicsNotify = false;
  352. bool findTargetCharacteristicsWrite = false;
  353. foreach (string characteristics in characteristicsList)
  354. {
  355. if (characteristics == targetCharacteristicsNotify)
  356. {
  357. findTargetCharacteristicsNotify = true;
  358. Log("FindTargetCharacteristicsNotify OK");
  359. }
  360. else if (characteristics == targetCharacteristicsWrite)
  361. {
  362. findTargetCharacteristicsWrite = true;
  363. Log("FindTargetCharacteristicsWrite OK");
  364. }
  365. }
  366. if (!findTargetCharacteristicsNotify || !findTargetCharacteristicsWrite)
  367. {
  368. HandleConnectFail();
  369. //一般都不会出现这个问题,直接使用Unknown
  370. smartBowHelper.InvokeOnBluetoothError(BluetoothError.Unknown, "目标设备特征值 Characteristics 不存在!");
  371. yield break;
  372. }
  373. BleApi.SubscribeCharacteristic(selectedDeviceId, targetService, targetCharacteristicsNotify, false);
  374. isSubscribed = true;
  375. isSubscribing = false;
  376. Log("SubscribeCharacteristicNotify OK");
  377. _connectedTime = Time.realtimeSinceStartup;
  378. OnConnected?.Invoke();
  379. }
  380. /// <summary>
  381. /// 连接错误
  382. /// </summary>
  383. private void HandleConnectFail()
  384. {
  385. if (selectedDeviceId != null) BleApi.Disconnect(selectedDeviceId);
  386. bool isLockBefore = isConnectLocking;
  387. ReinitAfterConnectFail();
  388. if (isLockBefore) OnConnectionFailed?.Invoke();
  389. }
  390. private void ReinitAfterConnectFail()
  391. {
  392. isConnectLocking = false;
  393. isScanningDevices = false;
  394. isScanningServices = false;
  395. isScanningCharacteristics = false;
  396. isSubscribed = false;
  397. isSubscribing = false;
  398. selectedDeviceId = null;
  399. deviceList.Clear();
  400. serviceList.Clear();
  401. characteristicsList.Clear();
  402. }
  403. private bool Write(string text)
  404. {
  405. if (!isSubscribed) return false;
  406. byte[] payload = Encoding.ASCII.GetBytes(text);
  407. BleApi.BLEData data = new BleApi.BLEData();
  408. data.buf = new byte[512];
  409. data.size = (short)payload.Length;
  410. data.deviceId = selectedDeviceId;
  411. data.serviceUuid = targetService;
  412. data.characteristicUuid = targetCharacteristicsWrite;
  413. for (int i = 0; i < payload.Length; i++)
  414. data.buf[i] = payload[i];
  415. // no error code available in non-blocking mode
  416. BleApi.SendData(in data, false);
  417. Log("Write(" + text + ")");
  418. return true;
  419. }
  420. private bool WriteByte(byte[] payload)
  421. {
  422. if (!isSubscribed) return false;
  423. BleApi.BLEData data = new BleApi.BLEData();
  424. data.buf = new byte[512];
  425. data.size = (short)payload.Length;
  426. data.deviceId = selectedDeviceId;
  427. data.serviceUuid = targetService;
  428. data.characteristicUuid = targetCharacteristicsWrite;
  429. for (int i = 0; i < payload.Length; i++)
  430. data.buf[i] = payload[i];
  431. // no error code available in non-blocking mode
  432. BleApi.SendData(in data, false);
  433. Log("Write(byte[])");
  434. return true;
  435. }
  436. private bool Disconnect()
  437. {
  438. if (!isConnectLocking)
  439. {
  440. Warn("Disconnect Invalid, because not in connect.");
  441. return false;
  442. }
  443. if (isSubscribing)
  444. {
  445. Warn("Disconnect Invalid, because is subscribing.");
  446. return false;
  447. }
  448. if (isScanningDevices) StopDeviceScan();
  449. if (selectedDeviceId != null) BleApi.Disconnect(selectedDeviceId);
  450. ReinitAfterConnectFail();
  451. Log("Disconnect OK");
  452. return true;
  453. }
  454. /// <summary>
  455. /// 其实是 WinRT BLE API 的设备 ID 格式,它包含两部分:
  456. /// 前面:BluetoothLE#BluetoothLE00:e0:4c:2a:12:97 → 设备的内部标识(适配器 + 随机 ID)。
  457. /// 后面:d3:5c:89:57:68:de → 真实的 MAC 地址。
  458. /// 微软官方文档也说明了: WinRT 不直接给 BluetoothAddress,而是拼接在 Id 的末尾。
  459. /// </summary>
  460. /// <param name="deviceId">参考 BluetoothLE#BluetoothLE00:e0:4c:2a:12:97-d3:5c:89:57:68:de</param>
  461. /// <returns></returns>
  462. string ExtractMacFromDeviceId(string deviceId)
  463. {
  464. if (string.IsNullOrEmpty(deviceId))
  465. return null;
  466. // WinRT 格式一般是:BluetoothLE#BluetoothLEXX:XX:XX:XX:XX:XX-YY:YY:YY:YY:YY:YY
  467. int lastDash = deviceId.LastIndexOf('-');
  468. if (lastDash >= 0 && lastDash < deviceId.Length - 1)
  469. {
  470. return deviceId.Substring(lastDash + 1); // 提取最后一段 MAC
  471. }
  472. return deviceId;
  473. }
  474. }
  475. }