207 lines
7.6 KiB
C#
207 lines
7.6 KiB
C#
using Assets.Scripts.Ble;
|
|
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.Threading;
|
|
using UnityEngine;
|
|
|
|
namespace Assets.Scripts.Devices.Ble
|
|
{
|
|
public class BleDeviceAdapter : DeviceAdapter
|
|
{
|
|
public override ConnectionInterface Interface => ConnectionInterface.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)
|
|
{
|
|
discoveredDevices.Clear();
|
|
this.StopScan();
|
|
}
|
|
else
|
|
{
|
|
this.StartScan();
|
|
}
|
|
}
|
|
|
|
public override IEnumerable<AbstractDevice> GetDevices()
|
|
{
|
|
//throw new NotImplementedException();
|
|
return discoveredDevices.Select(d => d.Value);
|
|
}
|
|
|
|
public override DeviceAdapterState GetState()
|
|
{
|
|
if(hwInterface.BleState == BleState.On)
|
|
{
|
|
return DeviceAdapterState.On;
|
|
}
|
|
return DeviceAdapterState.Unavailable;
|
|
}
|
|
|
|
public override void StartScan()
|
|
{
|
|
hwInterface.StartScan((device) =>
|
|
{
|
|
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 (device.SensorType == Ant.SensorType.FtmsTrainer)
|
|
{
|
|
var device1 = new Ftms(device.Peripheral, hwInterface);
|
|
if (discoveredDevices.ContainsKey(device.Peripheral.Address))
|
|
{
|
|
discoveredDevices[device.Peripheral.Address] = device1;
|
|
}
|
|
else
|
|
{
|
|
discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
}
|
|
}
|
|
else if (device.SensorType == Ant.SensorType.Trainer)
|
|
{
|
|
//var device1 = new Ftms(device.Peripheral, hwInterface);
|
|
//discoveredDevices.Add(device.Peripheral.Address, device0);
|
|
|
|
var device1 = new Tacx(device.Peripheral, hwInterface);
|
|
if (discoveredDevices.ContainsKey(device.Peripheral.Address))
|
|
{
|
|
discoveredDevices[device.Peripheral.Address] = device1;
|
|
}
|
|
else
|
|
{
|
|
discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
}
|
|
}
|
|
else if (device.SensorType == Ant.SensorType.HeartRate)
|
|
{
|
|
var device1 = new HeartRate(device.Peripheral, hwInterface);
|
|
discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
}
|
|
else if (device.SensorType == Ant.SensorType.Power)
|
|
{
|
|
var device1 = new CyclingPower(device.Peripheral, hwInterface);
|
|
discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
}
|
|
else if (device.SensorType == Ant.SensorType.SpeedCadence)
|
|
{
|
|
var device1 = new SpeedCadence(device.Peripheral, hwInterface);
|
|
discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
}
|
|
|
|
var device111 = discoveredDevices.Last().Value;
|
|
if (device111 != null && device111.State == Ant.DeviceState.Disconnected)
|
|
{
|
|
if (DeviceCache.Exist(device111))
|
|
{
|
|
//TODO:取消注释,自动连接设备
|
|
//Debug.Log("自动连接" + device111.Id);
|
|
|
|
device111.Connect();
|
|
}
|
|
}
|
|
|
|
//discoveredDevices.Add(device.Peripheral.Address, new BleDevice(device.Peripheral, hwInterface, device.SensorType));
|
|
|
|
}
|
|
if (discoveredDevices.ContainsKey(device.Peripheral.Address))
|
|
{
|
|
discoveredDevices[device.Peripheral.Address].SignalStrength = device.Rssi;
|
|
discoveredDevices[device.Peripheral.Address].LastActiveTime = DateTime.Now;
|
|
Debug.Log($"设备{ device.Peripheral.Name }信号量:{ device.Rssi }");
|
|
}
|
|
});
|
|
}
|
|
|
|
public override void StopScan()
|
|
{
|
|
hwInterface.StopScan();
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
this.ReleaseDevices();
|
|
hwInterface.Dispose();
|
|
timer.Stop();
|
|
timer.Dispose();
|
|
base.Dispose();
|
|
}
|
|
|
|
public void ReleaseDevices()
|
|
{
|
|
foreach (var item in discoveredDevices)
|
|
{
|
|
item.Value.Dispose();
|
|
}
|
|
}
|
|
|
|
public override void Refresh()
|
|
{
|
|
var list = discoveredDevices.ToList();
|
|
foreach (var item in list)
|
|
{
|
|
if (item.Value.State == Ant.DeviceState.Disconnected)
|
|
{
|
|
item.Value.Dispose();
|
|
discoveredDevices.Remove(item.Key);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清除未连接的设备
|
|
/// </summary>
|
|
public void ClearDevice(string address)
|
|
{
|
|
var list = discoveredDevices.ToList();
|
|
foreach (var item in list)
|
|
{
|
|
if(item.Value.State == Ant.DeviceState.Disconnected)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(address) && item.Key != address)
|
|
{
|
|
continue;
|
|
}
|
|
hwInterface.pCache.Remove(item.Key);
|
|
item.Value.Dispose();
|
|
discoveredDevices.Remove(item.Key);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|