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; } }
public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState) { base.OnConnectionStateChange(gatt, status, newState); IDevice device; Trace.Message($"OnConnectionStateChange: GattStatus: {status}"); switch (newState) { // disconnected case ProfileState.Disconnected: if (_adapter.DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device)) { Trace.Message("Disconnected by user"); //Found so we can remove it _adapter.DeviceOperationRegistry.Remove(gatt.Device.Address); _adapter.ConnectedDeviceRegistry.Remove(gatt.Device.Address); gatt.Close(); 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, bacause our device was not found in the registry but was still connected if (_adapter.ConnectedDeviceRegistry.TryGetValue(gatt.Device.Address, out device)) { Trace.Message($"Disconnected '{device.Name}' by lost connection"); _adapter.ConnectedDeviceRegistry.Remove(gatt.Device.Address); gatt.Close(); _adapter.HandleDisconnectedDevice(false, device); break; } gatt.Close(); // Close GATT regardless, else we can accumulate zombie gatts. Trace.Message("Disconnect. Device not found in registry. Not raising disconnect/lost event."); break; // connecting case ProfileState.Connecting: Trace.Message("Connecting"); break; // connected case ProfileState.Connected: Trace.Message("Connected"); //Try to find the device in the registry so that the same instance is updated if (_adapter.DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device)) { ((Device)device).Update(gatt.Device, gatt, this); //Found so we can remove it _adapter.DeviceOperationRegistry.Remove(gatt.Device.Address); } else { //only for on auto-reconnect (device is not in operation registry) device = new Device(_adapter, gatt.Device, gatt, this, 0); } if (status != GattStatus.Success) { // The aboe 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}"); gatt.Close(); } else { _adapter.ConnectedDeviceRegistry[gatt.Device.Address] = device; _adapter.HandleConnectedDevice(device); } break; // disconnecting case ProfileState.Disconnecting: Trace.Message("Disconnecting"); break; } }
public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState) { base.OnConnectionStateChange(gatt, status, newState); IDevice device; if (status != GattStatus.Success) { Trace.Message($"OnConnectionStateChange: GattCallback error: {status}"); device = new Device(_adapter, gatt.Device, gatt, this, 0); _adapter.HandleConnectionFail(device, $"GattCallback error: {status}"); // We don't return. Allowing to fall-through to the SWITCH, which will assume a disconnect, close GATT and clean up. // The above error event handles the case where the error happened during a Connect call, which will close out any waiting asyncs. } else { Trace.Message("GattCallback state: {0}", newState.ToString()); } switch (newState) { // disconnected case ProfileState.Disconnected: if (_adapter.DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device)) { Trace.Message("Disconnected by user"); //Found so we can remove it _adapter.DeviceOperationRegistry.Remove(gatt.Device.Address); _adapter.ConnectedDeviceRegistry.Remove(gatt.Device.Address); gatt.Close(); _adapter.HandleDisconnectedDevice(true, device); break; } //connection must have been lost, bacause our device was not found in the registry but was still connected if (_adapter.ConnectedDeviceRegistry.TryGetValue(gatt.Device.Address, out device)) { Trace.Message("Disconnected by lost connection"); _adapter.ConnectedDeviceRegistry.Remove(gatt.Device.Address); gatt.Close(); _adapter.HandleDisconnectedDevice(false, device); break; } gatt.Close(); // Close GATT regardless, else we can accumulate zombie gatts. Trace.Message("Disconnect. Device not found in registry. Not raising disconnect/lost event."); break; // connecting case ProfileState.Connecting: Trace.Message("Connecting"); break; // connected case ProfileState.Connected: Trace.Message("Connected"); //Try to find the device in the registry so that the same instance is updated if (_adapter.DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device)) { ((Device)device).Update(gatt.Device, gatt, this); //Found so we can remove it _adapter.DeviceOperationRegistry.Remove(gatt.Device.Address); } else { //only for on auto-reconnect (device is not in operation registry) device = new Device(_adapter, gatt.Device, gatt, this, 0); } _adapter.ConnectedDeviceRegistry[gatt.Device.Address] = device; _adapter.HandleConnectedDevice(device); break; // disconnecting case ProfileState.Disconnecting: Trace.Message("Disconnecting"); break; } }