设备断开优化
This commit is contained in:
parent
63987511e7
commit
2274a13619
@ -53,11 +53,12 @@ namespace Assets.Scripts.Devices.Ble
|
||||
|
||||
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))
|
||||
{
|
||||
return;
|
||||
}
|
||||
Debug.Log("disconnected event");
|
||||
if (this.State != DeviceState.Disconnected)
|
||||
{
|
||||
this.State = DeviceState.Disconnected;
|
||||
|
||||
@ -31,6 +31,7 @@ namespace Assets.Scripts.Ble
|
||||
private Action<BleAdvertisementInfo> _discoveredCallback;
|
||||
|
||||
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<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>>();
|
||||
@ -246,6 +247,7 @@ namespace Assets.Scripts.Ble
|
||||
},
|
||||
(address, error) =>
|
||||
{
|
||||
Debug.Log("error device:" + address+ error);
|
||||
BleResponse s = new BleResponse
|
||||
{
|
||||
IsSuccess = false,
|
||||
@ -261,6 +263,7 @@ namespace Assets.Scripts.Ble
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("265 error device:" + address + error);
|
||||
peripheralDisconnectedEvent(this, ble, null, false);
|
||||
}
|
||||
|
||||
@ -320,6 +323,12 @@ namespace Assets.Scripts.Ble
|
||||
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));
|
||||
if (characteristicCallback.Any())
|
||||
{
|
||||
@ -343,12 +352,16 @@ namespace Assets.Scripts.Ble
|
||||
//设备断开连接
|
||||
public void DisconnectPeripheral(BlePeripheralInfo peripheral, Action callback)
|
||||
{
|
||||
diconnectedCallbacks.Add(peripheral.Address,callback);
|
||||
BluetoothLEHardwareInterface.DisconnectPeripheral(peripheral.Address, (address) =>
|
||||
{
|
||||
Debug.Log("断开回调"+ address);
|
||||
PeripheralDisconnected(address,peripheral);
|
||||
peripheralDisconnectedEvent(this, peripheral, null, true);
|
||||
callback?.Invoke();
|
||||
Debug.Log("断开回调" + address);
|
||||
var ble = new WinBlePeripheralInfo(address, string.Empty);
|
||||
PeripheralDisconnected(address, ble);
|
||||
peripheralDisconnectedEvent(this, ble, null, true);
|
||||
|
||||
var currentCallback = diconnectedCallbacks.Where(c => c.Key.Equals(address)).Select(c=>c.Value).FirstOrDefault();
|
||||
currentCallback?.Invoke();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user