解决蓝牙卡顿问题

This commit is contained in:
suntao committed 2021-06-08 10:30:26 +08:00
1 parent 432e02e3de
commit aa75bb281f
14 files changed
+409 -319

No files matched your search

@@ -99,29 +99,14 @@ namespace Assets.Scripts.Ble
{
//if(address != 224160707349234)
//{
//return;
//return;
//}
if (service.HasValue)
{
//Debug.Log($"address:{ address }, name:{ name }, service:{ (service == null ? "" : service.Value.ToString()) }");
}
if (!string.IsNullOrWhiteSpace(name))
{
if (pCache.ContainsKey(address.ToString()))
{
(pCache[address.ToString()].Peripheral as WinBlePeripheralInfo).SetName(name);
}
}
//Debug.Log("service:" + service.ToString()+",name:" + name);
if (!service.HasValue || ServiceUuids.Services.Select(s => s.IdGuid).All(x => !x.Equals(service.Value)))
{
return;
}
//Debug.Log($"address:{ address }, name:{ name }, service:{ (service == null ? "" : service.Value.ToString()) }");
SensorType sensor = SensorType.None;
List<Guid> services = null;
List<Guid> services = new List<Guid>();
if (service != null)
{
{
services = new List<Guid> { service.Value };
foreach(var item in ServiceUuids.Services)
{
@@ -152,36 +137,51 @@ namespace Assets.Scripts.Ble
}
}
};
if (!pCache.ContainsKey(address.ToString()))
var addressStr = address.ToString("X12");
if (!pCache.ContainsKey(addressStr))
{
var device = new BleAdvertisementInfo(new WinBlePeripheralInfo(address.ToString(), name), rssi, true, services, null, sensor);
pCache.Add(address.ToString(), device);
var device = new BleAdvertisementInfo(new WinBlePeripheralInfo(addressStr, name), rssi, true, services, null, sensor);
pCache.Add(addressStr, device);
WclBleGattThread gattClient = this.SetUpGattClient(device.Peripheral);
//WclBleGattThread gattClient = this.SetUpGattClient(device.Peripheral);
//this.ConnectInternal(gattClient);
}
else
if (!string.IsNullOrWhiteSpace(name))
{
//Debug.Log(sensor);
//pCache[address.ToString()].SensorType = sensor;
(pCache[addressStr].Peripheral as WinBlePeripheralInfo).SetName(name);
}
if(sensor == SensorType.None)
{
return;
}
pCache[addressStr].SensorType = sensor;
//Debug.Log(sensor);
//pCache[address.ToString()].SensorType = sensor;
foreach (var item in services)
{
pCache[address.ToString()].TryAddService(item);
}
pCache[address.ToString()].Index++;
if(pCache[address.ToString()].SensorType == SensorType.Power && pCache[address.ToString()].Services.Any(s=> s.Equals(ServiceUuids.Get(ServiceUuids.TacxBle).IdGuid)))
{
pCache[address.ToString()].SensorType = SensorType.Trainer;
//Debug.Log("纠正为trainer, "+ pCache[address.ToString()].Index);
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[address.ToString()].Index > 4 || pCache[address.ToString()].SensorType != SensorType.Power)
if (pCache[addressStr].SensorType == SensorType.Power)
{
if (pCache[addressStr].Index > 4)
{
_discoveredCallback?.Invoke(pCache[address.ToString()]);
_discoveredCallback?.Invoke(pCache[addressStr]);
}
}
else if(pCache[addressStr].SensorType != SensorType.None)
{
_discoveredCallback?.Invoke(pCache[addressStr]);
}
}
private WclBleGattThread SetUpGattClient(BlePeripheralInfo peripheral)
@@ -203,8 +203,10 @@ namespace Assets.Scripts.Ble
{
this.callbacks.Add(info, callback);
WclBleGattThread wclBleGattThread = this.wclBleMainThread.GetGattThread(info);
if(wclBleGattThread == null)
{
wclBleGattThread = this.SetUpGattClient(info);
this.ConnectInternal(wclBleGattThread);
return;
@@ -259,7 +261,7 @@ namespace Assets.Scripts.Ble
private void ManagerStatusChanged(WclBleMainThread sender, WclBleManagerStatus status)
{
this.BleState = BleWinHwInterface.StateFromNativeState(status);
Debug.Log("win hw:" + status);
//Debug.Log("win hw:" + status);
}
@@ -272,6 +274,7 @@ namespace Assets.Scripts.Ble
return;
}
this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
this.callbacks.Remove(gattClient.Peripheral);
}
private void CleanUpPeripheral(BlePeripheralInfo peripheralInfo)
@@ -285,13 +288,13 @@ namespace Assets.Scripts.Ble
}
private void GattServicesDiscovered(WclBleGattThread gattClient, BleResponse<List<BleServiceInfo>> response)
{
Debug.Log("services discovered");
//Debug.Log("services discovered");
//this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
foreach (var item in response.Data)
{
Debug.Log(item.ToString());
}
//foreach (var item in response.Data)
//{
// Debug.Log(item.ToString());
//}
if (this.servicesCallbacks.ContainsKey(gattClient.Peripheral))
{
@@ -311,7 +314,7 @@ namespace Assets.Scripts.Ble
private void GattCharacteristicSubscribed(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse response)
{
Debug.Log("characteristics subscribed");
//Debug.Log("characteristics subscribed");
if (this.characteristicNotificationCallbacks.ContainsKey(gattClient.Peripheral))
{
this.characteristicNotificationCallbacks[gattClient.Peripheral].Invoke(this, characteristic, response);
@@ -428,6 +431,8 @@ namespace Assets.Scripts.Ble
public void Dispose()
{
this.wclBleMainThread.ManagerStatusChanged -= ManagerStatusChanged;
this.wclBleMainThread.ScanInfoReceived -= WatcherScanInfoReceived;
this.wclBleMainThread.Stop();
this.wclBleMainThread = null;
hwInterface = null;
@@ -218,6 +218,7 @@ namespace Assets.Scripts.Ble.Win
{
try
{
Debug.Log("onconnected");
WclBleGattClient.GattConnectionChanged connected = this.Connected;
if (connected != null)
{
+114 -107
View File
@@ -12,8 +12,8 @@ using UnityEngine;
namespace Assets.Scripts.Devices.Ble.Win
{
internal class WclBleGattThread
{
internal class WclBleGattThread: WclBleThread
{
public delegate void GattCharacteristicChangedDelegate(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse<byte[]> response);
public delegate void GattCharacteristicReadDelegate(WclBleGattThread gattClient, BleCharacteristicInfo characteristic, BleResponse<byte[]> response);
public delegate void GattCharacteristicsDiscoveredDelegate(WclBleGattThread gattClient, BleServiceInfo service, BleResponse<List<BleCharacteristicInfo>> response);
@@ -175,57 +175,7 @@ namespace Assets.Scripts.Devices.Ble.Win
this.Peripheral = bleDevice;
this.rPtr = radio;
_bleDevice = bleDevice;
//this.address = long.Parse(_bleDevice.Address, System.Globalization.NumberStyles.HexNumber);
this.address = long.Parse(_bleDevice.Address);
}
public bool Start()
{
if (this.start)
{
return false;
}
this.start = true;
this.sEvent = new ManualResetEvent(false);
this.tEvent = new ManualResetEvent(false);
this.aEvent = new AutoResetEvent(false);
//thread = new Thread(new ThreadStart(ThreadProc));
// this.sEvent.Set();
// IntPtr[] handle = new IntPtr[]
// {
// this.tEvent.SafeWaitHandle.DangerousGetHandle(),
// this.aEvent.SafeWaitHandle.DangerousGetHandle()
// };
// while (WclAlertableWait.Wait(handle, 2U, WclAlertableWait.INFINITE) != WclAlertableWait.WAIT_OBJECT_0)
// {
// Action action;
// if (this.actions.TryDequeue(out action))
// {
// if (action != null)
// {
// Debug.Log("连接设备");
// action();
// }
// }
// }
// }));
//thread.Start();
//this.sEvent.WaitOne();
ThreadProc();
return true;
}
public void Stop()
{
Debug.Log("停止");
start = false;
thread?.Abort();
this.address = long.Parse(_bleDevice.Address, System.Globalization.NumberStyles.HexNumber);
}
private void ThreadProc()
@@ -238,28 +188,15 @@ namespace Assets.Scripts.Devices.Ble.Win
//this.InternalCleanUp();
}
private void CleanUpWorkerThread()
protected override void CleanUpWorkerThread()
{
this.gatt.Disconnect();
base.ProcessPendingAPCMessages();
this.gatt.Dispose();
this.gatt = null;
}
private void InternalCleanUp()
{
this.sEvent.Close();
this.sEvent.Dispose();
this.sEvent = null;
this.aEvent.Close();
this.aEvent.Dispose();
this.aEvent = null;
this.tEvent.Close();
this.tEvent.Dispose();
this.tEvent = null;
}
private void SetUpWorkerThread()
protected override void SetUpWorkerThread()
{
this.gatt = new WclBleGattClient(this.address, this.rPtr)
{
@@ -302,7 +239,13 @@ namespace Assets.Scripts.Devices.Ble.Win
IsSuccess = true,
Data = value
};
this.gattCharacteristicChanged?.Invoke(this, characteristic, response);
if (!base.IsTerminating)
{
//Loom.QueueOnMainThread(() =>
//{
this.gattCharacteristicChanged?.Invoke(this, characteristic, response);
//});
}
}
private void OnCharacteristicRead(BleCharacteristicInfo characteristic, int result, byte[] data)
@@ -319,7 +262,13 @@ namespace Assets.Scripts.Devices.Ble.Win
{
response.Error = new BleHwInterfaceError(new int?(result), "WclBleGattClientErrorDomain", "Error when reading value");
}
this.gattCharacteristicRead.Invoke(this, characteristic, response);
if (!base.IsTerminating)
{
//Loom.QueueOnMainThread(() =>
//{
this.gattCharacteristicRead.Invoke(this, characteristic, response);
//});
}
}
private void OnCharacteristicsDiscovered(BleServiceInfo service, int result, GattCharacteristics characteristics)
@@ -336,18 +285,33 @@ namespace Assets.Scripts.Devices.Ble.Win
{
// response.Error = new BleHwInterfaceError(new int?(result), "WclBleGattClientErrorDomain", "Error when reading BLE services");
}
this.gattCharacteristicsDiscovered(this, service, response);
if (!base.IsTerminating)
{
//Loom.QueueOnMainThread(() =>
//{
this.gattCharacteristicsDiscovered(this, service, response);
//});
}
}
private void OnConnected(WclBleGattClient sender, int error)
{
BleResponse response = new BleResponse
{
IsSuccess = WclBleErrors.IsSuccessCode(error)
};
this.gattConnected?.Invoke(this, response);
}
//Debug.Log("gatt thread onconnected");
if (base.IsTerminating)
{
return;
}
BleResponse response = new BleResponse
{
IsSuccess = WclBleErrors.IsSuccessCode(error)
};
//Loom.QueueOnMainThread(() =>
//{
this.gattConnected?.Invoke(this, response);
//});
}
private void OnDisconnected(WclBleGattClient sender, int reason)
{
@@ -356,16 +320,29 @@ namespace Assets.Scripts.Devices.Ble.Win
IsSuccess = true,
Error = new BleHwInterfaceError(new int?(reason), "WclBleGattClientErrorDomain", string.Format("GATT disconnect reason is - {0}", reason))
};
this.gattDisconnected?.Invoke(this, response);
//Loom.QueueOnMainThread(() =>
//{
this.gattDisconnected?.Invoke(this, response);
//});
}
public int Connect()
{
this.gatt.Connect();
//this.actions.Enqueue(() =>
//{
// this.gatt.Connect();
//});
if (!base.CanLoadWork)
{
return WclBleErrors.WCL_E_CONNECTION_NOT_ACTIVE;
}
if(this.gatt.State != WclBleGattClientState.Disconnected)
{
return WclBleErrors.WCL_E_BLUETOOTH_CLIENT_CONNECTED;
}
base.ClearActionQueue();
base.EnqueueAction(() =>
{
this.gatt.Connect();
});
return WclBleErrors.WCL_E_SUCCESS;
}
@@ -378,9 +355,12 @@ namespace Assets.Scripts.Devices.Ble.Win
public int DiscoverServices()
{
GattServices services;
int result = this.gatt.DiscoverServices(out services);
this.OnServicesDiscovered(result, services);
base.EnqueueAction(() =>
{
GattServices services;
int result = this.gatt.DiscoverServices(out services);
this.OnServicesDiscovered(result, services);
});
return WclBleErrors.WCL_E_SUCCESS;
}
@@ -398,8 +378,12 @@ namespace Assets.Scripts.Devices.Ble.Win
{
response.Error = new BleHwInterfaceError(new int?(result), "WclBleGattClientErrorDomain", "Error when reading BLE services");
}
gattServicesDiscovered(this, response);
if (!base.IsTerminating) {
//Loom.QueueOnMainThread(() =>
//{
gattServicesDiscovered(this, response);
//});
}
}
private List<BleCharacteristicInfo> ProcessCharacteristics(BleServiceInfo service, GattCharacteristics characteristics)
@@ -431,10 +415,12 @@ namespace Assets.Scripts.Devices.Ble.Win
{
GattService ns = this.servicesMapping[service];
GattCharacteristics characteristics;
int result = this.gatt.DiscoverCharacteristics(ns, out characteristics);
this.OnCharacteristicsDiscovered(service, result, characteristics);
base.EnqueueAction(() =>
{
GattCharacteristics characteristics;
int result = this.gatt.DiscoverCharacteristics(ns, out characteristics);
this.OnCharacteristicsDiscovered(service, result, characteristics);
});
return WclBleErrors.WCL_E_SUCCESS;
}
@@ -447,10 +433,12 @@ namespace Assets.Scripts.Devices.Ble.Win
}
GattCharacteristic nCh = this.charMapping[characteristic];
byte[] data;
int result = this.gatt.ReadCharacteristicValue(nCh, out data);
this.OnCharacteristicRead(characteristic, result, data);
base.EnqueueAction(() =>
{
byte[] data;
int result = this.gatt.ReadCharacteristicValue(nCh, out data);
this.OnCharacteristicRead(characteristic, result, data);
});
return WclBleErrors.WCL_E_SUCCESS;
}
@@ -462,16 +450,17 @@ namespace Assets.Scripts.Devices.Ble.Win
}
GattCharacteristic nCh = this.charMapping[characteristic];
int result = this.gatt.SubscribeCharacteristic(nCh);
this.OnCharacteristicsSubscribed(characteristic, nCh, result);
base.EnqueueAction(() =>
{
int result = this.gatt.SubscribeCharacteristic(nCh);
this.OnCharacteristicsSubscribed(characteristic, nCh, result);
});
return WclBleErrors.WCL_E_SUCCESS;
}
private void OnCharacteristicsSubscribed(BleCharacteristicInfo characteristic, GattCharacteristic nCharacteristic, int result)
{
Debug.Log("char subscribed");
//Debug.Log("char subscribed");
BleResponse response = new BleResponse
{
IsSuccess = WclBleErrors.IsSuccessCode(result)
@@ -484,7 +473,12 @@ namespace Assets.Scripts.Devices.Ble.Win
{
response.Error = new BleHwInterfaceError(new int?(result), "WclBleGattClientErrorDomain", "Error when subscribing Characteristics");
}
this.gattCharacteristicsSubscribed(this, characteristic, response);
if (!base.IsTerminating)
{
//Loom.QueueOnMainThread(() => {
this.gattCharacteristicsSubscribed(this, characteristic, response);
//});
}
}
public int WriteCharacteristic(BleCharacteristicInfo characteristicInfo, byte[] data)
@@ -494,14 +488,27 @@ namespace Assets.Scripts.Devices.Ble.Win
return WclBleErrors.WCL_E_CONNECTION_NOT_ACTIVE;
}
GattCharacteristic gCh = this.charMapping[characteristicInfo];
int result = this.gatt.WriteCharacteristic(gCh, data);
base.EnqueueAction(() =>
{
int result = this.gatt.WriteCharacteristic(gCh, data);
this.OnCharactersisticsWrote(characteristicInfo, result);
});
return WclBleErrors.WCL_E_SUCCESS;
}
private void OnCharactersisticsWrote(BleCharacteristicInfo characteristic, int result)
{
var response = new BleResponse<BleCharacteristicWriteType>
{
IsSuccess = WclBleErrors.IsSuccessCode(result),
Data = BleCharacteristicWriteType.WriteWithResponse
};
this.gattCharacteristicWrote?.Invoke(this, characteristic, response);
}
private class WinBleCharacteristicInfo : BleCharacteristicInfo
{
// Token: 0x06003F85 RID: 16261 RVA: 0x000EA274 File Offset: 0x000E8474
@@ -11,7 +11,7 @@ using UnityEngine;
namespace Assets.Scripts.Devices.Ble.Win
{
internal class WclBleMainThread
internal class WclBleMainThread: WclBleThread
{
internal delegate void ManagerStatusChangedCallback(WclBleMainThread thread, WclBleManagerStatus status);
@@ -34,24 +34,11 @@ namespace Assets.Scripts.Devices.Ble.Win
private readonly Dictionary<BlePeripheralInfo, WclBleGattThread> gattClients = new Dictionary<BlePeripheralInfo, WclBleGattThread>();
internal WclBleMainThread()
{
}
private Thread wThread;
public bool Start()
{
//this.wThread = new Thread(new ThreadStart(this.SetUpWorkerThread));
//this.wThread.Start();
//Task.Run(() =>
//{
// this.SetUpWorkerThread();
//});
SetUpWorkerThread();
return true;
WclAlertableWait.SetApcSync();
}
private void SetUpWorkerThread()
protected override void SetUpWorkerThread()
{
wclBleManager = new WclBleManager
{
@@ -72,6 +59,28 @@ namespace Assets.Scripts.Devices.Ble.Win
//Debug.Log("bbb");
}
protected override void CleanUpWorkerThread()
{
this.wclWatcher.InfoPacketReceived = null;
this.wclWatcher.UuidPacketReceived = null;
if (this.wclWatcher.Active)
{
this.wclWatcher.StopScan();
}
//this.StopGattThreads();
if (this.wclBleManager.Active)
{
this.wclBleManager.ManagerStatusChanged = null;
this.wclBleManager.Close();
}
base.ProcessPendingAPCMessages();
this.wclWatcher.Dispose();
this.wclBleManager.Dispose();
this.wclBleManager = null;
this.wclWatcher = null;
}
private void OnManagerStatusChanged(WclBleManager manager, WclBleManagerStatus status)
{
//Debug.Log(status);
@@ -62,11 +62,6 @@ namespace Assets.Scripts.Ble
// Token: 0x06002178 RID: 8568 RVA: 0x0008B1CD File Offset: 0x000893CD
private void OnStatusChanges(IntPtr sender, WclBleManagerStatus status)
{
//if (WclBleManager.Info)
//{
Debug.Log(string.Format("WCLBleManger state changed -> new state {0}", status));
//}
//System.IO.File.WriteAllText("d:/demo.log", status.ToString());
this.BleStatus = status;
}
@@ -103,7 +103,10 @@ namespace Assets.Scripts.Ble
//{
// WclBleWatcher.SafeInvoke(this.InfoPacketReceived, this, address, timestamp, rssi, name, packetType, flags);
//}.OnUIThreadAsync();
this.InfoPacketReceived(this, address, timestamp, rssi, name, packetType, flags);
Loom.QueueOnMainThread(() =>
{
this.InfoPacketReceived(this, address, timestamp, rssi, name, packetType, flags);
});
}
// Token: 0x06002133 RID: 8499 RVA: 0x000895BC File Offset: 0x000877BC
@@ -115,7 +118,10 @@ namespace Assets.Scripts.Ble
// WclBleWatcher.SafeInvoke(this.UuidPacketReceived, this, address, timestamp, rssi, uuid);
//}.OnUIThreadAsync();
this.UuidPacketReceived(this, address, timestamp, rssi, uuid);
Loom.QueueOnMainThread(() =>
{
this.UuidPacketReceived(this, address, timestamp, rssi, uuid);
});
}
// Token: 0x06002134 RID: 8500 RVA: 0x000895F9 File Offset: 0x000877F9