diff --git a/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs b/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs index df340422..558dd132 100644 --- a/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs +++ b/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs @@ -36,7 +36,7 @@ namespace Assets.Scripts.Devices.Ble { //移除到3s没有扫描到的设备 var now = DateTime.Now; - var needRemove = discoveredDevices.Where(c => (now - c.Value.LastActiveTime).TotalSeconds >= 3 && c.Value.State != Ant.DeviceState.Connected).ToList(); + var needRemove = discoveredDevices.Where(c => (now - c.Value.LastActiveTime).TotalSeconds > 5 && c.Value.State == Ant.DeviceState.Disconnected).ToList(); foreach (var item in needRemove) { discoveredDevices.Remove(item.Key); diff --git a/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs b/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs index c1936aa5..34e562bb 100644 --- a/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs +++ b/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs @@ -1,16 +1,11 @@ using Assets.Scripts.Ble.Scan; -using Assets.Scripts.Ble.Win; using Assets.Scripts.Devices.Ant; using Assets.Scripts.Devices.Ble; using Assets.Scripts.Devices.Ble.Interfaces; -using Assets.Scripts.Devices.Ble.Win; using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading; -using System.Threading.Tasks; -using System.Timers; using UnityEngine; namespace Assets.Scripts.Ble @@ -225,7 +220,7 @@ namespace Assets.Scripts.Ble IsSuccess = true, Error = null }; - //PeripheralDisconnected(address, info);//连接前断开 + PeripheralDisconnected(address, info);//连接前断开 callback?.Invoke(self, info, s); this.callbacks.Remove(info); Debug.Log("连接成功!" + info.Name); @@ -244,7 +239,9 @@ namespace Assets.Scripts.Ble { var ble = new WinBlePeripheralInfo(address,string.Empty); Debug.Log("disconnect device:" + address); +#if UNITY_IOS peripheralDisconnectedEvent(this, ble, null, false); +#endif PeripheralDisconnected(address, ble); }, (address, error) => @@ -254,8 +251,19 @@ namespace Assets.Scripts.Ble IsSuccess = false, Error = new BleHwInterfaceError(error) }; - callback?.Invoke(self, info, s); - this.callbacks.Remove(info); + var ble = new WinBlePeripheralInfo(address, string.Empty); + + if (this.callbacks.Where(c => c.Key.MatchAddress(address)).Any()) + { + var current = this.callbacks.Where(c => c.Key.MatchAddress(address)).FirstOrDefault(); + current.Value?.Invoke(self, ble, s); + this.callbacks.Remove(current.Key); + } + else + { + peripheralDisconnectedEvent(this, ble, null, false); + } + if (this.callbacks.Any()) { this.ConnectPeripheral(this.callbacks.First().Key, this.callbacks.First().Value); @@ -265,7 +273,7 @@ namespace Assets.Scripts.Ble } } - #region 私有方法 +#region 私有方法 //服务发现 private void ServicesDiscovered(string address, string service) { @@ -330,7 +338,7 @@ namespace Assets.Scripts.Ble characteristicsDiscoveredCallbacks.Remove(characteristicsDiscoveredCallback.First().Key); } } - #endregion +#endregion //设备断开连接 public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback) diff --git a/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs b/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs index a367ca46..1ee7f81a 100644 --- a/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs +++ b/Assets/Scripts/Devices/Ble/mobile/BleMobileThread.cs @@ -43,17 +43,10 @@ namespace Assets.Scripts.Devices.Ble public void StartWatcher() { var self = this; - if (statusEnum == WclBleManagerStatus.RadioOff) + BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) => { - BluetoothLEHardwareInterface.BluetoothEnable(true); - } - else - { - BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) => - { - ScanInfoReceived?.Invoke(self, address, name, rssi, bytes); - }, true); - } + ScanInfoReceived?.Invoke(self, address, name, rssi, bytes); + }, true); } public void Initialize() {