修复设备选择界面多设备选择bug

This commit is contained in:
suntao committed 2021-06-04 18:28:11 +08:00
1 parent 7729332f41
commit 5671b35dce
21 files changed
+111 -56

No files matched your search

+7 -1
View File
@@ -13,6 +13,11 @@ namespace Assets.Scripts.Devices
public SensorType Sensor { get; protected set; }
public string Id
{
get; protected set;
}
public virtual string Name
{
get;
@@ -34,8 +39,9 @@ namespace Assets.Scripts.Devices
/// </summary>
public int SignalStrength { get; internal set; }
protected AbstractDevice(NetworkType networkType)
protected AbstractDevice(string id, NetworkType networkType)
{
this.Id = id;
this.Network = networkType;
}
@@ -93,8 +93,8 @@ namespace Assets.Scripts.Devices.Ant
//private readonly DeviceDetailService _deviceDetailService;
//private readonly AntManufacturerService antManufacturerService;
protected List<IPageHandler> pageHandlers = new List<IPageHandler>();
public AbstractAntDevice(String defaultSourceName, racerSportType sportType, SensorType sensor)
: base(sportType, true)
public AbstractAntDevice(string id, String defaultSourceName, racerSportType sportType, SensorType sensor)
: base(id, sportType, true)
{
State = DeviceState.Disconnected;
this.Name = defaultSourceName;
+11 -10
View File
@@ -81,11 +81,11 @@ namespace Assets.Scripts.Devices.Ant
}
//CheckStatus();
deviceList.Add(new FitDevice());
deviceList.Add(new PowerDevice());
deviceList.Add(new CadenceDevice());
deviceList.Add(new HeartRateDevice());
deviceList.Add(new BikeSpdCadDevice());
deviceList.Add(new FitDevice(""));
deviceList.Add(new PowerDevice(""));
deviceList.Add(new CadenceDevice(""));
deviceList.Add(new HeartRateDevice(""));
deviceList.Add(new BikeSpdCadDevice(""));
var timer = new System.Timers.Timer(1000);
timer.AutoReset = true;
@@ -336,27 +336,28 @@ namespace Assets.Scripts.Devices.Ant
var device1 = deviceList.SingleOrDefault(d => d.searchProfile.deviceType == deviceTypeId);
if (device1 != null)
{
var id = $"{ deviceNumber }:{ deviceTypeId}:{ transmissionTypeId }";
AbstractAntDevice device = null;
switch (device1.Sensor)
{
case SensorType.None:
break;
case SensorType.Cadence:
device = new CadenceDevice();
device = new CadenceDevice(id);
break;
case SensorType.HeartRate:
device = new HeartRateDevice();
device = new HeartRateDevice(id);
break;
case SensorType.Power:
device = new PowerDevice();
device = new PowerDevice(id);
break;
case SensorType.Speed:
break;
case SensorType.SpeedCadence:
device = new BikeSpdCadDevice();
device = new BikeSpdCadDevice(id);
break;
case SensorType.Trainer:
device = new FitDevice();
device = new FitDevice(id);
break;
case SensorType.VirtualPower:
break;
@@ -18,8 +18,8 @@ namespace Assets.Scripts.Devices.Ant
private RotationData _speedData = new RotationData();
private RotationData _cadenceData = new RotationData();
public BikeSpdCadDevice(double wheelCircumfrence_m = DEFAULT_WHEEL_CIRCUMFERENCE_m)
: base("Ant+ Speed&Cadence", racerSportType.Biking, SensorType.SpeedCadence)
public BikeSpdCadDevice(string id, double wheelCircumfrence_m = DEFAULT_WHEEL_CIRCUMFERENCE_m)
: base(id, "Ant+ Speed&Cadence", racerSportType.Biking, SensorType.SpeedCadence)
{
Priority = 1;
this.wheelCircumfrence_m = wheelCircumfrence_m;
+2 -2
View File
@@ -28,8 +28,8 @@ namespace Assets.Scripts.Devices.Ant
private DateTime _now = DateTime.Now;
private RotationData _rotationData = new RotationData();
public CadenceDevice()
: base("Ant+ Cadence", racerSportType.Biking, SensorType.Cadence)
public CadenceDevice(string id)
: base(id, "Ant+ Cadence", racerSportType.Biking, SensorType.Cadence)
{
Priority = 2;
//if (speedSensor.isInUse)
+1 -1
View File
@@ -26,7 +26,7 @@ namespace Assets.Scripts.Devices.Ant
public readonly byte uid;
public DataSourceBase(racerSportType sportType, bool isHuman):base(NetworkType.ANT)
public DataSourceBase(string id, racerSportType sportType, bool isHuman):base(id, NetworkType.ANT)
{
this.sportType = sportType;
this.isHuman = isHuman;
+2 -2
View File
@@ -25,8 +25,8 @@ namespace Assets.Scripts.Devices.Ant
/// </summary>
private double _grade = 0;
private bool IsCalibrating { get; set; }
public FitDevice()
: base("Ant+ Trainer", racerSportType.Unknown, SensorType.Trainer)
public FitDevice(string id)
: base(id, "Ant+ Trainer", racerSportType.Unknown, SensorType.Trainer)
{
Priority = 0;
this.StateChange = (state) =>
@@ -26,8 +26,8 @@ namespace Assets.Scripts.Devices.Ant
//ds_AntPlus_BikeSpd speedSensor;
public HeartRateDevice()
: base("Ant+ HeartRate", racerSportType.Unknown, SensorType.HeartRate)
public HeartRateDevice(string id)
: base(id, "Ant+ HeartRate", racerSportType.Unknown, SensorType.HeartRate)
{
Priority = 2;
}
+2 -2
View File
@@ -23,8 +23,8 @@ namespace Assets.Scripts.Devices.Ant
//List<IPageHandler> pageHandlers;
public PowerDevice()
: base("Ant+ Power", racerSportType.Biking, SensorType.Power)
public PowerDevice(string id)
: base(id, "Ant+ Power", racerSportType.Biking, SensorType.Power)
{
Priority = 2;
//pageHandlers = new List<IPageHandler>();
+27 -4
View File
@@ -23,10 +23,15 @@ namespace Assets.Scripts.Devices.Ble
private readonly HashSet<BleServiceInfo> pendingServices = new HashSet<BleServiceInfo>();
public override string Name { get => peripheralInfo.Name; protected set => base.Name = value; }
public string Address {
get
{
return peripheralInfo.Address;
}
}
public List<ICharacteristic> Characteristics { get; protected set; } = new List<ICharacteristic>();
public BleDevice(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface, SensorType sensor) : base(NetworkType.BLE)
public BleDevice(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface, SensorType sensor) : base(peripheralInfo.Address, NetworkType.BLE)
{
this.hwInterface = bleWinHwInterface;
this.hwInterface.BluetoothStateChangedEvent += BluetoothStateChangedEvent;
@@ -45,7 +50,23 @@ namespace Assets.Scripts.Devices.Ble
public void ConnectToPeripheralIfPossible()
{
this.hwInterface.ConnectPeripheral(this.peripheralInfo, PeripheralConnectedAction);
if(this.peripheralInfo == null)
{
return;
}
if(!this.hwInterface.BleState.Equals(BleState.On) || this.State != DeviceState.Disconnected)
{
return;
}
this.State = DeviceState.Connecting;
try
{
this.hwInterface.ConnectPeripheral(this.peripheralInfo, PeripheralConnectedAction);
}
catch (Exception)
{
this.State = DeviceState.Disconnected;
}
}
private void PeripheralConnectedAction(BleWinHwInterface hwInterface, BlePeripheralInfo sender, BleResponse response)
@@ -201,6 +222,7 @@ namespace Assets.Scripts.Devices.Ble
{
if (this.State != DeviceState.Disconnected)
{
Debug.Log("断开设备" + this.Name);
this.hwInterface.DisconnectPeripheral(this.peripheralInfo, ()=> {
this.State = DeviceState.Disconnected;
});
@@ -238,7 +260,8 @@ namespace Assets.Scripts.Devices.Ble
public override void Disconnect(bool save = true)
{
//throw new NotImplementedException();
this.hwInterface.DisconnectPeripheral(this.peripheralInfo, null);
//this.hwInterface.DisconnectPeripheral(this.peripheralInfo, null);
this.Disconnect();
}
}
}
@@ -62,6 +62,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
return;
this.Cadence = 0;
}
Debug.Log("踏频值:" + this.Cadence);
}
private void HandleSpeedUpdate()
@@ -16,7 +16,7 @@ namespace Assets.Scripts.Devices.Ble.Devices
private CyclingPowerMeasurement cyclingPowerMeasurement;
public CyclingPower(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Power)
{
Priority = 2;
cyclingPowerMeasurement = new CyclingPowerMeasurement(1920);
base.Characteristics.Add(cyclingPowerMeasurement);
bleWinHwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;
@@ -18,6 +18,7 @@ namespace Assets.Scripts.Devices.Ble.Devices
private HeartRateMeasurement heartRateMeasurement;
public HeartRate(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.HeartRate)
{
Priority = 2;
heartRateMeasurement = new HeartRateMeasurement();
base.Characteristics.Add(heartRateMeasurement);
@@ -21,6 +21,7 @@ namespace Assets.Scripts.Devices.Ble.Devices
public SpeedCadence(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.SpeedCadence)
{
Debug.Log("创建速度踏频设备");
Priority = 1;
_cyclingSpeedCadenceMeasurement = new CyclingSpeedCadenceMeasurement(this._wheelCircumference);
base.Characteristics.Add(_cyclingSpeedCadenceMeasurement);
@@ -266,9 +266,19 @@ namespace Assets.Scripts.Ble
private void GattConnected(WclBleGattThread gattClient, BleResponse response)
{
Debug.Log($"gatt connected { response.ToString() }");
if (!response.IsSuccess)
{
gattClient.Stop();
return;
}
this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
}
private void CleanUpPeripheral(BlePeripheralInfo peripheralInfo)
{
//this.callbacks.Clear()
}
private void GattDisconnected(WclBleGattThread gattClient, BleResponse response)
{
Debug.Log("gatt disconnected");
@@ -326,7 +336,7 @@ namespace Assets.Scripts.Ble
}
private void GattCharacteristicChanged(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
Debug.Log("characteristic changed");
//Debug.Log("characteristic changed");
if(this.wclBleMainThread.GetGattThread(characteristic.Peripheral) != null)
{
this.characteristicReadEvent.Invoke(this, characteristic, response);
@@ -418,7 +428,7 @@ namespace Assets.Scripts.Ble
public void Dispose()
{
this.wclBleMainThread.Dispose();
this.wclBleMainThread.Stop();
this.wclBleMainThread = null;
hwInterface = null;
pCache.Clear();
@@ -98,7 +98,7 @@ namespace Assets.Scripts.Devices.Ble.Win
});
}
public void Dispose()
public void Stop()
{
Debug.Log("停止thread");
foreach (var item in this.gattClients.Values)
+1 -1
View File
@@ -46,7 +46,7 @@ namespace Assets.Scripts.Devices
var adapter = adapters.FirstOrDefault(a => a.Interface == connectionInterface);
if(adapter != null)
{
Debug.Log("bbbbbb " + (adapter.GetState().ToString()));
//Debug.Log("bbbbbb " + (adapter.GetState().ToString()));
return adapter.GetState();
}
return DeviceAdapterState.Unavailable;