powerfun-unity/Assets/Scripts/Devices/Ble/Characteristic/ICharacteristicExtensions.cs
2021-06-04 13:22:59 +08:00

47 lines
1.8 KiB
C#

using Assets.Scripts.Devices.Ble.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assets.Scripts.Devices.Ble.Characteristic
{
public static class ICharacteristicExtensions
{
public static void HandleByteAttributeValue(this ICharacteristic characteristic, byte[] attribute, int offset, byte? subject)
{
if (attribute.Length < 1 + offset)
throw new ArgumentException("attribute");
byte num = attribute[offset];
subject =new byte?(num);
}
public static void HandleShortAttributeValue(this ICharacteristic characteristic, byte[] attribute, int offset, short? subject)
{
if (attribute.Length < 2 + offset)
throw new ArgumentException("attribute");
short num = BitConverter.ToInt16(attribute, offset);
subject= new short?(num);
}
public static void HandleUshortAttributeValue(this ICharacteristic characteristic, byte[] attribute, int offset, ushort? subject)
{
if (attribute.Length < 2 + offset)
throw new ArgumentException("attribute");
ushort num = BitConverter.ToUInt16(attribute, offset);
subject =new ushort?(num);
}
public static void HandleUint24AttributeValue(this ICharacteristic characteristic, byte[] attribute, int offset, uint? subject)
{
if (attribute.Length < 4 + offset - 1)
throw new ArgumentException("attribute");
byte[] numArray = new byte[4];
Buffer.BlockCopy((Array)attribute, offset, (Array)numArray, 0, 3);
uint num = BitConverter.ToUInt32(numArray, 0);
subject = (new uint?(num));
}
}
}