接入蓝牙基本结构
This commit is contained in:
1 parent
84e8b50bbf
commit
2f84f70a5f
152 files changed
+6426
-56
No files matched your search
@@ -0,0 +1,190 @@
|
||||
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 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<BleWinHwInterface, BleCharacteristicInfo, BleResponse<byte[]>>> registeredCharacteristicReadEvents = new Dictionary<Guid, Action<BleWinHwInterface, 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(BleWinHwInterface 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(BleWinHwInterface 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<BleWinHwInterface, BleCharacteristicInfo, BleResponse> notificationCallback,
|
||||
Action<BleWinHwInterface, 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 927b28f8d3c950b4793567e37f28d27c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in new issue
Block a user