/// <summary> /// Handles lidgren connection close event (fires Closed event). /// </summary> public override void OnConnectionClose() { if (!gracefulClosing && !attemptingReconnection) { ConnectionInterrupted?.Invoke(); attemptingReconnection = true; PausedConnectionResponse response = new PausedConnectionResponse { ReconnectHost = Host, ReconnectPort = Port }; while (!gracefulClosing && failures < RetryAttempts) { response.FailedAttempts = failures; ConnectionPaused?.Invoke(response); if (response.Abort) { break; } NetConnection newConnection = Client.Connect(response.ReconnectHost, response.ReconnectPort); try { newConnection.WaitForConnectionToOpen(); RestoreConnection(newConnection, response); ConnectionRestored?.Invoke(); return; } catch (ConnectionOpenException) { failures++; } } ConnectionAborted?.Invoke(); AbortConnection(); } }
public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState) { base.OnConnectionStateChange(gatt, status, newState); if (!gatt.Device.Address.Equals(_device.BluetoothDevice.Address)) { Trace.Message($"Gatt callback for device {_device.BluetoothDevice.Address} was called for device with address {gatt.Device.Address}. This shoud not happen. Please log an issue."); return; } //ToDo ignore just for me Trace.Message($"References of parent device and gatt callback device equal? {ReferenceEquals(_device.BluetoothDevice, gatt.Device).ToString().ToUpper()}"); Trace.Message($"OnConnectionStateChange: GattStatus: {status}"); switch (newState) { // disconnected case ProfileState.Disconnected: // Close GATT regardless, else we can accumulate zombie gatts. CloseGattInstances(gatt); if (_device.IsOperationRequested) { Trace.Message("Disconnected by user"); //Found so we can remove it _device.IsOperationRequested = false; _adapter.ConnectedDeviceRegistry.Remove(gatt.Device.Address); if (status != GattStatus.Success) { // The above error event handles the case where the error happened during a Connect call, which will close out any waiting asyncs. // Android > 5.0 uses this switch branch when an error occurs during connect Trace.Message($"Error while connecting '{_device.Name}'. Not raising disconnect event."); _adapter.HandleConnectionFail(_device, $"GattCallback error: {status}"); } else { //we already hadled device error so no need th raise disconnect event(happens when device not in range) _adapter.HandleDisconnectedDevice(true, _device); } break; } //connection must have been lost, because the callback was not triggered by calling disconnect Trace.Message($"Disconnected '{_device.Name}' by lost connection"); _adapter.ConnectedDeviceRegistry.Remove(gatt.Device.Address); _adapter.HandleDisconnectedDevice(false, _device); // inform pending tasks ConnectionInterrupted?.Invoke(this, EventArgs.Empty); break; // connecting case ProfileState.Connecting: Trace.Message("Connecting"); break; // connected case ProfileState.Connected: Trace.Message("Connected"); //Check if the operation was requested by the user if (_device.IsOperationRequested) { _device.Update(gatt.Device, gatt); //Found so we can remove it _device.IsOperationRequested = false; } else { //ToDo explore this //only for on auto-reconnect (device is not in operation registry) _device.Update(gatt.Device, gatt); } if (status != GattStatus.Success) { // The above error event handles the case where the error happened during a Connect call, which will close out any waiting asyncs. // Android <= 4.4 uses this switch branch when an error occurs during connect Trace.Message($"Error while connecting '{_device.Name}'. GattStatus: {status}. "); _adapter.HandleConnectionFail(_device, $"GattCallback error: {status}"); CloseGattInstances(gatt); } else { _adapter.ConnectedDeviceRegistry[gatt.Device.Address] = _device; _adapter.HandleConnectedDevice(_device); } _device.HandleConnectionCancellation(); break; // disconnecting case ProfileState.Disconnecting: Trace.Message("Disconnecting"); break; } }