2021-08-19 18:03:25 +08:00

74 lines
2.8 KiB
C#

using Assets.Scripts.Ble;
using Assets.Scripts.Devices.Ant.Interfaces;
using Assets.Scripts.Devices.Ble.Characteristic;
using Assets.Scripts.Devices.Ble.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts.Devices.Ble.Devices
{
public class CyclingPower : BleDevice, IPowerDevice
{
private List<BleServiceInfo> Services;
private CyclingPowerMeasurement cyclingPowerMeasurement;
public CyclingPower(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Power)
{
Priority = 2;
cyclingPowerMeasurement = new CyclingPowerMeasurement(1920);
base.Characteristics.Add(cyclingPowerMeasurement);
bleWinHwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;
}
public int Power { get => cyclingPowerMeasurement.Power; set => throw new NotImplementedException(); }
protected override void CreateServices(List<BleServiceInfo> discoveredServices)
{
this.Services = discoveredServices;
foreach (var service in this.Services)
{
hwInterface.DiscoverCharacteristic(service, (hwInterface, service1, response) =>
{
foreach (var character in response.Data)
{
if (character.MatchGuid(ServiceUuids.Characteristics.CyclingPowerMeasurement))
{
Debug.Log("功率功能");
this.hwInterface.SubscribeCharacteristic(character, (hw, cha, res) =>
{
//Debug.Log("1111111111111111111111");
});
}
}
});
}
}
private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
//Debug.Log("power main call" + string.Join(",", response.Data));
//Debug.Log(characteristic.MatchGuid(cyclingPowerMeasurement.Uuid));
//Debug.Log(characteristic.Service.MatchGuid(cyclingPowerMeasurement.ServiceUuid));
//if (characteristic.MatchGuid(cyclingPowerMeasurement.Uuid))
//{
// cyclingPowerMeasurement.HandleAttributeReceived(response.Data);
//}
foreach (var item in base.Characteristics)
{
if (characteristic.MatchGuid(item.Uuid))
{
item.HandleAttributeReceived(response.Data);
}
}
}
}
}