设备扫描优化&我的收藏返回记忆位置

This commit is contained in:
lishuo committed 2022-07-19 15:47:43 +08:00
1 parent c209811f9f
commit 7f56624ec7
10 files changed
+71 -61

No files matched your search

+24 -21
View File
@@ -1,14 +1,11 @@
using Assets.Scripts.Ble;
using Assets.Scripts.Ble.Service;
using Assets.Scripts.Devices.Ble.Devices;
using Assets.Scripts.Devices.Ble.Interfaces;
using Assets.Scripts.UI.Prefab.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts.Devices.Ble
@@ -20,14 +17,33 @@ namespace Assets.Scripts.Devices.Ble
private IDictionary<string, BleDevice> discoveredDevices = new Dictionary<string, BleDevice>();
public IBleWinHwInterface hwInterface { get; private set; }
private System.Timers.Timer timer;
public BleDeviceAdapter(IBleWinHwInterface bleWinHwInterface)
{
if (timer == null)
{
timer = new System.Timers.Timer(1000);
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
hwInterface = bleWinHwInterface;// BleWinHwInterface.GetInterface();
hwInterface.BluetoothStateChangedEvent += HwInterface_BluetoothStateChangedEvent;
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//移除到3s没有扫描到的设备
var now = DateTime.Now;
var needRemove = discoveredDevices.Where(c => (now - c.Value.LastActiveTime).TotalSeconds >= 3 && c.Value.State != Ant.DeviceState.Connected).ToList();
foreach (var item in needRemove)
{
discoveredDevices.Remove(item.Key);
}
}
private void HwInterface_BluetoothStateChangedEvent(IBleWinHwInterface hwInterface, BleState bleState)
{
if(bleState == BleState.Off)
@@ -60,14 +76,10 @@ namespace Assets.Scripts.Devices.Ble
{
hwInterface.StartScan((device) =>
{
if (!discoveredDevices.ContainsKey(device.Peripheral.Address))
if (!discoveredDevices.ContainsKey(device.Peripheral.Address)&& !string.IsNullOrWhiteSpace(device.Peripheral.Name))
{
Debug.Log($"发现设备{ device.Peripheral.Address }, \t\tName:{ device.Peripheral.Name }, type:{ device.SensorType } 服务:{string.Join(",", device.Services)}");
if (string.IsNullOrWhiteSpace(device.Peripheral.Name))
{
return;
}
if (device.SensorType == Ant.SensorType.FtmsTrainer)
{
var device1 = new Ftms(device.Peripheral, hwInterface);
@@ -110,10 +122,6 @@ namespace Assets.Scripts.Devices.Ble
var device1 = new SpeedCadence(device.Peripheral, hwInterface);
discoveredDevices.Add(device.Peripheral.Address, device1);
}
else
{
return;
}
var device111 = discoveredDevices.Last().Value;
if (device111 != null && device111.State == Ant.DeviceState.Disconnected)
@@ -134,14 +142,7 @@ namespace Assets.Scripts.Devices.Ble
{
discoveredDevices[device.Peripheral.Address].SignalStrength = device.Rssi;
discoveredDevices[device.Peripheral.Address].LastActiveTime = DateTime.Now;
//Debug.Log($"设备{ device.Peripheral.Name }信号量:{ device.Rssi }");
}
//移除到5s没有扫描到的设备
var now = DateTime.Now;
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);
Debug.Log($"设备{ device.Peripheral.Name }信号量:{ device.Rssi }");
}
});
}
@@ -155,6 +156,8 @@ namespace Assets.Scripts.Devices.Ble
{
this.ReleaseDevices();
hwInterface.Dispose();
timer.Stop();
timer.Dispose();
base.Dispose();
}
@@ -225,7 +225,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);
@@ -242,8 +242,10 @@ namespace Assets.Scripts.Ble
CharacteristicsDiscovered(address, service, characteristic);
}, (address) =>
{
var ble = new WinBlePeripheralInfo(address,string.Empty);
Debug.Log("disconnect device:" + address);
PeripheralDisconnected(address, info);
peripheralDisconnectedEvent(this, ble, null, false);
PeripheralDisconnected(address, ble);
},
(address, error) =>
{
@@ -36,7 +36,7 @@ namespace Assets.Scripts.Devices.Ble
this.managerStatusChanged -= value;
}
}
WclBleManagerStatus statusEnum = WclBleManagerStatus.RadioOn;
WclBleManagerStatus statusEnum = WclBleManagerStatus.RadioOff;
internal BleMobileThread() {
Initialize();//初始蓝牙
}
@@ -45,7 +45,6 @@ namespace Assets.Scripts.Devices.Ble
var self = this;
if (statusEnum == WclBleManagerStatus.RadioOff)
{
//Initialize();
BluetoothLEHardwareInterface.BluetoothEnable(true);
}
else