设备断开优化

This commit is contained in:
lishuo 2022-07-20 10:44:47 +08:00
parent 63987511e7
commit 2274a13619
2 changed files with 19 additions and 5 deletions

View File

@ -53,11 +53,12 @@ namespace Assets.Scripts.Devices.Ble
private void HwInterface_PeripheralDisconnectedEvent(IBleWinHwInterface hwInterface, BlePeripheralInfo peripheral, BleResponse response, bool manualDisconnect) private void HwInterface_PeripheralDisconnectedEvent(IBleWinHwInterface hwInterface, BlePeripheralInfo peripheral, BleResponse response, bool manualDisconnect)
{ {
Debug.Log("disconnected event" + this.peripheralInfo.Address + "==" + peripheral.Address);
if (!peripheral.MatchAddress(this.peripheralInfo.Address)) if (!peripheral.MatchAddress(this.peripheralInfo.Address))
{ {
return; return;
} }
Debug.Log("disconnected event");
if (this.State != DeviceState.Disconnected) if (this.State != DeviceState.Disconnected)
{ {
this.State = DeviceState.Disconnected; this.State = DeviceState.Disconnected;

View File

@ -31,6 +31,7 @@ namespace Assets.Scripts.Ble
private Action<BleAdvertisementInfo> _discoveredCallback; 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>> callbacks = new Dictionary<BlePeripheralInfo, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse>>();
private Dictionary<string, Action> diconnectedCallbacks = new Dictionary<string, Action>();
private Dictionary<BlePeripheralInfo, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>>> servicesCallbacks = new Dictionary<BlePeripheralInfo, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>>>(); 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<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<BlePeripheralInfo, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse>> characteristicNotificationCallbacks = new Dictionary<BlePeripheralInfo, Action<IBleWinHwInterface, BleCharacteristicInfo, BleResponse>>();
@ -246,6 +247,7 @@ namespace Assets.Scripts.Ble
}, },
(address, error) => (address, error) =>
{ {
Debug.Log("error device:" + address+ error);
BleResponse s = new BleResponse BleResponse s = new BleResponse
{ {
IsSuccess = false, IsSuccess = false,
@ -261,6 +263,7 @@ namespace Assets.Scripts.Ble
} }
else else
{ {
Debug.Log("265 error device:" + address + error);
peripheralDisconnectedEvent(this, ble, null, false); peripheralDisconnectedEvent(this, ble, null, false);
} }
@ -320,6 +323,12 @@ namespace Assets.Scripts.Ble
this.callbacks.Remove(currentCallback.First().Key); this.callbacks.Remove(currentCallback.First().Key);
} }
var currentDisconnectCallback = this.disconnectedCallback.Where(c => c.Key.MatchAddress(address));
if (currentDisconnectCallback.Any())
{
this.disconnectedCallback.Remove(currentDisconnectCallback.First().Key);
}
var characteristicCallback = characteristicNotificationCallbacks.Where(c => c.Key.MatchAddress(address)); var characteristicCallback = characteristicNotificationCallbacks.Where(c => c.Key.MatchAddress(address));
if (characteristicCallback.Any()) if (characteristicCallback.Any())
{ {
@ -343,12 +352,16 @@ namespace Assets.Scripts.Ble
//设备断开连接 //设备断开连接
public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback) public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback)
{ {
diconnectedCallbacks.Add(peripheral.Address,callback);
BluetoothLEHardwareInterface.DisconnectPeripheral(peripheral.Address, (address) => BluetoothLEHardwareInterface.DisconnectPeripheral(peripheral.Address, (address) =>
{ {
Debug.Log("断开回调"+ address); Debug.Log("断开回调" + address);
PeripheralDisconnected(address,peripheral); var ble = new WinBlePeripheralInfo(address, string.Empty);
peripheralDisconnectedEvent(this, peripheral, null, true); PeripheralDisconnected(address, ble);
callback?.Invoke(); peripheralDisconnectedEvent(this, ble, null, true);
var currentCallback = diconnectedCallbacks.Where(c => c.Key.Equals(address)).Select(c=>c.Value).FirstOrDefault();
currentCallback?.Invoke();
}); });
} }