Merge remote-tracking branch 'origin/dev_mobile_ble' into dev_mobile

# Conflicts:
#	Assets/Plugins/Android/AndroidManifest.xml
#	Assets/Plugins/Android/AndroidManifest.xml.meta
#	Assets/Plugins/iOS.meta
#	Assets/Scripts/App.cs
#	Assets/Scripts/Devices/MainDeviceAdapter.cs
#	Assets/Scripts/Scenes/LoginController.cs
#	Assets/Scripts/Scenes/Ride/Scripts/PlayerController.cs
This commit is contained in:
Caiyanpeng committed 2021-08-25 10:23:51 +08:00
commit f155b2b627
60 files changed
+4602 -65

No files matched your search

+1 -1
View File
@@ -11,7 +11,7 @@ using Assets.Scripts.Devices;
public static class App
{
public static string Host = "http://192.168.0.101:5084/";
public static string Host = "http://192.168.0.101:5083/";
public static string AppVersion = Application.version;
+7 -7
View File
@@ -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<BleService> services = new HashSet<BleService>();
@@ -32,7 +32,7 @@ namespace Assets.Scripts.Devices.Ble
}
public List<ICharacteristic> Characteristics { get; protected set; } = new List<ICharacteristic>();
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)
@@ -123,7 +123,7 @@ namespace Assets.Scripts.Devices.Ble
Debug.Log("断开设备" + this.Name);
//App.MainDeviceAdapter.PrintStatus();
this.State = DeviceState.Disconnected;
this.hwInterface.DisconnectPeripheral(this.peripheralInfo, () => {
//App.MainDeviceAdapter.PrintStatus();
@@ -136,7 +136,7 @@ namespace Assets.Scripts.Devices.Ble
private void ServicesDiscoveredAction(BleWinHwInterface hwInterface, BlePeripheralInfo sender, BleResponse<List<BleServiceInfo>> response)
private void ServicesDiscoveredAction(IBleWinHwInterface hwInterface, BlePeripheralInfo sender, BleResponse<List<BleServiceInfo>> 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)
{
@@ -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<string, BleDevice> discoveredDevices = new Dictionary<string, BleDevice>();
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)
+6 -1
View File
@@ -37,5 +37,10 @@ namespace Assets.Scripts.Devices.Ble
{
return base.ToString() + "\n" + this.Data;
}
}
public static implicit operator BleResponse<T>(BleResponse<List<BleServiceInfo>> v)
{
throw new NotImplementedException();
}
}
}
@@ -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);
}
@@ -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<byte[]> response);
public delegate void CharacteristicReadCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response);
}
@@ -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<List<BleCharacteristicInfo>> response);
public delegate void CharacteristicsDiscoveredCallback(IBleWinHwInterface hwInterface, BleServiceInfo service, BleResponse<List<BleCharacteristicInfo>> response);
}
@@ -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<BleServiceInfo> 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<byte[]> response)
private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
//Debug.Log("power main call" + string.Join(",", response.Data));
+3 -2
View File
@@ -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<BleServiceInfo> 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<byte[]> response)
private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
foreach (var item in base.Characteristics)
{
@@ -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<BleServiceInfo> 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<byte[]> response)
private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
//Debug.Log("heart rate main call" + string.Join(",", response.Data));
@@ -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<BleServiceInfo> 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<byte[]> response)
private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
//Debug.Log("power main call" + string.Join(",", response.Data));
+3 -2
View File
@@ -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
/// </summary>
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<byte[]> response)
private void CharacteristicReadMainCallback(IBleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
{
foreach (var item in base.Characteristics)
{
@@ -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<byte[]> response)
private void HrmCharacteristicRead(IBleWinHwInterface 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)
@@ -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<BleAdvertisementInfo> discoveredCallBack);
void ConnectPeripheral(BlePeripheralInfo info, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse> callback);
void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback);
void DiscoverServices(BlePeripheralInfo peripheral, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>> callback);
void DiscoverCharacteristic(BleServiceInfo service, CharacteristicsDiscoveredCallback callback);
void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse> callback);
void WriteCharacteristic(BleCharacteristicInfo characteristic, byte[] data);
void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback);
void Dispose();
BleState BleState { get; set; }
Dictionary<string, BleAdvertisementInfo> pCache { get; set; }
event BluetoothStateChangedCallback BluetoothStateChangedEvent;
event CharacteristicReadCallback CharacteristicReadEvent;
event PeripheralDisconnectedCallback PeripheralDisconnectedEvent;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 86216427171ac784296ca577b921b2e7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -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);
}
@@ -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<BleCharacteristicInfo> characteristics;
private readonly Dictionary<Guid, Action<BleWinHwInterface, BleCharacteristicInfo, BleResponse<byte[]>>> registeredCharacteristicReadEvents = new Dictionary<Guid, Action<BleWinHwInterface, BleCharacteristicInfo, BleResponse<byte[]>>>();
private readonly Dictionary<Guid, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse<byte[]>>> registeredCharacteristicReadEvents = new Dictionary<Guid, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse<byte[]>>>();
public IReadOnlyList<BleCharacteristicInfo> Characteristics
{
@@ -58,7 +59,7 @@ namespace Assets.Scripts.Ble.Service
this.hwInterface.CharacteristicReadEvent += CharacteristicReadMainCallback;
}
private void CharacteristicReadMainCallback(BleWinHwInterface hwInterface, BleCharacteristicInfo characteristic, BleResponse<byte[]> response)
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))
@@ -79,7 +80,7 @@ namespace Assets.Scripts.Ble.Service
this.CheckCharacteristicDiscoveredResponse(this.hwInterface, this.serviceInfo, scanCharacteristicsResponse);
}
private void CheckCharacteristicDiscoveredResponse(BleWinHwInterface hwInterface, BleServiceInfo service, BleResponse<List<BleCharacteristicInfo>> response)
private void CheckCharacteristicDiscoveredResponse(IBleWinHwInterface hwInterface, BleServiceInfo service, BleResponse<List<BleCharacteristicInfo>> response)
{
if (!service.Equals(this.serviceInfo))
{
@@ -98,8 +99,8 @@ namespace Assets.Scripts.Ble.Service
protected abstract void CharacteristicsDiscovered(BleResponse<List<BleCharacteristicInfo>> response);
protected void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action<BleWinHwInterface, BleCharacteristicInfo, BleResponse> notificationCallback,
Action<BleWinHwInterface, BleCharacteristicInfo, BleResponse<byte[]>> readCallback)
protected void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse> notificationCallback,
Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse<byte[]>> readCallback)
{
this.hwInterface.SubscribeCharacteristic(characteristic, (hw, info, response) =>
{
@@ -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<string, BleAdvertisementInfo> pCache = new Dictionary<string, BleAdvertisementInfo>();
private Dictionary<string, BleAdvertisementInfo> _pCache;
public Dictionary<string, BleAdvertisementInfo> pCache
{
get
{
if (_pCache == null)
{
_pCache = new Dictionary<string, BleAdvertisementInfo>();
}
return _pCache;
}
set { _pCache = value; }
}
private Action<BleAdvertisementInfo> _discoveredCallback;
private Dictionary<BlePeripheralInfo, Action<BleWinHwInterface, BlePeripheralInfo, BleResponse>> callbacks = new Dictionary<BlePeripheralInfo, Action<BleWinHwInterface, BlePeripheralInfo, BleResponse>>();
@@ -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<BleWinHwInterface, BlePeripheralInfo, BleResponse> callback)
public void ConnectPeripheral(BlePeripheralInfo info, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse> 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<BleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>> callback)
public void DiscoverServices(BlePeripheralInfo peripheral, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>> 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<BleWinHwInterface, BleCharacteristicInfo, BleResponse> callback)
public void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse> callback)
{
WclBleGattThread gattThread = this.wclBleMainThread.GetGattThread(characteristic.Peripheral);
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9fa2f8906f5360c47b807eaef8d42263
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,399 @@
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 BleMobileThread bleMobileThread;
private Dictionary<string, BleAdvertisementInfo> _pCache;
public Dictionary<string, BleAdvertisementInfo> pCache
{
get
{
if (_pCache == null)
{
_pCache = new Dictionary<string, BleAdvertisementInfo>();
}
return _pCache;
}
set { _pCache = value; }
}
private Action<BleAdvertisementInfo> _discoveredCallback;
private Dictionary<BlePeripheralInfo, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse>> callbacks = new Dictionary<BlePeripheralInfo, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse>>();
private Dictionary<BlePeripheralInfo, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>>> servicesCallbacks = new Dictionary<BlePeripheralInfo, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>>>();
//private Dictionary<BlePeripheralInfo, Action<BleWinHwInterface, BleServiceInfo, BleResponse<List<BleCharacteristicInfo>>>> characteristicsCallbacks = new Dictionary<BlePeripheralInfo, Action<BleWinHwInterface, BleServiceInfo, BleResponse<List<BleCharacteristicInfo>>>>();
private Dictionary<BlePeripheralInfo, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse>> characteristicNotificationCallbacks = new Dictionary<BlePeripheralInfo, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse>>();
private Dictionary<BleCharacteristicInfo, CharacteristicReadCallback> characteristicReadCallbacks = new Dictionary<BleCharacteristicInfo, CharacteristicReadCallback>();
private Dictionary<BleServiceInfo, CharacteristicsDiscoveredCallback> characteristicsDiscoveredCallbacks = new Dictionary<BleServiceInfo, CharacteristicsDiscoveredCallback>();
private Dictionary<BlePeripheralInfo, Action> disconnectedCallback = new Dictionary<BlePeripheralInfo, Action>();
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()
{
bleMobileThread = new BleMobileThread();
bleMobileThread.ManagerInitialized += BleMobileThread_ManagerInitialized;
bleMobileThread.ManagerStatusChanged += ManagerStatusChanged;
bleMobileThread.ScanInfoReceived += WatcherScanInfoReceived;
}
private void BleMobileThread_ManagerInitialized(BleMobileThread thread)
{
this.BleState = BleState.On;
}
private void ManagerStatusChanged(BleMobileThread sender, WclBleManagerStatus status)
{
this.BleState = BleMobileInterface.StateFromNativeState(status);
}
private void WatcherScanInfoReceived(BleMobileThread sender, string address, string name, int rssi,string[] uuids)
{
SensorType sensor = SensorType.None;
List<Guid> services = new List<Guid>();
if (uuids != null && uuids.Length > 0)
{
foreach (var item in uuids)
{
services.Add(new Guid(item));
}
foreach (var item in ServiceUuids.Services)
{
if (!uuids.Contains(item.IdGuid.ToString()))
{
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;
if (!pCache.ContainsKey(addressStr))
{
var device = new BleAdvertisementInfo(new WinBlePeripheralInfo(addressStr, name), rssi, true, services, null, sensor);
pCache.Add(addressStr, device);
}
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 uuids)
{
pCache[addressStr].TryAddService(new Guid(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;
//}
List<BleServiceInfo> servicelist = new List<BleServiceInfo>();
List<BleCharacteristicInfo> characteristilist = new List<BleCharacteristicInfo>();
public void ConnectPeripheral(BlePeripheralInfo info, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse> callback)
{
BleResponse s = new BleResponse();
s.IsSuccess = true;
s.Error = null;
var self = this;
BluetoothLEHardwareInterface.ConnectToPeripheral(info.Address, (address) =>
{
callback?.Invoke(self, info, s);
}, (address, service) => {
if (servicesCallbacks.Where(c => c.Key.Address == address).Any())
{
var serviceCallback = servicesCallbacks.Where(c => c.Key.Address == address).FirstOrDefault();
List<BleServiceInfo> servicelist = new List<BleServiceInfo>();
servicelist.Add(new WinBleServiceInfo(serviceCallback.Key,new Guid(service)));
BleResponse<List<BleServiceInfo>> response = new BleResponse<List<BleServiceInfo>>
{
IsSuccess = true,
Error = null,
Data = servicelist,
};
serviceCallback.Value?.Invoke(this, serviceCallback.Key, response);
}
}, (address, service, characteristic) =>
{
if (characteristicsDiscoveredCallbacks.Where(c => c.Key.Peripheral.Address == address).Any())
{
var characteristicCallback = characteristicsDiscoveredCallbacks.Where(c => c.Key.Peripheral.Address == address).FirstOrDefault();
List<BleCharacteristicInfo> characteristiclist = new List<BleCharacteristicInfo>();
characteristiclist.Add(new WinBleCharacteristicInfo(new Guid(characteristic), new WinBleServiceInfo(characteristicCallback.Key.Peripheral, new Guid(service)),0));
BleResponse<List<BleCharacteristicInfo>> response = new BleResponse<List<BleCharacteristicInfo>>
{
IsSuccess = true,
Error = null,
Data = characteristiclist,
};
characteristicCallback.Value?.Invoke(this, characteristicCallback.Key, response);
}
}, null);
}
//设备断开连接
public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback)
{
BluetoothLEHardwareInterface.DisconnectPeripheral(peripheral.Address, (address) =>
{
//peripheralDisconnectedEvent(this, peripheral, null, true);
callback?.Invoke();
});
}
public static BleMobileInterface GetInterface()
{
if(hwInterface == null)
{
hwInterface = new BleMobileInterface();
}
return hwInterface;
}
//扫描设备
public void StartScan(Action<BleAdvertisementInfo> discoveredCallBack)
{
pCache.Clear();
_discoveredCallback = discoveredCallBack;
bleMobileThread.StartWatcher();
}
//发现服务列表
public void DiscoverServices(BlePeripheralInfo peripheral, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>> callback)
{
this.servicesCallbacks.Add(peripheral, callback);
}
//发现特征值
public void DiscoverCharacteristic(BleServiceInfo service, CharacteristicsDiscoveredCallback callback)
{
this.characteristicsDiscoveredCallbacks.Add(service, callback);
}
//订阅特征值
public void SubscribeCharacteristic(BleCharacteristicInfo characteristic, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse> callback)
{
this.characteristicNotificationCallbacks.Add(characteristic.Peripheral, callback);
BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(characteristic.Peripheral.Address, characteristic.Service.ToString(), characteristic.Id.ToString(), null, (deviceAddress, characteristric, bytes) =>
{
if (characteristicReadEvent != null)
{
BleResponse<byte[]> response = new BleResponse<byte[]>
{
IsSuccess = true,
Error = null,
Data = bytes,
};
characteristicReadEvent.Invoke(this, characteristic, response);
}
});
}
//写入特征值
public void WriteCharacteristic(BleCharacteristicInfo characteristic, byte[] data)
{
BluetoothLEHardwareInterface.WriteCharacteristic(characteristic.Peripheral.Address, characteristic.Service.Id.ToString(), characteristic.Id.ToString(), data, data.Length, false, (characteristicUUID) => {
BluetoothLEHardwareInterface.Log("Write Succeeded");
});
}
public void Dispose()
{
bleMobileThread.ManagerInitialized -= BleMobileThread_ManagerInitialized;
bleMobileThread.ManagerStatusChanged -= ManagerStatusChanged;
bleMobileThread.ScanInfoReceived -= WatcherScanInfoReceived;
bleMobileThread.Stop();
bleMobileThread = null;
hwInterface = null;
pCache.Clear();
}
public void ReadCharacteristic(BleCharacteristicInfo characteristic, CharacteristicReadCallback callback)
{
BluetoothLEHardwareInterface.ReadCharacteristic(characteristic.Peripheral.Address, characteristic.Service.Id.ToString(), characteristic.Id.ToString(), (c, bytes) => {
BleResponse<byte[]> response = new BleResponse<byte[]>
{
IsSuccess = true,
Error = null,
Data = bytes,
};
callback?.Invoke(this, characteristic, response);
});
}
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;
}
}
private class WinBleServiceInfo : BleServiceInfo
{
// Token: 0x06003F86 RID: 16262 RVA: 0x000EA27F File Offset: 0x000E847F
public WinBleServiceInfo(BlePeripheralInfo peripheral, Guid id) : base(id, peripheral)
{
}
}
private class WinBleCharacteristicInfo : BleCharacteristicInfo
{
// Token: 0x06003F86 RID: 16262 RVA: 0x000EA27F File Offset: 0x000E847F
public WinBleCharacteristicInfo(Guid id, BleServiceInfo service, BleCharacteristicProperties properties) : base(id,service, properties)
{
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2e5257ea169e69a498a3e4d935a51306
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,80 @@
using Assets.Scripts.Ble;
using System.Timers;
namespace Assets.Scripts.Devices.Ble
{
internal class BleMobileThread
{
public delegate void WclInitializedDelegate(BleMobileThread sender);
private BleMobileThread.WclInitializedDelegate managerInitialized;
public event BleMobileThread.WclInitializedDelegate ManagerInitialized
{
add
{
this.managerInitialized += value;
}
remove
{
this.managerInitialized -= value;
}
}
public delegate void WclAdvertisementPacketDelegate(BleMobileThread sender, string address, string name, int rssi, string[] uuids);
public BleMobileThread.WclAdvertisementPacketDelegate ScanInfoReceived;
internal delegate void ManagerStatusChangedCallback(BleMobileThread thread, WclBleManagerStatus status);
private BleMobileThread.ManagerStatusChangedCallback managerStatusChanged;
public event BleMobileThread.ManagerStatusChangedCallback ManagerStatusChanged
{
add
{
this.managerStatusChanged += value;
}
remove
{
this.managerStatusChanged -= value;
}
}
internal BleMobileThread() {
var self = this;
//初始蓝牙
BluetoothLEHardwareInterface.Initialize(true, false, () => {
managerInitialized?.Invoke(self);
},
(error) => {
BluetoothLEHardwareInterface.Log("Error: " + error);
if (error.Contains("Bluetooth LE Not Enabled"))
BluetoothLEHardwareInterface.BluetoothEnable(true);
}, (status) => {
var statusEnum = WclBleManagerStatus.RadioOn;
switch (status)
{
case 13:
statusEnum = WclBleManagerStatus.RadioOff;
break;
case 11:
statusEnum = WclBleManagerStatus.RadioOn;
break;
}
if (managerStatusChanged != null)
{
managerStatusChanged.Invoke(self, statusEnum);
}
});
}
public void StartWatcher() {
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, null, (address, name, rssi, bytes) =>
{
ScanInfoReceived?.Invoke(this, address, name, rssi, bytes);
}, true);
}
public void Stop()
{
BluetoothLEHardwareInterface.StopScan();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cec5e50ac67fde54196ea58f5b1b790b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+12 -5
View File
@@ -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;
@@ -14,10 +15,12 @@ namespace Assets.Scripts.Devices
private List<DeviceAdapter> adapters = new List<DeviceAdapter>();
public MainDeviceAdapter()
{
#if !(UNITY_IOS || UNITY_ANDROID)
this.CreateAntAdapter();
#if UNITY_IOS || UNITY_TVOS || UNITY_ANDROID
this.CreateBleAdapter();
#endif
#else
this.CreateBleAdapter();
this.CreateAntAdapter();
#endif
}
private void CreateAntAdapter()
@@ -27,7 +30,11 @@ namespace Assets.Scripts.Devices
private void CreateBleAdapter()
{
adapters.Add(new BleDeviceAdapter());
#if UNITY_IOS || UNITY_TVOS || UNITY_ANDROID
adapters.Add(new BleDeviceAdapter(BleMobileInterface.GetInterface()));
#else
adapters.Add(new BleDeviceAdapter(BleWinHwInterface.GetInterface()));
#endif
}
public void StartScan()
+3
View File
@@ -330,6 +330,7 @@ public class LoginController : MonoBehaviour
{
wxReturnLogin();
});
#if !(UNITY_IOS || UNITY_ANDROID)
wxBrowser = wxLogin1.Find("Browser (GUI)").GetComponent<Browser>();
if (wxBrowser != null)
{
@@ -941,7 +942,9 @@ public class LoginController : MonoBehaviour
void wxReturnLogin()
{
wxLock = true;
#if !(UNITY_IOS || UNITY_ANDROID)
wxBrowser.LoadHTML("<div/>");
#endif
//wxBrowser.Url = "chrome://version/";
wxLogin1.DOLocalMoveY(-573, 0.3f).onComplete = () =>
{
@@ -56,7 +56,7 @@ namespace Assets.Scenes.Ride.Scripts
weight = App.CurrentUser.Weight;
bicycleWeight = App.CurrentUser.BicycleWeight;
//#if UNITY_EDITOR
power = 500;
//power = 500;
//#endif
mainController.TrackResistance(currentSlope * App.RideSetting.Sensitivity / 100);
}