97 lines
3.6 KiB
C#
97 lines
3.6 KiB
C#
using Assets.Scripts.Ble;
|
|
using Assets.Scripts.Ble.Service;
|
|
using Assets.Scripts.Devices.Ble.Devices;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
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>();
|
|
|
|
private BleWinHwInterface hwInterface;
|
|
public BleDeviceAdapter()
|
|
{
|
|
hwInterface = BleWinHwInterface.GetInterface();
|
|
}
|
|
|
|
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))
|
|
{
|
|
Debug.Log($"发现设备{ device.Peripheral.Address }, \t\tName:{ device.Peripheral.Name }, type:{ device.SensorType }");
|
|
|
|
if (string.IsNullOrWhiteSpace(device.Peripheral.Name))
|
|
{
|
|
return;
|
|
}
|
|
if(device.SensorType == Ant.SensorType.Trainer)
|
|
{
|
|
//var device1 = new Ftms(device.Peripheral, hwInterface);
|
|
//discoveredDevices.Add(device.Peripheral.Address, device1);
|
|
|
|
var device1 = new Tacx(device.Peripheral, hwInterface);
|
|
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);
|
|
}
|
|
//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;
|
|
//Debug.Log($"设备{ device.Peripheral.Name }信号量:{ device.Rssi }");
|
|
}
|
|
});
|
|
}
|
|
|
|
public override void StopScan()
|
|
{
|
|
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
hwInterface.Dispose();
|
|
base.Dispose();
|
|
}
|
|
}
|
|
}
|