接入蓝牙基本结构
This commit is contained in:
1 parent
84e8b50bbf
commit
2f84f70a5f
152 files changed
+6426
-56
No files matched your search
@@ -0,0 +1,70 @@
|
||||
using Assets.Scripts.Ble.Characteristic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Assets.Scripts.Ble.HeartRate
|
||||
{
|
||||
public class HeartRateMeasurementValue : SuccessOnlyCharacteristicValue
|
||||
{
|
||||
private HeartRateMeasurementValue.HrmFormat Format { get; }
|
||||
|
||||
// Token: 0x170005EB RID: 1515
|
||||
// (get) Token: 0x0600214D RID: 8525 RVA: 0x0008999B File Offset: 0x00087B9B
|
||||
public int BeatsPerMinute { get; }
|
||||
|
||||
public HeartRateMeasurementValue(byte[] data): base(ServiceUuids.Characteristics.HrMeasurement, data)
|
||||
{
|
||||
if(data == null || data.Length < 2)
|
||||
{
|
||||
throw new ArgumentException(string.Concat(new string[]
|
||||
{
|
||||
"Invalid data for ",
|
||||
base.Id.ToString(),
|
||||
" ",
|
||||
(data != null) ? string.Join(",", data) : null,
|
||||
"."
|
||||
}), "data");
|
||||
}
|
||||
HeartRateMeasurementValue.HrmFlags hrmFlags = (HeartRateMeasurementValue.HrmFlags)BitConvertHelper.ToUInt8(data, 0);
|
||||
this.Format = (HrmFormat)(hrmFlags.HasFlag(HeartRateMeasurementValue.HrmFlags.Format) ? 1 : 0);
|
||||
if (this.Format == HeartRateMeasurementValue.HrmFormat.Short)
|
||||
{
|
||||
this.BeatsPerMinute = BitConvertHelper.ToUInt8(data, 1);
|
||||
return;
|
||||
}
|
||||
if(data.Length < 3)
|
||||
{
|
||||
throw new ArgumentException("数据出错");
|
||||
}
|
||||
this.BeatsPerMinute = BitConvertHelper.ToUInt16(data, 1);
|
||||
}
|
||||
|
||||
|
||||
[Flags]
|
||||
private enum HrmFlags
|
||||
{
|
||||
// Token: 0x040029D0 RID: 10704
|
||||
Format = 1,
|
||||
// Token: 0x040029D1 RID: 10705
|
||||
SensorContactStatus1 = 2,
|
||||
// Token: 0x040029D2 RID: 10706
|
||||
SensorContactStatus2 = 4,
|
||||
// Token: 0x040029D3 RID: 10707
|
||||
Energy = 8,
|
||||
// Token: 0x040029D4 RID: 10708
|
||||
RR = 16
|
||||
}
|
||||
|
||||
// Token: 0x02000921 RID: 2337
|
||||
private enum HrmFormat
|
||||
{
|
||||
// Token: 0x040029D6 RID: 10710
|
||||
Short,
|
||||
// Token: 0x040029D7 RID: 10711
|
||||
Long
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3668128267e8c52448ad703e31f8bd88
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,60 @@
|
||||
using Assets.Scripts.Ble.Commands;
|
||||
using Assets.Scripts.Ble.Service;
|
||||
using Assets.Scripts.Commands;
|
||||
using Assets.Scripts.Devices.Ble;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.Scripts.Ble.HeartRate
|
||||
{
|
||||
public class HeartRateService : BleService
|
||||
{
|
||||
private BleCharacteristicInfo heartRateCharacteristic;
|
||||
|
||||
public HeartRateService(BleServiceInfo serviceInfo, BleWinHwInterface bleWinHwInterface) :base(serviceInfo, bleWinHwInterface)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void CharacteristicsDiscovered(BleResponse<List<BleCharacteristicInfo>> response)
|
||||
{
|
||||
foreach (var item in response.Data)
|
||||
{
|
||||
if (item.MatchGuid(ServiceUuids.Characteristics.HrMeasurement))
|
||||
{
|
||||
this.heartRateCharacteristic = item;
|
||||
base.SubscribeCharacteristic(item, HrmCharacteristicSubscribed, HrmCharacteristicRead);
|
||||
}
|
||||
if (item.MatchGuid(ServiceUuids.Characteristics.BodySensorLocation))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HrmCharacteristicSubscribed(BleWinHwInterface winHwInterface, BleCharacteristicInfo characteristic, BleResponse response)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void HrmCharacteristicRead(BleWinHwInterface winHwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
|
||||
{
|
||||
HeartRateMeasurementValue heartRateMeasurementValue;
|
||||
if(base.CheckCharacteristicResponse<HeartRateMeasurementValue>(characteristic, response, (BleCharacteristicInfo info, BleResponse<byte[]> bleResponse) => new HeartRateMeasurementValue(bleResponse.Data), out heartRateMeasurementValue) != CommandResponseCode.Success)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//Debug.Log("心率:"+heartRateMeasurementValue.BeatsPerMinute);
|
||||
}
|
||||
|
||||
protected override BleCharacteristicInfo GetCharacteristicForCommandType(CommandType type)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d36bb0439a6a6c142af91aa05a67a7e0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in new issue
Block a user