2021-05-19 14:38:48 +08:00
|
|
|
|
using Assets.Scripts.Ble;
|
|
|
|
|
|
using Assets.Scripts.Ble.Service;
|
|
|
|
|
|
using Assets.Scripts.Devices.Ble.Devices;
|
2021-08-19 18:03:25 +08:00
|
|
|
|
using Assets.Scripts.Devices.Ble.Interfaces;
|
2021-06-08 10:30:26 +08:00
|
|
|
|
using Assets.Scripts.UI.Prefab.Device;
|
2021-05-19 14:38:48 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2021-08-27 15:06:56 +08:00
|
|
|
|
using System.Threading;
|
2021-05-19 14:38:48 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
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>();
|
|
|
|
|
|
|
2021-08-19 18:03:25 +08:00
|
|
|
|
private IBleWinHwInterface hwInterface { get; set; }
|
|
|
|
|
|
public BleDeviceAdapter(IBleWinHwInterface bleWinHwInterface)
|
2021-05-19 14:38:48 +08:00
|
|
|
|
{
|
2021-08-19 18:03:25 +08:00
|
|
|
|
|
|
|
|
|
|
hwInterface = bleWinHwInterface;// BleWinHwInterface.GetInterface();
|
2021-06-10 17:46:43 +08:00
|
|
|
|
|
|
|
|
|
|
hwInterface.BluetoothStateChangedEvent += HwInterface_BluetoothStateChangedEvent;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-19 18:03:25 +08:00
|
|
|
|
private void HwInterface_BluetoothStateChangedEvent(IBleWinHwInterface hwInterface, BleState bleState)
|
2021-06-10 17:46:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
//Debug.Log("22222222222222" + bleState);
|
|
|
|
|
|
if(bleState == BleState.Off)
|
|
|
|
|
|
{
|
|
|
|
|
|
discoveredDevices.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
this.StartScan();
|
|
|
|
|
|
}
|
2021-05-19 14:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
2021-06-10 17:46:43 +08:00
|
|
|
|
{
|
2021-05-19 14:38:48 +08:00
|
|
|
|
hwInterface.StartScan((device) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!discoveredDevices.ContainsKey(device.Peripheral.Address))
|
|
|
|
|
|
{
|
2021-06-04 10:35:58 +08:00
|
|
|
|
Debug.Log($"发现设备{ device.Peripheral.Address }, \t\tName:{ device.Peripheral.Name }, type:{ device.SensorType }");
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(device.Peripheral.Name))
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-09-23 18:14:53 +08:00
|
|
|
|
if (device.SensorType == Ant.SensorType.Trainer)
|
2021-05-19 14:38:48 +08:00
|
|
|
|
{
|
2021-09-23 18:14:53 +08:00
|
|
|
|
//var device0 = new Ftms(device.Peripheral, hwInterface);
|
|
|
|
|
|
//discoveredDevices.Add(device.Peripheral.Address, device0);
|
|
|
|
|
|
|
2021-06-04 10:35:58 +08:00
|
|
|
|
var device1 = new Tacx(device.Peripheral, hwInterface);
|
2021-09-23 18:14:53 +08:00
|
|
|
|
if (discoveredDevices.ContainsKey(device.Peripheral.Address))
|
|
|
|
|
|
{
|
|
|
|
|
|
discoveredDevices[device.Peripheral.Address] = device1;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
|
|
|
|
}
|
2021-05-19 14:38:48 +08:00
|
|
|
|
}
|
2021-09-23 18:14:53 +08:00
|
|
|
|
else if (device.SensorType == Ant.SensorType.HeartRate)
|
2021-05-19 14:38:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
var device1 = new HeartRate(device.Peripheral, hwInterface);
|
|
|
|
|
|
discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
|
|
|
|
}
|
2021-09-23 18:14:53 +08:00
|
|
|
|
else if (device.SensorType == Ant.SensorType.Power)
|
2021-05-19 14:38:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
var device1 = new CyclingPower(device.Peripheral, hwInterface);
|
|
|
|
|
|
discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
|
|
|
|
}
|
2021-09-23 18:14:53 +08:00
|
|
|
|
else if (device.SensorType == Ant.SensorType.SpeedCadence)
|
2021-06-04 10:35:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
var device1 = new SpeedCadence(device.Peripheral, hwInterface);
|
|
|
|
|
|
discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
|
|
|
|
}
|
2021-09-23 18:14:53 +08:00
|
|
|
|
else if (device.SensorType == Ant.SensorType.Rower)
|
|
|
|
|
|
{
|
|
|
|
|
|
var device1 = new FtmsRower(device.Peripheral, hwInterface);
|
|
|
|
|
|
if (discoveredDevices.ContainsKey(device.Peripheral.Address))
|
|
|
|
|
|
{
|
|
|
|
|
|
discoveredDevices[device.Peripheral.Address] = device1;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-06-08 10:30:26 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-08-30 18:28:26 +08:00
|
|
|
|
|
2021-06-08 10:30:26 +08:00
|
|
|
|
var device111 = discoveredDevices.Last().Value;
|
|
|
|
|
|
if (device111 != null && device111.State == Ant.DeviceState.Disconnected)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (DeviceCache.Exist(device111))
|
|
|
|
|
|
{
|
|
|
|
|
|
//TODO:取消注释,自动连接设备
|
|
|
|
|
|
//Debug.Log("自动连接" + device111.Id);
|
2021-06-09 18:16:35 +08:00
|
|
|
|
device111.Connect();
|
2021-06-08 10:30:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-19 14:38:48 +08:00
|
|
|
|
//discoveredDevices.Add(device.Peripheral.Address, new BleDevice(device.Peripheral, hwInterface, device.SensorType));
|
2021-06-08 10:30:26 +08:00
|
|
|
|
|
2021-05-19 14:38:48 +08:00
|
|
|
|
}
|
2021-06-04 10:35:58 +08:00
|
|
|
|
if (discoveredDevices.ContainsKey(device.Peripheral.Address))
|
|
|
|
|
|
{
|
|
|
|
|
|
discoveredDevices[device.Peripheral.Address].SignalStrength = device.Rssi;
|
2021-06-09 18:16:35 +08:00
|
|
|
|
//Debug.Log($"设备{ device.Peripheral.Name }信号量:{ device.Rssi }");
|
2021-06-04 10:35:58 +08:00
|
|
|
|
}
|
2021-05-19 14:38:48 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void StopScan()
|
|
|
|
|
|
{
|
2021-08-26 19:33:43 +08:00
|
|
|
|
hwInterface.StopScan();
|
2021-05-19 14:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Dispose()
|
|
|
|
|
|
{
|
2021-06-08 10:30:26 +08:00
|
|
|
|
this.ReleaseDevices();
|
2021-05-19 14:38:48 +08:00
|
|
|
|
hwInterface.Dispose();
|
|
|
|
|
|
base.Dispose();
|
|
|
|
|
|
}
|
2021-06-08 10:30:26 +08:00
|
|
|
|
|
|
|
|
|
|
public void ReleaseDevices()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var item in discoveredDevices)
|
|
|
|
|
|
{
|
|
|
|
|
|
item.Value.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-06-09 18:16:35 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清除未连接的设备
|
|
|
|
|
|
/// </summary>
|
2021-06-10 17:46:43 +08:00
|
|
|
|
public void ClearDevice(string address)
|
2021-06-09 18:16:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
var list = discoveredDevices.ToList();
|
|
|
|
|
|
foreach (var item in list)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(item.Value.State == Ant.DeviceState.Disconnected)
|
|
|
|
|
|
{
|
2021-06-10 17:46:43 +08:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(address) && item.Key != address)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2021-06-09 18:16:35 +08:00
|
|
|
|
hwInterface.pCache.Remove(item.Key);
|
2021-06-10 17:46:43 +08:00
|
|
|
|
item.Value.Dispose();
|
2021-06-09 18:16:35 +08:00
|
|
|
|
discoveredDevices.Remove(item.Key);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-05-19 14:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|