解决设备连接问题
This commit is contained in:
parent
629e27f5cd
commit
63987511e7
@ -36,7 +36,7 @@ namespace Assets.Scripts.Devices.Ble
|
|||||||
{
|
{
|
||||||
//移除到3s没有扫描到的设备
|
//移除到3s没有扫描到的设备
|
||||||
var now = DateTime.Now;
|
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)
|
foreach (var item in needRemove)
|
||||||
{
|
{
|
||||||
discoveredDevices.Remove(item.Key);
|
discoveredDevices.Remove(item.Key);
|
||||||
|
|||||||
@ -1,16 +1,11 @@
|
|||||||
using Assets.Scripts.Ble.Scan;
|
using Assets.Scripts.Ble.Scan;
|
||||||
using Assets.Scripts.Ble.Win;
|
|
||||||
using Assets.Scripts.Devices.Ant;
|
using Assets.Scripts.Devices.Ant;
|
||||||
using Assets.Scripts.Devices.Ble;
|
using Assets.Scripts.Devices.Ble;
|
||||||
using Assets.Scripts.Devices.Ble.Interfaces;
|
using Assets.Scripts.Devices.Ble.Interfaces;
|
||||||
using Assets.Scripts.Devices.Ble.Win;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Timers;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Assets.Scripts.Ble
|
namespace Assets.Scripts.Ble
|
||||||
@ -225,7 +220,7 @@ namespace Assets.Scripts.Ble
|
|||||||
IsSuccess = true,
|
IsSuccess = true,
|
||||||
Error = null
|
Error = null
|
||||||
};
|
};
|
||||||
//PeripheralDisconnected(address, info);//连接前断开
|
PeripheralDisconnected(address, info);//连接前断开
|
||||||
callback?.Invoke(self, info, s);
|
callback?.Invoke(self, info, s);
|
||||||
this.callbacks.Remove(info);
|
this.callbacks.Remove(info);
|
||||||
Debug.Log("连接成功!" + info.Name);
|
Debug.Log("连接成功!" + info.Name);
|
||||||
@ -244,7 +239,9 @@ namespace Assets.Scripts.Ble
|
|||||||
{
|
{
|
||||||
var ble = new WinBlePeripheralInfo(address,string.Empty);
|
var ble = new WinBlePeripheralInfo(address,string.Empty);
|
||||||
Debug.Log("disconnect device:" + address);
|
Debug.Log("disconnect device:" + address);
|
||||||
|
#if UNITY_IOS
|
||||||
peripheralDisconnectedEvent(this, ble, null, false);
|
peripheralDisconnectedEvent(this, ble, null, false);
|
||||||
|
#endif
|
||||||
PeripheralDisconnected(address, ble);
|
PeripheralDisconnected(address, ble);
|
||||||
},
|
},
|
||||||
(address, error) =>
|
(address, error) =>
|
||||||
@ -254,8 +251,19 @@ namespace Assets.Scripts.Ble
|
|||||||
IsSuccess = false,
|
IsSuccess = false,
|
||||||
Error = new BleHwInterfaceError(error)
|
Error = new BleHwInterfaceError(error)
|
||||||
};
|
};
|
||||||
callback?.Invoke(self, info, s);
|
var ble = new WinBlePeripheralInfo(address, string.Empty);
|
||||||
this.callbacks.Remove(info);
|
|
||||||
|
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())
|
if (this.callbacks.Any())
|
||||||
{
|
{
|
||||||
this.ConnectPeripheral(this.callbacks.First().Key, this.callbacks.First().Value);
|
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)
|
private void ServicesDiscovered(string address, string service)
|
||||||
{
|
{
|
||||||
@ -330,7 +338,7 @@ namespace Assets.Scripts.Ble
|
|||||||
characteristicsDiscoveredCallbacks.Remove(characteristicsDiscoveredCallback.First().Key);
|
characteristicsDiscoveredCallbacks.Remove(characteristicsDiscoveredCallback.First().Key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
//设备断开连接
|
//设备断开连接
|
||||||
public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback)
|
public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback)
|
||||||
|
|||||||
@ -43,17 +43,10 @@ namespace Assets.Scripts.Devices.Ble
|
|||||||
|
|
||||||
public void StartWatcher() {
|
public void StartWatcher() {
|
||||||
var self = this;
|
var self = this;
|
||||||
if (statusEnum == WclBleManagerStatus.RadioOff)
|
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) =>
|
||||||
{
|
{
|
||||||
BluetoothLEHardwareInterface.BluetoothEnable(true);
|
ScanInfoReceived?.Invoke(self, address, name, rssi, bytes);
|
||||||
}
|
}, true);
|
||||||
else
|
|
||||||
{
|
|
||||||
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) =>
|
|
||||||
{
|
|
||||||
ScanInfoReceived?.Invoke(self, address, name, rssi, bytes);
|
|
||||||
}, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user