2021-08-19 18:03:25 +08:00
|
|
|
|
using Assets.Scripts.Ble;
|
|
|
|
|
|
using Assets.Scripts.Devices.Ant;
|
2021-05-19 14:38:48 +08:00
|
|
|
|
using Assets.Scripts.Devices.Ble;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2021-06-04 10:35:58 +08:00
|
|
|
|
using UnityEngine;
|
2021-05-19 14:38:48 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Assets.Scripts.Devices
|
|
|
|
|
|
{
|
|
|
|
|
|
public sealed class MainDeviceAdapter : IDisposable
|
|
|
|
|
|
{
|
|
|
|
|
|
private List<DeviceAdapter> adapters = new List<DeviceAdapter>();
|
|
|
|
|
|
public MainDeviceAdapter()
|
|
|
|
|
|
{
|
2021-08-31 16:27:36 +08:00
|
|
|
|
#if UNITY_IOS || UNITY_ANDROID
|
2021-08-20 18:45:16 +08:00
|
|
|
|
this.CreateBleAdapter();
|
|
|
|
|
|
#else
|
2023-04-14 16:16:08 +08:00
|
|
|
|
//this.CreateBleAdapter(); TODO: uncomment this line to enable BLE on Windows
|
2021-08-20 18:45:16 +08:00
|
|
|
|
this.CreateAntAdapter();
|
|
|
|
|
|
#endif
|
2021-05-19 14:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CreateAntAdapter()
|
|
|
|
|
|
{
|
|
|
|
|
|
adapters.Add(new AntDeviceAdapter());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CreateBleAdapter()
|
|
|
|
|
|
{
|
2021-08-31 16:27:36 +08:00
|
|
|
|
#if UNITY_IOS || UNITY_ANDROID
|
2021-08-20 18:45:16 +08:00
|
|
|
|
adapters.Add(new BleDeviceAdapter(BleMobileInterface.GetInterface()));
|
2021-08-19 18:03:25 +08:00
|
|
|
|
#else
|
|
|
|
|
|
adapters.Add(new BleDeviceAdapter(BleWinHwInterface.GetInterface()));
|
|
|
|
|
|
#endif
|
2021-05-19 14:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void StartScan()
|
|
|
|
|
|
{
|
|
|
|
|
|
adapters.ForEach(item =>
|
|
|
|
|
|
{
|
|
|
|
|
|
item.StartScan();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void StopScan()
|
|
|
|
|
|
{
|
2021-08-26 19:33:43 +08:00
|
|
|
|
adapters.ForEach(item =>
|
|
|
|
|
|
{
|
|
|
|
|
|
item.StopScan();
|
|
|
|
|
|
});
|
2021-05-19 14:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DeviceAdapterState GetState(ConnectionInterface connectionInterface)
|
|
|
|
|
|
{
|
|
|
|
|
|
var adapter = adapters.FirstOrDefault(a => a.Interface == connectionInterface);
|
|
|
|
|
|
if(adapter != null)
|
|
|
|
|
|
{
|
2021-06-04 18:28:11 +08:00
|
|
|
|
//Debug.Log("bbbbbb " + (adapter.GetState().ToString()));
|
2021-05-19 14:38:48 +08:00
|
|
|
|
return adapter.GetState();
|
|
|
|
|
|
}
|
|
|
|
|
|
return DeviceAdapterState.Unavailable;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<AbstractDevice> GetDevices()
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = new List<AbstractDevice>();
|
|
|
|
|
|
foreach (var item in adapters)
|
|
|
|
|
|
{
|
|
|
|
|
|
result.AddRange(item.GetDevices());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
//throw new NotImplementedException();
|
|
|
|
|
|
foreach (var item in adapters)
|
|
|
|
|
|
{
|
|
|
|
|
|
item.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
|
|
|
|
{
|
2021-06-10 17:46:43 +08:00
|
|
|
|
Debug.Log($"clear device {address}");
|
2021-06-09 18:16:35 +08:00
|
|
|
|
foreach (var item in adapters)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(item is BleDeviceAdapter)
|
|
|
|
|
|
{
|
2021-06-10 17:46:43 +08:00
|
|
|
|
(item as BleDeviceAdapter).ClearDevice(address);
|
2021-06-09 18:16:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-06-10 17:46:43 +08:00
|
|
|
|
|
|
|
|
|
|
public void PrintStatus()
|
|
|
|
|
|
{
|
|
|
|
|
|
var str = "----------------------------\r\n";
|
|
|
|
|
|
var devices = App.MainDeviceAdapter.GetDevices().ToList();
|
|
|
|
|
|
foreach (var item in devices)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item.Sensor != SensorType.Trainer)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (item.Network != NetworkType.BLE)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
str += item.Name + $"{ item.State }\r\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
str += "---------------------------\r\n";
|
|
|
|
|
|
Debug.Log(str);
|
|
|
|
|
|
}
|
2022-06-10 19:31:07 +08:00
|
|
|
|
|
|
|
|
|
|
public BleDeviceAdapter GetBleDeviceAdapter()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var item in adapters)
|
|
|
|
|
|
{
|
|
|
|
|
|
var bleadapter = (BleDeviceAdapter)item;
|
|
|
|
|
|
if (bleadapter != null) {
|
|
|
|
|
|
return bleadapter;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2021-05-19 14:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|