93 lines
3.5 KiB
C#
93 lines
3.5 KiB
C#
using Assets.Scripts.Ble;
|
||
using Assets.Scripts.Devices.Ant.Interfaces;
|
||
using Assets.Scripts.Devices.Ble.Characteristic;
|
||
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 HeartRate : BleDevice, IHeartRateDevice
|
||
{
|
||
int IHeartRateDevice.HeartRate { get => heartRateMeasurement.BeatsPerMinute; set => throw new NotImplementedException(); }
|
||
|
||
private List<BleServiceInfo> Services;
|
||
private HeartRateMeasurement heartRateMeasurement;
|
||
public HeartRate(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.HeartRate)
|
||
{
|
||
Priority = 2;
|
||
heartRateMeasurement = new HeartRateMeasurement();
|
||
|
||
base.Characteristics.Add(heartRateMeasurement);
|
||
|
||
bleWinHwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;
|
||
}
|
||
|
||
protected override void CreateServices(List<BleServiceInfo> discoveredServices)
|
||
{
|
||
//throw new NotImplementedException();
|
||
this.Services = discoveredServices;
|
||
|
||
foreach (var service in this.Services)
|
||
{
|
||
hwInterface.DiscoverCharacteristic(service, (hwInterface, service1, response) =>
|
||
{
|
||
foreach (var character in response.Data)
|
||
{
|
||
if (character.MatchGuid(heartRateMeasurement.Uuid))
|
||
{
|
||
this.hwInterface.SubscribeCharacteristic(character, (hw, cha, res) =>
|
||
{
|
||
//Debug.Log($"心率计subscribe char:{ }");
|
||
});
|
||
continue;
|
||
}
|
||
}
|
||
foreach (var item in Characteristics.Where(c => c.IsOptional))
|
||
{
|
||
//Debug.Log(item.GetType() + "服务可用"+ item.Uuid.ToString() +", service:" + item.ServiceUuid);
|
||
var ccc = response.Data.FirstOrDefault(r => r.MatchGuid(item.Uuid));
|
||
if (ccc == null)
|
||
{
|
||
item.SetUnavailable();
|
||
}
|
||
else
|
||
{
|
||
Debug.Log(item.GetType() + "服务可用");
|
||
|
||
GetBatteryLevel(ccc);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
}
|
||
|
||
public void GetBatteryLevel(BleCharacteristicInfo bbbb)
|
||
{
|
||
this.hwInterface.ReadCharacteristic(bbbb, (hwInterface1, characteristic1, response1) => {
|
||
Debug.Log("read收到消息:" + string.Join(",", response1));
|
||
});
|
||
}
|
||
|
||
private void CharacteristicReadMainCallback(BleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
|
||
{
|
||
//Debug.Log("heart rate main call" + string.Join(",", response.Data));
|
||
|
||
foreach (var item in base.Characteristics)
|
||
{
|
||
if (characteristic.MatchGuid(item.Uuid))
|
||
{
|
||
item.HandleAttributeReceived(response.Data);
|
||
}
|
||
}
|
||
|
||
//heartRateMeasurement.HandleAttributeReceived(response.Data);
|
||
}
|
||
}
|
||
|
||
}
|