解决蓝牙设备丢失以后状态不变的问题

This commit is contained in:
suntao committed 2021-06-09 18:16:35 +08:00
1 parent 15e440715d
commit 5f7cc43593
43 files changed
+114 -5363

No files matched your search

@@ -17,7 +17,7 @@ namespace Assets.Scripts.Ble
private static BleWinHwInterface hwInterface;
private WclBleMainThread wclBleMainThread;
private readonly Dictionary<string, BleAdvertisementInfo> pCache = new Dictionary<string, BleAdvertisementInfo>();
public readonly Dictionary<string, BleAdvertisementInfo> pCache = new Dictionary<string, BleAdvertisementInfo>();
private Action<BleAdvertisementInfo> _discoveredCallback;
@@ -28,6 +28,7 @@ namespace Assets.Scripts.Ble
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
@@ -236,9 +237,8 @@ namespace Assets.Scripts.Ble
this.servicesCallbacks.Remove(peripheral);
this.characteristicNotificationCallbacks.Remove(peripheral);
gattThread.Discounect();
callback?.Invoke();
this.disconnectedCallback.Add(peripheral, callback);
gattThread.Discounect();
}
}
@@ -272,6 +272,9 @@ namespace Assets.Scripts.Ble
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);
@@ -286,6 +289,22 @@ namespace Assets.Scripts.Ble
private void GattDisconnected(WclBleGattThread gattClient, BleResponse response)
{
Debug.Log("gatt disconnected");
this.callbacks.Remove(gattClient.Peripheral);
this.servicesCallbacks.Remove(gattClient.Peripheral);
this.characteristicNotificationCallbacks.Remove(gattClient.Peripheral);
var manualDisconnect = disconnectedCallback.ContainsKey(gattClient.Peripheral);
this.peripheralDisconnectedEvent(this, gattClient.Peripheral, response, manualDisconnect);
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<List<BleServiceInfo>> response)
{