支持蓝牙可用状态变更。搜索不到设备自动停止当前线程。

This commit is contained in:
suntao committed 2021-06-10 17:46:43 +08:00
1 parent dd474f1f61
commit 734893ff83
27 files changed
+290 -143

No files matched your search

@@ -225,20 +225,22 @@ namespace Assets.Scripts.Ble
//{
int num = gattClient.Connect();
Debug.Log("连接设备返回" + num);
//});
}
internal void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback)
{
var gattThread = this.wclBleMainThread.GetGattThread(peripheral);
if(gattThread != null)
if(gattThread != null && gattThread.CanLoadWork)
{
this.callbacks.Remove(peripheral);
this.servicesCallbacks.Remove(peripheral);
this.characteristicNotificationCallbacks.Remove(peripheral);
this.disconnectedCallback.Add(peripheral, callback);
gattThread.Discounect();
gattThread.Discounect();
}
}
@@ -255,6 +257,7 @@ namespace Assets.Scripts.Ble
public void StartScan(Action<BleAdvertisementInfo> discoveredCallBack)
{
pCache.Clear();
_discoveredCallback = discoveredCallBack;
this.wclBleMainThread.StartWatcher();
}
@@ -288,15 +291,17 @@ namespace Assets.Scripts.Ble
private void GattDisconnected(WclBleGattThread gattClient, BleResponse response)
{
Debug.Log("gatt disconnected");
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();
@@ -304,7 +309,8 @@ namespace Assets.Scripts.Ble
}
//this.pCache.Remove(gattClient.Peripheral.Address);
}
private void GattServicesDiscovered(WclBleGattThread gattClient, BleResponse<List<BleServiceInfo>> response)
{
@@ -324,7 +330,7 @@ namespace Assets.Scripts.Ble
private void GattCharacteristicsDiscovered(WclBleGattThread gattClient, BleServiceInfo service, BleResponse<List<BleCharacteristicInfo>> response)
{
Debug.Log("characteristics discovered");
//Debug.Log("characteristics discovered");
if (this.characteristicsDiscoveredCallbacks.ContainsKey(service))
{
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assets.Scripts.Devices.Ble.Win
{
internal delegate void GattThreadStopped(WclBleMainThread thread, WclBleGattThread gatt);
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9ea66f9d6ac61144a97caaaa882a2cb9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -194,7 +194,7 @@ namespace Assets.Scripts.Ble.Win
{
try
{
Debug.Log("onconnected");
Debug.Log($"wclbleGattClient onconnected { error }");
WclBleGattClient.GattConnectionChanged connected = this.Connected;
if (connected != null)
{
@@ -170,23 +170,24 @@ namespace Assets.Scripts.Devices.Ble.Win
private ManualResetEvent sEvent;
private AutoResetEvent aEvent;
private ManualResetEvent tEvent;
internal WclBleGattThread(BlePeripheralInfo bleDevice, IntPtr radio)
internal WclBleGattThread(BlePeripheralInfo bleDevice, IntPtr radio, WclBleGattThread.WclBleGattThreadStoppedCallback stopCallback)
{
this.Peripheral = bleDevice;
this.rPtr = radio;
_bleDevice = bleDevice;
this.address = long.Parse(_bleDevice.Address, System.Globalization.NumberStyles.HexNumber);
this.threadStoppedCallback = stopCallback;
}
private void ThreadProc()
{
this.SetUpWorkerThread();
//this.sEvent.Set();
//this.DoWorkerLoop();
//private void ThreadProc()
// {
// this.SetUpWorkerThread();
// //this.sEvent.Set();
// //this.DoWorkerLoop();
//this.CleanUpWorkerThread();
//this.InternalCleanUp();
}
// //this.CleanUpWorkerThread();
// //this.InternalCleanUp();
//}
protected override void CleanUpWorkerThread()
{
@@ -194,7 +195,10 @@ namespace Assets.Scripts.Devices.Ble.Win
base.ProcessPendingAPCMessages();
this.gatt.Dispose();
this.gatt = null;
}
this.threadStoppedCallback?.Invoke(this);
}
protected override void SetUpWorkerThread()
{
@@ -305,8 +309,13 @@ namespace Assets.Scripts.Devices.Ble.Win
}
BleResponse response = new BleResponse
{
IsSuccess = WclBleErrors.IsSuccessCode(error)
};
IsSuccess = WclBleErrors.IsSuccessCode(error),
};
if (!response.IsSuccess)
{
response.Error = new BleHwInterfaceError(error, "", "");
}
//Loom.QueueOnMainThread(() =>
//{
this.gattConnected?.Invoke(this, response);
@@ -508,6 +517,8 @@ namespace Assets.Scripts.Devices.Ble.Win
this.gattCharacteristicWrote?.Invoke(this, characteristic, response);
}
private WclBleGattThread.WclBleGattThreadStoppedCallback threadStoppedCallback;
public delegate void WclBleGattThreadStoppedCallback(WclBleGattThread clientThread);
private class WinBleCharacteristicInfo : BleCharacteristicInfo
{
@@ -28,6 +28,19 @@ namespace Assets.Scripts.Devices.Ble.Win
}
}
private GattThreadStopped gattThreadStopped;
public event GattThreadStopped GattThreadStopped
{
add
{
this.gattThreadStopped += value;
}
remove
{
this.gattThreadStopped -= value;
}
}
private WclBleManager wclBleManager;
private WclBleWatcher wclWatcher;
private readonly object locker = new object();
@@ -107,7 +120,7 @@ namespace Assets.Scripts.Devices.Ble.Win
});
}
public void Stop()
public override bool Stop()
{
Debug.Log("停止thread");
foreach (var item in this.gattClients.Values)
@@ -116,6 +129,8 @@ namespace Assets.Scripts.Devices.Ble.Win
}
wclBleManager.Dispose();
return true;
}
public string Test()
@@ -153,13 +168,33 @@ namespace Assets.Scripts.Devices.Ble.Win
throw new ArgumentException("gatt thread 已经创建");
}
WclBleGattThread wclBleGattThread = new WclBleGattThread(info, this.wclBleManager.Radio);
WclBleGattThread wclBleGattThread = new WclBleGattThread(info, this.wclBleManager.Radio, this.GattThreadStoppedCallback);
this.gattClients[info] = wclBleGattThread;
return wclBleGattThread;
}
}
private void GattThreadStoppedCallback(WclBleGattThread gattThread)
{
Debug.Log("stop callback");
bool flag = false;
object obj = this.locker;
lock (obj)
{
if (this.gattClients.ContainsKey(gattThread.Peripheral))
{
this.gattClients.Remove(gattThread.Peripheral);
flag = true;
}
}
if (flag)
{
this.gattThreadStopped?.Invoke(this, gattThread);
}
}
public event WclBleMainThread.WclAdvertisementPacketDelegate ScanInfoReceived
{
add
@@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Threading;
using UnityEngine;
namespace Assets.Scripts.Devices.Ble.Win
{
@@ -16,6 +17,7 @@ namespace Assets.Scripts.Devices.Ble.Win
{
get
{
//Debug.Log($"{ this.started }, { this.IsTerminating }");
return this.started || this.IsTerminating;
}
}
@@ -36,6 +38,7 @@ namespace Assets.Scripts.Devices.Ble.Win
{
get
{
//Debug.Log($"{ this.IsRunning }, { this.IsTerminating }");
return this.IsRunning && !this.IsTerminating;
}
}