192 lines
5.2 KiB
C#
192 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Assets.Scripts.Ble.Characteristic;
|
|
using Assets.Scripts.Ble.Commands;
|
|
using Assets.Scripts.Commands;
|
|
using Assets.Scripts.Devices.Ble;
|
|
using Assets.Scripts.Devices.Ble.Extension;
|
|
using Assets.Scripts.Devices.Ble.Interfaces;
|
|
using UnityEngine;
|
|
|
|
namespace Assets.Scripts.Ble.Service
|
|
{
|
|
public abstract class BleService
|
|
{
|
|
private BleWinHwInterface hwInterface;
|
|
private BleServiceInfo serviceInfo;
|
|
|
|
private List<BleCharacteristicInfo> characteristics;
|
|
private readonly Dictionary<Guid, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse<byte[]>>> registeredCharacteristicReadEvents = new Dictionary<Guid, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse<byte[]>>>();
|
|
|
|
public IReadOnlyList<BleCharacteristicInfo> Characteristics
|
|
{
|
|
get
|
|
{
|
|
return this.characteristics;
|
|
}
|
|
}
|
|
|
|
private CommandType supportedCommands;
|
|
|
|
// Token: 0x0400110D RID: 4365
|
|
private CommandType tempCommands;
|
|
|
|
public CommandType SupportedCommands
|
|
{
|
|
get
|
|
{
|
|
return this.supportedCommands;
|
|
}
|
|
private set
|
|
{
|
|
if (this.supportedCommands.HasFlag(value))
|
|
{
|
|
return;
|
|
}
|
|
this.supportedCommands |= value;
|
|
}
|
|
}
|
|
|
|
|
|
public BleService(BleServiceInfo serviceInfo, BleWinHwInterface bleHwInterface)
|
|
{
|
|
this.hwInterface = bleHwInterface;
|
|
this.serviceInfo = serviceInfo;
|
|
|
|
this.hwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;
|
|
}
|
|
|
|
private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
|
|
{
|
|
//Debug.Log("read main callback");
|
|
if (this.registeredCharacteristicReadEvents.ContainsKey(characteristic.Id) && characteristic.Service.Equals(this.serviceInfo))
|
|
{
|
|
//Debug.Log("read main callback 1111111111111");
|
|
this.registeredCharacteristicReadEvents[characteristic.Id](hwInterface, characteristic, response);
|
|
}
|
|
}
|
|
|
|
public void DiscoverCharacteristics(BleResponse<List<BleCharacteristicInfo>> scanCharacteristicsResponse = null)
|
|
{
|
|
if (scanCharacteristicsResponse == null)
|
|
{
|
|
Debug.Log("ccc ======================================== "+this.serviceInfo.Peripheral.Name);
|
|
this.hwInterface.DiscoverCharacteristic(this.serviceInfo, this.CheckCharacteristicDiscoveredResponse);
|
|
return;
|
|
}
|
|
this.CheckCharacteristicDiscoveredResponse(this.hwInterface, this.serviceInfo, scanCharacteristicsResponse);
|
|
}
|
|
|
|
private void CheckCharacteristicDiscoveredResponse(IBleWinHwInterface hwInterface, BleServiceInfo service, BleResponse<List<BleCharacteristicInfo>> response)
|
|
{
|
|
if (!service.Equals(this.serviceInfo))
|
|
{
|
|
return;
|
|
}
|
|
if (!response.IsSuccess)
|
|
{
|
|
return;
|
|
}
|
|
//if(response.Data)
|
|
|
|
this.characteristics = response.Data;
|
|
|
|
this.CharacteristicsDiscovered(response);
|
|
}
|
|
|
|
protected abstract void CharacteristicsDiscovered(BleResponse<List<BleCharacteristicInfo>> response);
|
|
|
|
protected void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse> notificationCallback,
|
|
Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse<byte[]>> readCallback)
|
|
{
|
|
this.hwInterface.SubscribeCharacteristic(characteristic, (hw, info, response) =>
|
|
{
|
|
if (readCallback != null)
|
|
{
|
|
this.registeredCharacteristicReadEvents[characteristic.Id] = readCallback;
|
|
Debug.Log("添加callback");
|
|
}
|
|
|
|
if (notificationCallback == null)
|
|
{
|
|
return;
|
|
}
|
|
notificationCallback.Invoke(hw, info, response);
|
|
});
|
|
}
|
|
|
|
protected Commands.CommandResponseCode CheckCharacteristicResponse<TResponseValue>(BleCharacteristicInfo characteristic, BleResponse<byte[]> response,
|
|
Func<BleCharacteristicInfo, BleResponse<byte[]>, TResponseValue> valueCreator, out TResponseValue value) where TResponseValue : BaseCharacteristicValue
|
|
{
|
|
value = default(TResponseValue);
|
|
|
|
if (!response.IsSuccess)
|
|
{
|
|
return response.Error.ToDeviceCommandResponseType();
|
|
}
|
|
|
|
try
|
|
{
|
|
value = valueCreator(characteristic, response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
throw;
|
|
}
|
|
|
|
CommandResponseCode commandResponseCode = value.ToDeviceCommandResponseCode();
|
|
|
|
return commandResponseCode;
|
|
}
|
|
|
|
protected void AddSupportedCommand(CommandType newCommands)
|
|
{
|
|
this.SupportedCommands |= newCommands;
|
|
|
|
if (newCommands.HasFlag(CommandType.SetSimParameters))
|
|
{
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback)
|
|
{
|
|
this.hwInterface.ReadCharacteristic(characteristic, callback);
|
|
}
|
|
|
|
protected BleCharacteristicInfo CheckServiceCanSendCommand(Command command)
|
|
{
|
|
CommandType type = command.Type;
|
|
//if (!this.SupportedCommands(type))
|
|
//{
|
|
|
|
//}
|
|
|
|
BleCharacteristicInfo characteristicForCommandType = this.GetCharacteristicForCommandType(type);
|
|
|
|
return characteristicForCommandType;
|
|
}
|
|
|
|
|
|
protected abstract BleCharacteristicInfo GetCharacteristicForCommandType(CommandType type);
|
|
|
|
public bool SupportsCommands(CommandType flags)
|
|
{
|
|
return this.SupportedCommands.HasFlag(flags);
|
|
}
|
|
|
|
public virtual bool SendCommand(Command command)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|