diff --git a/Assets/Scripts/Devices/Ble/BleDevice.cs b/Assets/Scripts/Devices/Ble/BleDevice.cs index 6b634c20..5b799534 100644 --- a/Assets/Scripts/Devices/Ble/BleDevice.cs +++ b/Assets/Scripts/Devices/Ble/BleDevice.cs @@ -17,7 +17,7 @@ namespace Assets.Scripts.Devices.Ble public abstract class BleDevice : AbstractDevice { - protected BleWinHwInterface hwInterface; + protected IBleWinHwInterface hwInterface; protected BlePeripheralInfo peripheralInfo; private readonly HashSet services = new HashSet(); @@ -32,7 +32,7 @@ namespace Assets.Scripts.Devices.Ble } public List Characteristics { get; protected set; } = new List(); - public BleDevice(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface, SensorType sensor) : base(peripheralInfo.Address, NetworkType.BLE) + public BleDevice(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface, SensorType sensor) : base(peripheralInfo.Address, NetworkType.BLE) { this.hwInterface = bleWinHwInterface; this.hwInterface.BluetoothStateChangedEvent += BluetoothStateChangedEvent; @@ -50,7 +50,7 @@ namespace Assets.Scripts.Devices.Ble Characteristics.Add(new ModelNumberCharacteristic()); } - private void HwInterface_PeripheralDisconnectedEvent(BleWinHwInterface hwInterface, BlePeripheralInfo peripheral, BleResponse response, bool manualDisconnect) + private void HwInterface_PeripheralDisconnectedEvent(IBleWinHwInterface hwInterface, BlePeripheralInfo peripheral, BleResponse response, bool manualDisconnect) { if (!peripheral.MatchAddress(this.peripheralInfo.Address)) { @@ -93,7 +93,7 @@ namespace Assets.Scripts.Devices.Ble } } - private void PeripheralConnectedAction(BleWinHwInterface hwInterface, BlePeripheralInfo sender, BleResponse response) + private void PeripheralConnectedAction(IBleWinHwInterface hwInterface, BlePeripheralInfo sender, BleResponse response) { Debug.Log($"连接{ this.Name }"+response.IsSuccess); if (response.IsSuccess) @@ -136,7 +136,7 @@ namespace Assets.Scripts.Devices.Ble - private void ServicesDiscoveredAction(BleWinHwInterface hwInterface, BlePeripheralInfo sender, BleResponse> response) + private void ServicesDiscoveredAction(IBleWinHwInterface hwInterface, BlePeripheralInfo sender, BleResponse> response) { //Debug.Log("搜索service"); if(!response.IsSuccess || !response.Data.Any()) @@ -211,7 +211,7 @@ namespace Assets.Scripts.Devices.Ble { } - private void BluetoothStateChangedEvent(BleWinHwInterface hwInterface, BleState state) + private void BluetoothStateChangedEvent(IBleWinHwInterface hwInterface, BleState state) { switch (state) { diff --git a/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs b/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs index 823b439b..b32faaae 100644 --- a/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs +++ b/Assets/Scripts/Devices/Ble/BleDeviceAdapter.cs @@ -1,6 +1,7 @@ using Assets.Scripts.Ble; using Assets.Scripts.Ble.Service; using Assets.Scripts.Devices.Ble.Devices; +using Assets.Scripts.Devices.Ble.Interfaces; using Assets.Scripts.UI.Prefab.Device; using System; using System.Collections.Generic; @@ -17,15 +18,16 @@ namespace Assets.Scripts.Devices.Ble private IDictionary discoveredDevices = new Dictionary(); - private BleWinHwInterface hwInterface; - public BleDeviceAdapter() + private IBleWinHwInterface hwInterface { get; set; } + public BleDeviceAdapter(IBleWinHwInterface bleWinHwInterface) { - hwInterface = BleWinHwInterface.GetInterface(); + + hwInterface = bleWinHwInterface;// BleWinHwInterface.GetInterface(); hwInterface.BluetoothStateChangedEvent += HwInterface_BluetoothStateChangedEvent; } - private void HwInterface_BluetoothStateChangedEvent(BleWinHwInterface hwInterface, BleState bleState) + private void HwInterface_BluetoothStateChangedEvent(IBleWinHwInterface hwInterface, BleState bleState) { //Debug.Log("22222222222222" + bleState); if(bleState == BleState.Off) diff --git a/Assets/Scripts/Devices/Ble/BluetoothStateChangedCallback.cs b/Assets/Scripts/Devices/Ble/BluetoothStateChangedCallback.cs index c8fd1484..7a038776 100644 --- a/Assets/Scripts/Devices/Ble/BluetoothStateChangedCallback.cs +++ b/Assets/Scripts/Devices/Ble/BluetoothStateChangedCallback.cs @@ -1,4 +1,5 @@ -using System; +using Assets.Scripts.Devices.Ble.Interfaces; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,6 +7,6 @@ using System.Threading.Tasks; namespace Assets.Scripts.Ble { - public delegate void BluetoothStateChangedCallback(BleWinHwInterface hwInterface, BleState bleState); + public delegate void BluetoothStateChangedCallback(IBleWinHwInterface hwInterface, BleState bleState); } diff --git a/Assets/Scripts/Devices/Ble/CharacteristicReadCallback.cs b/Assets/Scripts/Devices/Ble/CharacteristicReadCallback.cs index 9328c12a..b81a34fe 100644 --- a/Assets/Scripts/Devices/Ble/CharacteristicReadCallback.cs +++ b/Assets/Scripts/Devices/Ble/CharacteristicReadCallback.cs @@ -1,4 +1,5 @@ using Assets.Scripts.Devices.Ble; +using Assets.Scripts.Devices.Ble.Interfaces; using System; using System.Collections.Generic; using System.Linq; @@ -7,5 +8,5 @@ using System.Threading.Tasks; namespace Assets.Scripts.Ble { - public delegate void CharacteristicReadCallback(BleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response); + public delegate void CharacteristicReadCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response); } diff --git a/Assets/Scripts/Devices/Ble/CharacteristicsDiscoveredCallback.cs b/Assets/Scripts/Devices/Ble/CharacteristicsDiscoveredCallback.cs index d3fab0e7..d32563d2 100644 --- a/Assets/Scripts/Devices/Ble/CharacteristicsDiscoveredCallback.cs +++ b/Assets/Scripts/Devices/Ble/CharacteristicsDiscoveredCallback.cs @@ -1,4 +1,5 @@ using Assets.Scripts.Devices.Ble; +using Assets.Scripts.Devices.Ble.Interfaces; using System; using System.Collections.Generic; using System.Linq; @@ -7,5 +8,5 @@ using System.Threading.Tasks; namespace Assets.Scripts.Ble { - public delegate void CharacteristicsDiscoveredCallback(BleWinHwInterface hwInterface, BleServiceInfo service, BleResponse> response); + public delegate void CharacteristicsDiscoveredCallback(IBleWinHwInterface hwInterface, BleServiceInfo service, BleResponse> response); } diff --git a/Assets/Scripts/Devices/Ble/Devices/CyclingPower.cs b/Assets/Scripts/Devices/Ble/Devices/CyclingPower.cs index 23bbca0b..097080b8 100644 --- a/Assets/Scripts/Devices/Ble/Devices/CyclingPower.cs +++ b/Assets/Scripts/Devices/Ble/Devices/CyclingPower.cs @@ -1,6 +1,7 @@ using Assets.Scripts.Ble; using Assets.Scripts.Devices.Ant.Interfaces; using Assets.Scripts.Devices.Ble.Characteristic; +using Assets.Scripts.Devices.Ble.Interfaces; using System; using System.Collections.Generic; using System.Linq; @@ -14,7 +15,7 @@ namespace Assets.Scripts.Devices.Ble.Devices { private List Services; private CyclingPowerMeasurement cyclingPowerMeasurement; - public CyclingPower(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Power) + public CyclingPower(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Power) { Priority = 2; cyclingPowerMeasurement = new CyclingPowerMeasurement(1920); @@ -49,7 +50,7 @@ namespace Assets.Scripts.Devices.Ble.Devices } - private void CharacteristicReadMainCallback(BleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) + private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) { //Debug.Log("power main call" + string.Join(",", response.Data)); diff --git a/Assets/Scripts/Devices/Ble/Devices/Ftms.cs b/Assets/Scripts/Devices/Ble/Devices/Ftms.cs index 5cc33535..1fb5db56 100644 --- a/Assets/Scripts/Devices/Ble/Devices/Ftms.cs +++ b/Assets/Scripts/Devices/Ble/Devices/Ftms.cs @@ -1,6 +1,7 @@ using Assets.Scripts.Ble; using Assets.Scripts.Devices.Ant.Interfaces; using Assets.Scripts.Devices.Ble.Characteristic; +using Assets.Scripts.Devices.Ble.Interfaces; using System; using System.Collections.Generic; using System.Linq; @@ -21,7 +22,7 @@ namespace Assets.Scripts.Devices.Ble.Devices private List Services; private BleCharacteristicInfo controlPointCharacteristic; - public Ftms(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) :base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Trainer) + public Ftms(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface) :base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Trainer) { this._ftmsIndoorBikeData = new FtmsIndoorBikeData(); base.Characteristics.Add(this._ftmsIndoorBikeData); @@ -60,7 +61,7 @@ namespace Assets.Scripts.Devices.Ble.Devices } } - private void CharacteristicReadMainCallback(BleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) + private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) { foreach (var item in base.Characteristics) { diff --git a/Assets/Scripts/Devices/Ble/Devices/HeartRate.cs b/Assets/Scripts/Devices/Ble/Devices/HeartRate.cs index c84781d1..5ce8abf3 100644 --- a/Assets/Scripts/Devices/Ble/Devices/HeartRate.cs +++ b/Assets/Scripts/Devices/Ble/Devices/HeartRate.cs @@ -1,6 +1,7 @@ using Assets.Scripts.Ble; using Assets.Scripts.Devices.Ant.Interfaces; using Assets.Scripts.Devices.Ble.Characteristic; +using Assets.Scripts.Devices.Ble.Interfaces; using System; using System.Collections.Generic; using System.Linq; @@ -16,7 +17,7 @@ namespace Assets.Scripts.Devices.Ble.Devices private List Services; private HeartRateMeasurement heartRateMeasurement; - public HeartRate(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.HeartRate) + public HeartRate(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.HeartRate) { Priority = 2; heartRateMeasurement = new HeartRateMeasurement(); @@ -73,7 +74,7 @@ namespace Assets.Scripts.Devices.Ble.Devices }); } - private void CharacteristicReadMainCallback(BleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) + private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) { //Debug.Log("heart rate main call" + string.Join(",", response.Data)); diff --git a/Assets/Scripts/Devices/Ble/Devices/SpeedCadence.cs b/Assets/Scripts/Devices/Ble/Devices/SpeedCadence.cs index 504a4fd0..281d4546 100644 --- a/Assets/Scripts/Devices/Ble/Devices/SpeedCadence.cs +++ b/Assets/Scripts/Devices/Ble/Devices/SpeedCadence.cs @@ -1,6 +1,7 @@ using Assets.Scripts.Ble; using Assets.Scripts.Devices.Ant.Interfaces; using Assets.Scripts.Devices.Ble.Characteristic; +using Assets.Scripts.Devices.Ble.Interfaces; using System; using System.Collections.Generic; using System.Linq; @@ -18,7 +19,7 @@ namespace Assets.Scripts.Devices.Ble.Devices private double _wheelCircumference; private List Services; - public SpeedCadence(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.SpeedCadence) + public SpeedCadence(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.SpeedCadence) { Debug.Log("创建速度踏频设备"); Priority = 1; @@ -81,7 +82,7 @@ namespace Assets.Scripts.Devices.Ble.Devices } - private void CharacteristicReadMainCallback(BleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) + private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) { //Debug.Log("power main call" + string.Join(",", response.Data)); diff --git a/Assets/Scripts/Devices/Ble/Devices/Tacx.cs b/Assets/Scripts/Devices/Ble/Devices/Tacx.cs index 39333b15..5c6c1e94 100644 --- a/Assets/Scripts/Devices/Ble/Devices/Tacx.cs +++ b/Assets/Scripts/Devices/Ble/Devices/Tacx.cs @@ -2,6 +2,7 @@ using Assets.Scripts.Devices.Ant; using Assets.Scripts.Devices.Ant.Interfaces; using Assets.Scripts.Devices.Ble.Characteristic; +using Assets.Scripts.Devices.Ble.Interfaces; using System; using System.Collections.Generic; using System.Linq; @@ -30,7 +31,7 @@ namespace Assets.Scripts.Devices.Ble.Devices /// private double _grade = 0; - public Tacx(BlePeripheralInfo peripheralInfo, BleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Trainer) + public Tacx(BlePeripheralInfo peripheralInfo, IBleWinHwInterface bleWinHwInterface) : base(peripheralInfo, bleWinHwInterface, Ant.SensorType.Trainer) { tacxFecNotify = new TacxFecNotify(); base.Characteristics.Add(tacxFecNotify); @@ -75,7 +76,7 @@ namespace Assets.Scripts.Devices.Ble.Devices } } - private void CharacteristicReadMainCallback(BleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) + private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) { foreach (var item in base.Characteristics) { diff --git a/Assets/Scripts/Devices/Ble/HeartRate/HeartRateService.cs b/Assets/Scripts/Devices/Ble/HeartRate/HeartRateService.cs index dc9f5fef..00bb791b 100644 --- a/Assets/Scripts/Devices/Ble/HeartRate/HeartRateService.cs +++ b/Assets/Scripts/Devices/Ble/HeartRate/HeartRateService.cs @@ -2,6 +2,7 @@ using Assets.Scripts.Ble.Service; using Assets.Scripts.Commands; using Assets.Scripts.Devices.Ble; +using Assets.Scripts.Devices.Ble.Interfaces; using System; using System.Collections.Generic; using System.Linq; @@ -36,12 +37,12 @@ namespace Assets.Scripts.Ble.HeartRate } } - private void HrmCharacteristicSubscribed(BleWinHwInterface winHwInterface, BleCharacteristicInfo characteristic, BleResponse response) + private void HrmCharacteristicSubscribed(IBleWinHwInterface winHwInterface, BleCharacteristicInfo characteristic, BleResponse response) { } - private void HrmCharacteristicRead(BleWinHwInterface winHwInterface, BleCharacteristicInfo characteristic, BleResponse response) + private void HrmCharacteristicRead(IBleWinHwInterface winHwInterface, BleCharacteristicInfo characteristic, BleResponse response) { HeartRateMeasurementValue heartRateMeasurementValue; if(base.CheckCharacteristicResponse(characteristic, response, (BleCharacteristicInfo info, BleResponse bleResponse) => new HeartRateMeasurementValue(bleResponse.Data), out heartRateMeasurementValue) != CommandResponseCode.Success) diff --git a/Assets/Scripts/Devices/Ble/Interfaces/IBleWinHwInterface.cs b/Assets/Scripts/Devices/Ble/Interfaces/IBleWinHwInterface.cs new file mode 100644 index 00000000..dc500079 --- /dev/null +++ b/Assets/Scripts/Devices/Ble/Interfaces/IBleWinHwInterface.cs @@ -0,0 +1,28 @@ +using Assets.Scripts.Ble; +using Assets.Scripts.Ble.Scan; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Assets.Scripts.Devices.Ble.Interfaces +{ + public interface IBleWinHwInterface + { + void StartScan(Action discoveredCallBack); + void ConnectPeripheral(BlePeripheralInfo info, Action callback); + void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback); + void DiscoverServices(BlePeripheralInfo peripheral, Action>> callback); + void DiscoverCharacteristic(BleServiceInfo service, CharacteristicsDiscoveredCallback callback); + void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action callback); + void WriteCharacteristic(BleCharacteristicInfo characteristic, byte[] data); + void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback); + void Dispose(); + BleState BleState { get; set; } + Dictionary pCache { get; set; } + event BluetoothStateChangedCallback BluetoothStateChangedEvent; + event CharacteristicReadCallback CharacteristicReadEvent; + event PeripheralDisconnectedCallback PeripheralDisconnectedEvent; + } +} diff --git a/Assets/Scripts/Devices/Ble/Interfaces/IBleWinHwInterface.cs.meta b/Assets/Scripts/Devices/Ble/Interfaces/IBleWinHwInterface.cs.meta new file mode 100644 index 00000000..77926020 --- /dev/null +++ b/Assets/Scripts/Devices/Ble/Interfaces/IBleWinHwInterface.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 86216427171ac784296ca577b921b2e7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Devices/Ble/PeripheralDisconnectedCallback.cs b/Assets/Scripts/Devices/Ble/PeripheralDisconnectedCallback.cs index 3b24a05b..e6211017 100644 --- a/Assets/Scripts/Devices/Ble/PeripheralDisconnectedCallback.cs +++ b/Assets/Scripts/Devices/Ble/PeripheralDisconnectedCallback.cs @@ -1,4 +1,5 @@ using Assets.Scripts.Ble; +using Assets.Scripts.Devices.Ble.Interfaces; using System; using System.Collections.Generic; using System.Linq; @@ -7,5 +8,5 @@ using System.Threading.Tasks; namespace Assets.Scripts.Devices.Ble { - public delegate void PeripheralDisconnectedCallback(BleWinHwInterface hwInterface, BlePeripheralInfo peripheral, BleResponse response, bool manualDisconnect); + public delegate void PeripheralDisconnectedCallback(IBleWinHwInterface hwInterface, BlePeripheralInfo peripheral, BleResponse response, bool manualDisconnect); } diff --git a/Assets/Scripts/Devices/Ble/Service/BleService.cs b/Assets/Scripts/Devices/Ble/Service/BleService.cs index 40a0f123..b8679d82 100644 --- a/Assets/Scripts/Devices/Ble/Service/BleService.cs +++ b/Assets/Scripts/Devices/Ble/Service/BleService.cs @@ -8,6 +8,7 @@ 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 @@ -18,7 +19,7 @@ namespace Assets.Scripts.Ble.Service private BleServiceInfo serviceInfo; private List characteristics; - private readonly Dictionary>> registeredCharacteristicReadEvents = new Dictionary>>(); + private readonly Dictionary>> registeredCharacteristicReadEvents = new Dictionary>>(); public IReadOnlyList Characteristics { @@ -58,7 +59,7 @@ namespace Assets.Scripts.Ble.Service this.hwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback; } - private void CharacteristicReadMainCallback(BleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) + private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse response) { //Debug.Log("read main callback"); if (this.registeredCharacteristicReadEvents.ContainsKey(characteristic.Id) && characteristic.Service.Equals(this.serviceInfo)) @@ -79,7 +80,7 @@ namespace Assets.Scripts.Ble.Service this.CheckCharacteristicDiscoveredResponse(this.hwInterface, this.serviceInfo, scanCharacteristicsResponse); } - private void CheckCharacteristicDiscoveredResponse(BleWinHwInterface hwInterface, BleServiceInfo service, BleResponse> response) + private void CheckCharacteristicDiscoveredResponse(IBleWinHwInterface hwInterface, BleServiceInfo service, BleResponse> response) { if (!service.Equals(this.serviceInfo)) { @@ -98,8 +99,8 @@ namespace Assets.Scripts.Ble.Service protected abstract void CharacteristicsDiscovered(BleResponse> response); - protected void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action notificationCallback, - Action> readCallback) + protected void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action notificationCallback, + Action> readCallback) { this.hwInterface.SubscribeCharacteristic(characteristic, (hw, info, response) => { diff --git a/Assets/Scripts/Devices/Ble/Win/BleWinHwInterface.cs b/Assets/Scripts/Devices/Ble/Win/BleWinHwInterface.cs index 96576511..794db60f 100644 --- a/Assets/Scripts/Devices/Ble/Win/BleWinHwInterface.cs +++ b/Assets/Scripts/Devices/Ble/Win/BleWinHwInterface.cs @@ -2,6 +2,7 @@ using Assets.Scripts.Ble.Win; using Assets.Scripts.Devices.Ant; using Assets.Scripts.Devices.Ble; +using Assets.Scripts.Devices.Ble.Interfaces; using Assets.Scripts.Devices.Ble.Win; using System; using System.Collections.Generic; @@ -12,13 +13,24 @@ using UnityEngine; namespace Assets.Scripts.Ble { - public sealed class BleWinHwInterface + public sealed class BleWinHwInterface: IBleWinHwInterface { private static BleWinHwInterface hwInterface; private WclBleMainThread wclBleMainThread; - public readonly Dictionary pCache = new Dictionary(); - + private Dictionary _pCache; + public Dictionary pCache + { + get + { + if (_pCache == null) + { + _pCache = new Dictionary(); + } + return _pCache; + } + set { _pCache = value; } + } private Action _discoveredCallback; private Dictionary> callbacks = new Dictionary>(); @@ -43,6 +55,7 @@ namespace Assets.Scripts.Ble } } + private BluetoothStateChangedCallback bluetoothStateChanged; public event BluetoothStateChangedCallback BluetoothStateChangedEvent { @@ -55,7 +68,7 @@ namespace Assets.Scripts.Ble this.bluetoothStateChanged -= value; } } - + private BleState nativeState; public BleState BleState { @@ -63,7 +76,7 @@ namespace Assets.Scripts.Ble { return this.nativeState; } - private set + set { if(this.nativeState != value) { @@ -201,7 +214,7 @@ namespace Assets.Scripts.Ble return wclBleGattThread; } - internal void ConnectPeripheral(BlePeripheralInfo info, Action callback) + public void ConnectPeripheral(BlePeripheralInfo info, Action callback) { this.callbacks.Add(info, callback); WclBleGattThread wclBleGattThread = this.wclBleMainThread.GetGattThread(info); @@ -229,8 +242,8 @@ namespace Assets.Scripts.Ble //}); } - - internal void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback) + + public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback) { var gattThread = this.wclBleMainThread.GetGattThread(peripheral); if(gattThread != null && gattThread.CanLoadWork) @@ -372,7 +385,7 @@ namespace Assets.Scripts.Ble } } - public void DiscoverServices(BlePeripheralInfo peripheral, Action>> callback) + public void DiscoverServices(BlePeripheralInfo peripheral, Action>> callback) { WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(peripheral); if(gattThread == null) @@ -420,7 +433,7 @@ namespace Assets.Scripts.Ble this.GattCharacteristicsDiscovered(gattThread, service, response); } - public void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action callback) + public void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action callback) { WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral); diff --git a/Assets/Scripts/Devices/Ble/mobile.meta b/Assets/Scripts/Devices/Ble/mobile.meta new file mode 100644 index 00000000..95152633 --- /dev/null +++ b/Assets/Scripts/Devices/Ble/mobile.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9fa2f8906f5360c47b807eaef8d42263 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs b/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs new file mode 100644 index 00000000..dbbda567 --- /dev/null +++ b/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs @@ -0,0 +1,525 @@ +using Assets.Scripts.Ble.Scan; +using Assets.Scripts.Ble.Win; +using Assets.Scripts.Devices.Ant; +using Assets.Scripts.Devices.Ble; +using Assets.Scripts.Devices.Ble.Interfaces; +using Assets.Scripts.Devices.Ble.Win; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace Assets.Scripts.Ble +{ + public sealed class BleMobileInterface: IBleWinHwInterface + { + private static BleMobileInterface hwInterface; + private WclBleMainThread wclBleMainThread; + + private Dictionary _pCache; + public Dictionary pCache + { + get + { + if (_pCache == null) + { + _pCache = new Dictionary(); + } + return _pCache; + } + set { _pCache = value; } + } + private Action _discoveredCallback; + + private Dictionary> callbacks = new Dictionary>(); + private Dictionary>>> servicesCallbacks = new Dictionary>>>(); + //private Dictionary>>> characteristicsCallbacks = new Dictionary>>>(); + private Dictionary> characteristicNotificationCallbacks = new Dictionary>(); + private Dictionary characteristicReadCallbacks = new Dictionary(); + private Dictionary characteristicsDiscoveredCallbacks = new Dictionary(); + + private Dictionary disconnectedCallback = new Dictionary(); + + private CharacteristicReadCallback characteristicReadEvent; + public event CharacteristicReadCallback CharacteristicReadEvent + { + add + { + this.characteristicReadEvent += value; + } + remove + { + this.characteristicReadEvent -= value; + } + } + + + private BluetoothStateChangedCallback bluetoothStateChanged; + public event BluetoothStateChangedCallback BluetoothStateChangedEvent + { + add + { + this.bluetoothStateChanged += value; + } + remove + { + this.bluetoothStateChanged -= value; + } + } + + private BleState nativeState; + public BleState BleState + { + get + { + return this.nativeState; + } + set + { + if(this.nativeState != value) + { + this.nativeState = value; + this.bluetoothStateChanged?.Invoke(this, this.nativeState); + } + } + } + + private PeripheralDisconnectedCallback peripheralDisconnectedEvent; + public event PeripheralDisconnectedCallback PeripheralDisconnectedEvent + { + add + { + this.peripheralDisconnectedEvent += value; + } + remove + { + this.peripheralDisconnectedEvent -= value; + } + } + + private BleMobileInterface() + { + wclBleMainThread = new WclBleMainThread(); + wclBleMainThread.ManagerStatusChanged += ManagerStatusChanged; + wclBleMainThread.ScanInfoReceived += WatcherScanInfoReceived; + //wclBleMainThread.gatt + wclBleMainThread.Start(); + + } + + private void WatcherScanInfoReceived(WclBleMainThread sender, long address, string name, int rssi, CPPBridge.WclBleAdvertisementType packetType, Guid? service) + { + //if(address != 224160707349234) + //{ + //return; + //} + //Debug.Log($"address:{ address }, name:{ name }, service:{ (service == null ? "" : service.Value.ToString()) }"); + + SensorType sensor = SensorType.None; + List services = new List(); + if (service != null) + { + services = new List { service.Value }; + foreach(var item in ServiceUuids.Services) + { + if(item.IdGuid != service.Value) + { + continue; + } + if(item.IdByteArray == ServiceUuids.Ftms) + { + sensor = SensorType.Trainer; + } + else if(item.IdByteArray == ServiceUuids.HeartRate) + { + sensor = SensorType.HeartRate; + } + else if(item.IdByteArray == ServiceUuids.CyclingPower) + { + sensor = SensorType.Power; + //sensor = SensorType.Trainer; + } + else if(item.IdByteArray == ServiceUuids.CyclingSpeedCadence) + { + sensor = SensorType.SpeedCadence; + } + else if(item.IdByteArray == ServiceUuids.TacxBle) + { + sensor = SensorType.Trainer; + } + } + }; + var addressStr = address.ToString("X12"); + if (!pCache.ContainsKey(addressStr)) + { + var device = new BleAdvertisementInfo(new WinBlePeripheralInfo(addressStr, name), rssi, true, services, null, sensor); + pCache.Add(addressStr, device); + + //WclBleGattThread gattClient = this.SetUpGattClient(device.Peripheral); + //this.ConnectInternal(gattClient); + } + if (!string.IsNullOrWhiteSpace(name)) + { + (pCache[addressStr].Peripheral as WinBlePeripheralInfo).SetName(name); + } + if(sensor == SensorType.None) + { + return; + } + pCache[addressStr].SensorType = sensor; + pCache[addressStr].Rssi = rssi; + //Debug.Log(sensor); + //pCache[address.ToString()].SensorType = sensor; + + foreach (var item in services) + { + pCache[addressStr].TryAddService(item); + } + + pCache[addressStr].Index++; + if(pCache[addressStr].SensorType == SensorType.Power && pCache[addressStr].Services.Any(s=> s.Equals(ServiceUuids.Get(ServiceUuids.TacxBle).IdGuid))) + { + pCache[addressStr].SensorType = SensorType.Trainer; + //Debug.Log("纠正为trainer, "+ pCache[address.ToString()].Index); + } + + + if (pCache[addressStr].SensorType == SensorType.Power) + { + if (pCache[addressStr].Index > 4) + { + _discoveredCallback?.Invoke(pCache[addressStr]); + } + } + else if(pCache[addressStr].SensorType != SensorType.None) + { + _discoveredCallback?.Invoke(pCache[addressStr]); + } + } + + private WclBleGattThread SetUpGattClient(BlePeripheralInfo peripheral) + { + WclBleGattThread wclBleGattThread = this.wclBleMainThread.CreateGattThread(peripheral); + wclBleGattThread.GattConnected += this.GattConnected; + wclBleGattThread.GattDisconnected += this.GattDisconnected; + wclBleGattThread.GattServicesDiscovered += this.GattServicesDiscovered; + wclBleGattThread.GattCharacteristicsDiscovered += this.GattCharacteristicsDiscovered; + wclBleGattThread.GattCharacteristicSubscribed += this.GattCharacteristicSubscribed; + wclBleGattThread.GattCharacteristicRead += this.GattCharacteristicRead; + wclBleGattThread.GattCharacteristicWrote += this.GattCharacteristicWrote; + wclBleGattThread.GattCharacteristicChanged += this.GattCharacteristicChanged; + wclBleGattThread.Start(); + return wclBleGattThread; + } + + public void ConnectPeripheral(BlePeripheralInfo info, Action callback) + { + this.callbacks.Add(info, callback); + WclBleGattThread wclBleGattThread = this.wclBleMainThread.GetGattThread(info); + + if(wclBleGattThread == null) + { + + wclBleGattThread = this.SetUpGattClient(info); + this.ConnectInternal(wclBleGattThread); + return; + } + else + { + this.ConnectInternal(wclBleGattThread); + } + } + + private void ConnectInternal(WclBleGattThread gattClient) + { + //Task.Run(() => + //{ + int num = gattClient.Connect(); + Debug.Log("连接设备返回" + num); + + + //}); + } + + public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback) + { + var gattThread = this.wclBleMainThread.GetGattThread(peripheral); + if(gattThread != null && gattThread.CanLoadWork) + { + this.callbacks.Remove(peripheral); + this.servicesCallbacks.Remove(peripheral); + this.characteristicNotificationCallbacks.Remove(peripheral); + + this.disconnectedCallback.Add(peripheral, callback); + gattThread.Discounect(); + } + } + + + public static BleMobileInterface GetInterface() + { + if(hwInterface == null) + { + hwInterface = new BleMobileInterface(); + } + + return hwInterface; + } + + public void StartScan(Action discoveredCallBack) + { + pCache.Clear(); + _discoveredCallback = discoveredCallBack; + this.wclBleMainThread.StartWatcher(); + } + + private void ManagerStatusChanged(WclBleMainThread sender, WclBleManagerStatus status) + { + this.BleState = BleMobileInterface.StateFromNativeState(status); + //Debug.Log("win hw:" + status); + + } + + private void GattConnected(WclBleGattThread gattClient, BleResponse response) + { + Debug.Log($"gatt connected { response.ToString() }"); + if (!response.IsSuccess) + { + gattClient.Stop(); + + this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response); + this.callbacks.Remove(gattClient.Peripheral); + return; + } + this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response); + this.callbacks.Remove(gattClient.Peripheral); + } + + private void CleanUpPeripheral(BlePeripheralInfo peripheralInfo) + { + //this.callbacks.Clear() + } + + private void GattDisconnected(WclBleGattThread gattClient, BleResponse response) + { + Debug.Log($"gatt disconnected { gattClient.Peripheral.Name }"); + + this.callbacks.Remove(gattClient.Peripheral); + this.servicesCallbacks.Remove(gattClient.Peripheral); + this.characteristicNotificationCallbacks.Remove(gattClient.Peripheral); + //App.MainDeviceAdapter.PrintStatus(); + + var manualDisconnect = disconnectedCallback.ContainsKey(gattClient.Peripheral); + this.peripheralDisconnectedEvent(this, gattClient.Peripheral, response, manualDisconnect); + + //App.MainDeviceAdapter.PrintStatus(); + if (disconnectedCallback.ContainsKey(gattClient.Peripheral)) + { + disconnectedCallback[gattClient.Peripheral].Invoke(); + disconnectedCallback.Remove(gattClient.Peripheral); + } + //this.pCache.Remove(gattClient.Peripheral.Address); + + + + } + private void GattServicesDiscovered(WclBleGattThread gattClient, BleResponse> response) + { + //Debug.Log("services discovered"); + //this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response); + + //foreach (var item in response.Data) + //{ + // Debug.Log(item.ToString()); + //} + + if (this.servicesCallbacks.ContainsKey(gattClient.Peripheral)) + { + this.servicesCallbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response); + } + } + + private void GattCharacteristicsDiscovered(WclBleGattThread gattClient, BleServiceInfo service, BleResponse> response) + { + //Debug.Log("characteristics discovered"); + + if (this.characteristicsDiscoveredCallbacks.ContainsKey(service)) + { + this.characteristicsDiscoveredCallbacks[service].Invoke(this, service, response); + } + } + + private void GattCharacteristicSubscribed(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse response) + { + //Debug.Log("characteristics subscribed"); + if (this.characteristicNotificationCallbacks.ContainsKey(gattClient.Peripheral)) + { + this.characteristicNotificationCallbacks[gattClient.Peripheral].Invoke(this, characteristic, response); + } + } + private void GattCharacteristicRead(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse response) + { + Debug.Log("characteristic read"); + + if (this.characteristicReadCallbacks.ContainsKey(characteristic)) + { + this.characteristicReadCallbacks[characteristic].Invoke(this, characteristic, response); + this.characteristicReadCallbacks.Remove(characteristic); + } + + this.characteristicReadEvent.Invoke(this, characteristic, response); + } + + private void GattCharacteristicWrote(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse response) + { + Debug.Log("characteristic wrote"); + } + private void GattCharacteristicChanged(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse response) + { + //Debug.Log("characteristic changed"); + if(this.wclBleMainThread.GetGattThread(characteristic.Peripheral) != null) + { + this.characteristicReadEvent.Invoke(this, characteristic, response); + } + } + + public void DiscoverServices(BlePeripheralInfo peripheral, Action>> callback) + { + WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(peripheral); + if(gattThread == null) + { + return; + } + + this.servicesCallbacks.Add(peripheral, callback); + int num = gattThread.DiscoverServices(); + if (WclBleErrors.IsSuccessCode(num)) + { + return; + } + + + BleResponse> response = new BleResponse> + { + IsSuccess = false, + Error = new BleHwInterfaceError(new int?(num), "WclBleGattClientErrorDomain", string.Format("Error discovering services - {0}", num)) + }; + this.GattServicesDiscovered(gattThread, response); + } + + public void DiscoverCharacteristic(BleServiceInfo service, CharacteristicsDiscoveredCallback callback) + { + WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(service.Peripheral); + if(gattThread == null) + { + return; + } + if (!this.characteristicsDiscoveredCallbacks.ContainsKey(service)) + { + this.characteristicsDiscoveredCallbacks.Add(service, callback); + } + int num = gattThread.DiscoverCharacteristics(service); + if (WclBleErrors.IsSuccessCode(num)) + { + return; + } + BleResponse> response = new BleResponse> + { + IsSuccess = false, + Error = new BleHwInterfaceError(new int?(num), "WclBleGattClientErrorDomain", string.Format("Error discovering characteristics - {0}", num)) + }; + this.GattCharacteristicsDiscovered(gattThread, service, response); + } + + public void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action callback) + { + WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral); + + this.characteristicNotificationCallbacks.Add(gattThread.Peripheral, callback); + int num = gattThread.SubscribeCharacteristic(characteristic); + if (WclBleErrors.IsSuccessCode(num)) + { + return; + } + BleResponse> response = new BleResponse> + { + IsSuccess = false, + Error = new BleHwInterfaceError(new int?(num), "WclBleGattClientErrorDomain", string.Format("Error subscribing characteristic - {0}", num)) + }; + this.GattCharacteristicSubscribed(gattThread, characteristic, response); + } + + public void WriteCharacteristic(BleCharacteristicInfo characteristic, byte[] data) + { + var gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral); + if(gattThread == null) + { + return; + } + + int num = gattThread.WriteCharacteristic(characteristic, data); + if (WclBleErrors.IsSuccessCode(num)) + { + Debug.Log("设置命令成功"); + return; + } + } + + + public void Dispose() + { + this.wclBleMainThread.ManagerStatusChanged -= ManagerStatusChanged; + this.wclBleMainThread.ScanInfoReceived -= WatcherScanInfoReceived; + this.wclBleMainThread.Stop(); + this.wclBleMainThread = null; + hwInterface = null; + pCache.Clear(); + } + + public void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback) + { + WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral); + if(gattThread == null) + { + return; + } + this.characteristicReadCallbacks.Add(characteristic, callback); + int num = gattThread.ReadCharacteristicValue(characteristic); + if (WclBleErrors.IsSuccessCode(num)) + { + return; + } + } + + private static BleState StateFromNativeState(WclBleManagerStatus status) + { + switch (status) + { + case WclBleManagerStatus.RadioOff: + return BleState.Off; + case WclBleManagerStatus.RadioOn: + return BleState.On; + case WclBleManagerStatus.Unknown: + return BleState.Unknown; + default: + return BleState.Unavailable; + } + } + + private class WinBlePeripheralInfo : BlePeripheralInfo + { + // Token: 0x06003F35 RID: 16181 RVA: 0x000E9FBF File Offset: 0x000E81BF + public WinBlePeripheralInfo(string address, string name) : base(address, name) + { + } + + // Token: 0x06003F36 RID: 16182 RVA: 0x000E9FC9 File Offset: 0x000E81C9 + public void SetName(string name) + { + base.Name = name; + } + } + } +} diff --git a/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs.meta b/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs.meta new file mode 100644 index 00000000..f9174596 --- /dev/null +++ b/Assets/Scripts/Devices/Ble/mobile/BleMobileInterface.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2e5257ea169e69a498a3e4d935a51306 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Devices/MainDeviceAdapter.cs b/Assets/Scripts/Devices/MainDeviceAdapter.cs index de5a621f..9768d5a6 100644 --- a/Assets/Scripts/Devices/MainDeviceAdapter.cs +++ b/Assets/Scripts/Devices/MainDeviceAdapter.cs @@ -1,4 +1,5 @@ -using Assets.Scripts.Devices.Ant; +using Assets.Scripts.Ble; +using Assets.Scripts.Devices.Ant; using Assets.Scripts.Devices.Ble; using System; using System.Collections.Generic; @@ -25,7 +26,11 @@ namespace Assets.Scripts.Devices private void CreateBleAdapter() { - adapters.Add(new BleDeviceAdapter()); +#if UNITY_IOS || UNITY_TVOS || UNITY_ANDROID + adapters.Add(new BleDeviceAdapter(BleWinHwInterface.GetInterface())); +#else + adapters.Add(new BleDeviceAdapter(BleWinHwInterface.GetInterface())); +#endif } public void StartScan()