BleWinHelper.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. string _filters = string.IsNullOrEmpty(smartBowHelper.GetFilters())
  147. ? bluetoothAim.deviceConfig.deviceName
  148. : smartBowHelper.GetFilters();
  149. //如果有定制执行定制
  150. Log($"发现设备{res.name},is fileters empty:{ string.IsNullOrEmpty(smartBowHelper.GetFilters())},current filters:{smartBowHelper.GetFilters()}");
  151. }
  152. else
  153. {
  154. // 统一格式化:去掉非十六进制字符,转大写
  155. string FormatMac(string mac) =>
  156. string.Concat(mac.Where(c => Uri.IsHexDigit(c))).ToUpper();
  157. string searchMac = FormatMac(ExtractMacFromDeviceId(res.id));
  158. string connectMac = FormatMac(smartBowHelper.GetConnectMacStr());
  159. Log($"发现设备 {searchMac},is connectMacStr:{connectMac },DeviceAddress:{searchMac}");
  160. }
  161. }
  162. if (res.nameUpdated)
  163. deviceList[res.id]["name"] = res.name;
  164. if (res.isConnectableUpdated)
  165. deviceList[res.id]["isConnectable"] = res.isConnectable.ToString();
  166. //这里参考手机蓝牙模式
  167. if (smartBowHelper.GetIsConnectName())
  168. {
  169. //后续匹配名字 可以是多个设备
  170. //Log( $"发现设备{res.name},is fileters empty:{ string.IsNullOrEmpty(smartBowHelper.GetFilters())},name:{smartBowHelper.GetFilters()}");
  171. string _filters = string.IsNullOrEmpty(smartBowHelper.GetFilters())
  172. ? bluetoothAim.deviceConfig.deviceName
  173. : smartBowHelper.GetFilters();
  174. string[] filterArray = _filters.Split('|'); // 支持多个名字
  175. foreach (var f in filterArray)
  176. {
  177. string trimmedFilter = f.Trim();
  178. if (string.Equals(trimmedFilter, deviceList[res.id]["name"].Trim(), StringComparison.OrdinalIgnoreCase)
  179. && res.isConnectable)
  180. {
  181. selectedDeviceId = res.id;
  182. StopDeviceScan();
  183. Log($"匹配设备名:{trimmedFilter}");
  184. break;
  185. }
  186. }
  187. }
  188. else
  189. {
  190. // 统一格式化:去掉非十六进制字符,转大写
  191. string FormatMac(string mac) =>
  192. string.Concat(mac.Where(c => Uri.IsHexDigit(c))).ToUpper();
  193. string searchMac = FormatMac(ExtractMacFromDeviceId(res.id));
  194. string connectMac = FormatMac(smartBowHelper.GetConnectMacStr());
  195. //Log($"发现设备 {searchMac},is connectMacStr:{connectMac },DeviceAddress:{searchMac}");
  196. //按mac地址匹配
  197. if (!string.IsNullOrEmpty(connectMac) && connectMac.Contains(searchMac) && res.isConnectable)
  198. {
  199. selectedDeviceId = res.id;
  200. StopDeviceScan();
  201. Log($"匹配设备 MAC:{searchMac}");
  202. }
  203. }
  204. }
  205. else if (status == BleApi.ScanStatus.FINISHED)
  206. {
  207. StartCoroutine(ScanServiceAndCharacteristicsToSubscribe());
  208. Log($" ScanStatus FINISHED !");
  209. //停止扫描后通知?
  210. OnScanEnded?.Invoke(selectedDeviceId != null);
  211. }
  212. } while (status == BleApi.ScanStatus.AVAILABLE);
  213. }
  214. if (isScanningServices)
  215. {
  216. BleApi.Service res;
  217. do
  218. {
  219. status = BleApi.PollService(out res, false);
  220. if (status == BleApi.ScanStatus.AVAILABLE)
  221. {
  222. serviceList.Add(res.uuid);
  223. }
  224. else if (status == BleApi.ScanStatus.FINISHED)
  225. {
  226. isScanningServices = false;
  227. }
  228. } while (status == BleApi.ScanStatus.AVAILABLE);
  229. }
  230. if (isScanningCharacteristics)
  231. {
  232. BleApi.Characteristic res;
  233. do
  234. {
  235. status = BleApi.PollCharacteristic(out res, false);
  236. if (status == BleApi.ScanStatus.AVAILABLE)
  237. {
  238. // Log("res.userDescription:"+ res.userDescription+ ",res.uuid:" + res.uuid);
  239. if (res.userDescription == "no description available" || //旧设备
  240. res.userDescription == "SPP Write Channel" || //新设备
  241. res.userDescription == "SPP Read Channel")
  242. {
  243. characteristicsList.Add(res.uuid);
  244. }
  245. else {
  246. //跟着之前代码
  247. characteristicsList.Add(res.userDescription);
  248. }
  249. //string name = res.userDescription != "no description available" ? res.userDescription : res.uuid;
  250. //characteristicsList.Add(name);
  251. }
  252. else if (status == BleApi.ScanStatus.FINISHED)
  253. {
  254. isScanningCharacteristics = false;
  255. }
  256. } while (status == BleApi.ScanStatus.AVAILABLE);
  257. }
  258. if (isSubscribed)
  259. {
  260. BleApi.BLEData res;
  261. while (BleApi.PollData(out res, false))
  262. {
  263. //string text = BitConverter.ToString(res.buf, 0, res.size);
  264. // string text = Encoding.ASCII.GetString(res.buf, 0, res.size);
  265. byte[] bytes = new byte[res.size];
  266. Array.Copy(res.buf, bytes, res.size);
  267. _receiveDataTime = Time.realtimeSinceStartup;
  268. OnCharacteristicChanged?.Invoke(res.deviceId, bytes);
  269. }
  270. }
  271. {
  272. BleApi.ErrorMessage res;
  273. BleApi.GetError(out res);
  274. if (lastError != res.msg)
  275. {
  276. Error(res.msg);
  277. lastError = res.msg;
  278. //对应设备才断开
  279. if (lastError.Contains("SendDataAsync") && lastError.Contains(selectedDeviceId) && isSubscribed)
  280. {
  281. HandleConnectFail();
  282. }
  283. }
  284. }
  285. }
  286. void LateUpdate()
  287. {
  288. if (
  289. _heartBeatInterval > 0 &&
  290. isSubscribed &&
  291. Time.realtimeSinceStartup - _connectedTime >= _heartBeatInterval &&
  292. Time.realtimeSinceStartup - _receiveDataTime >= _heartBeatInterval
  293. )
  294. {
  295. HandleConnectFail();
  296. }
  297. }
  298. private bool Connect()
  299. {
  300. if (isConnectLocking || isScanningDevices)
  301. {
  302. Warn("Connect Invalid, because is in connect.");
  303. return false;
  304. }
  305. //开始连接时候,重置一下参数
  306. ReinitAfterConnectFail();
  307. BleApi.StartDeviceScan();
  308. isConnectLocking = true;
  309. isScanningDevices = true;
  310. Log("Start Connect!");
  311. return true;
  312. }
  313. private void StopDeviceScan()
  314. {
  315. if (!isScanningDevices) return;
  316. BleApi.StopDeviceScan();
  317. isScanningDevices = false;
  318. Log($"Stop DeviceScan!");
  319. }
  320. private IEnumerator ScanServiceAndCharacteristicsToSubscribe()
  321. {
  322. isSubscribing = true;
  323. if (selectedDeviceId == null)
  324. {
  325. HandleConnectFail();
  326. smartBowHelper.InvokeOnBluetoothError(BluetoothError.Unknown, "选择设备 DeviceId 不存在!");
  327. yield break;
  328. }
  329. Log("SelectedDeviceId OK");
  330. BleApi.ScanServices(selectedDeviceId);
  331. isScanningServices = true;
  332. serviceList.Clear();
  333. while (isScanningServices) yield return null;
  334. bool findTargetService = false;
  335. foreach (string service in serviceList)
  336. {
  337. if (service == targetService)
  338. {
  339. findTargetService = true;
  340. Log("FindTargetService OK");
  341. break;
  342. }
  343. }
  344. if (!findTargetService)
  345. {
  346. HandleConnectFail();
  347. //一般都不会出现这个问题,直接使用Unknown
  348. smartBowHelper.InvokeOnBluetoothError(BluetoothError.Unknown, "目标设备服务 service 不存在!");
  349. yield break;
  350. }
  351. BleApi.ScanCharacteristics(selectedDeviceId, targetService);
  352. isScanningCharacteristics = true;
  353. characteristicsList.Clear();
  354. while (isScanningCharacteristics) yield return null;
  355. bool findTargetCharacteristicsNotify = false;
  356. bool findTargetCharacteristicsWrite = false;
  357. foreach (string characteristics in characteristicsList)
  358. {
  359. if (characteristics == targetCharacteristicsNotify)
  360. {
  361. findTargetCharacteristicsNotify = true;
  362. Log("FindTargetCharacteristicsNotify OK");
  363. }
  364. else if (characteristics == targetCharacteristicsWrite)
  365. {
  366. findTargetCharacteristicsWrite = true;
  367. Log("FindTargetCharacteristicsWrite OK");
  368. }
  369. }
  370. if (!findTargetCharacteristicsNotify || !findTargetCharacteristicsWrite)
  371. {
  372. HandleConnectFail();
  373. //一般都不会出现这个问题,直接使用Unknown
  374. smartBowHelper.InvokeOnBluetoothError(BluetoothError.Unknown, "目标设备特征值 Characteristics 不存在!");
  375. yield break;
  376. }
  377. BleApi.SubscribeCharacteristic(selectedDeviceId, targetService, targetCharacteristicsNotify, false);
  378. isSubscribed = true;
  379. isSubscribing = false;
  380. Log("SubscribeCharacteristicNotify OK");
  381. _connectedTime = Time.realtimeSinceStartup;
  382. OnConnected?.Invoke();
  383. }
  384. /// <summary>
  385. /// 连接错误
  386. /// </summary>
  387. private void HandleConnectFail()
  388. {
  389. if (selectedDeviceId != null) BleApi.Disconnect(selectedDeviceId);
  390. bool isLockBefore = isConnectLocking;
  391. ReinitAfterConnectFail();
  392. if (isLockBefore) OnConnectionFailed?.Invoke();
  393. }
  394. private void ReinitAfterConnectFail()
  395. {
  396. isConnectLocking = false;
  397. isScanningDevices = false;
  398. isScanningServices = false;
  399. isScanningCharacteristics = false;
  400. isSubscribed = false;
  401. isSubscribing = false;
  402. selectedDeviceId = null;
  403. deviceList.Clear();
  404. serviceList.Clear();
  405. characteristicsList.Clear();
  406. }
  407. private bool Write(string text)
  408. {
  409. if (!isSubscribed) return false;
  410. byte[] payload = Encoding.ASCII.GetBytes(text);
  411. BleApi.BLEData data = new BleApi.BLEData();
  412. data.buf = new byte[512];
  413. data.size = (short)payload.Length;
  414. data.deviceId = selectedDeviceId;
  415. data.serviceUuid = targetService;
  416. data.characteristicUuid = targetCharacteristicsWrite;
  417. for (int i = 0; i < payload.Length; i++)
  418. data.buf[i] = payload[i];
  419. // no error code available in non-blocking mode
  420. BleApi.SendData(in data, false);
  421. //Log("Write(" + text + ")");
  422. return true;
  423. }
  424. private bool WriteByte(byte[] payload)
  425. {
  426. if (!isSubscribed) return false;
  427. BleApi.BLEData data = new BleApi.BLEData();
  428. data.buf = new byte[512];
  429. data.size = (short)payload.Length;
  430. data.deviceId = selectedDeviceId;
  431. data.serviceUuid = targetService;
  432. data.characteristicUuid = targetCharacteristicsWrite;
  433. for (int i = 0; i < payload.Length; i++)
  434. data.buf[i] = payload[i];
  435. // no error code available in non-blocking mode
  436. BleApi.SendData(in data, false);
  437. Log("Write(byte[])");
  438. return true;
  439. }
  440. private bool Disconnect()
  441. {
  442. if (!isConnectLocking)
  443. {
  444. Warn("Disconnect Invalid, because not in connect.");
  445. return false;
  446. }
  447. if (isSubscribing)
  448. {
  449. Warn("Disconnect Invalid, because is subscribing.");
  450. return false;
  451. }
  452. if (isScanningDevices) StopDeviceScan();
  453. if (selectedDeviceId != null) BleApi.Disconnect(selectedDeviceId);
  454. ReinitAfterConnectFail();
  455. Log("Disconnect OK");
  456. return true;
  457. }
  458. /// <summary>
  459. /// 其实是 WinRT BLE API 的设备 ID 格式,它包含两部分:
  460. /// 前面:BluetoothLE#BluetoothLE00:e0:4c:2a:12:97 → 设备的内部标识(适配器 + 随机 ID)。
  461. /// 后面:d3:5c:89:57:68:de → 真实的 MAC 地址。
  462. /// 微软官方文档也说明了: WinRT 不直接给 BluetoothAddress,而是拼接在 Id 的末尾。
  463. /// </summary>
  464. /// <param name="deviceId">参考 BluetoothLE#BluetoothLE00:e0:4c:2a:12:97-d3:5c:89:57:68:de</param>
  465. /// <returns></returns>
  466. string ExtractMacFromDeviceId(string deviceId)
  467. {
  468. if (string.IsNullOrEmpty(deviceId))
  469. return null;
  470. // WinRT 格式一般是:BluetoothLE#BluetoothLEXX:XX:XX:XX:XX:XX-YY:YY:YY:YY:YY:YY
  471. int lastDash = deviceId.LastIndexOf('-');
  472. if (lastDash >= 0 && lastDash < deviceId.Length - 1)
  473. {
  474. return deviceId.Substring(lastDash + 1); // 提取最后一段 MAC
  475. }
  476. return deviceId;
  477. }
  478. }
  479. }