public override void OnConnectionStateChange(BluetoothDevice device, [GeneratedEnum] ProfileState status, [GeneratedEnum] ProfileState newState) { base.OnConnectionStateChange(device, status, newState); bool connected = newState == ProfileState.Connected; ConnectionCompleted?.Invoke(connected); SERVERC("OnConnection device=" + device.Address + " status=" + status.ToString() + " state=" + newState.ToString()); }
public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState) { base.OnConnectionStateChange(gatt, status, newState); IDevice device = null; if (status != GattStatus.Success) { Mvx.TaggedError("OnConnectionStateChange", "GattCallback error: {0}", status); device = new Device(gatt.Device, gatt, this, 0); DeviceConnectionError(this, new DeviceConnectionEventArgs() { Device = device }); // 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 { Mvx.Trace("GattCallback state: {0}", newState.ToString()); } switch (newState) { // disconnected case ProfileState.Disconnected: if (DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device)) { Mvx.Trace("Disconnected by user"); //Found so we can remove it DeviceOperationRegistry.Remove(gatt.Device.Address); ConnectedDeviceRegistry.Remove(gatt.Device.Address); gatt.Close(); DeviceDisconnected(this, new DeviceConnectionEventArgs { Device = device }); break; } //connection must have been lost, bacause our device was not found in the registry but was still connected if (ConnectedDeviceRegistry.TryGetValue(gatt.Device.Address, out device)) { Mvx.Trace("Disconnected by lost connection"); ConnectedDeviceRegistry.Remove(gatt.Device.Address); gatt.Close(); DeviceConnectionLost(this, new DeviceConnectionEventArgs() { Device = device }); break; } gatt.Close(); // Close GATT regardless, else we can accumulate zombie gatts. Mvx.Trace("Disconnect. Device not found in registry. Not raising disconnect/lost event."); break; // connecting case ProfileState.Connecting: Mvx.Trace("Connecting"); break; // connected case ProfileState.Connected: Mvx.Trace("Connected"); //Try to find the device in the registry so that the same instance is updated if (DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device)) { ((Device)device).Update(gatt.Device, gatt, this); //Found so we can remove it DeviceOperationRegistry.Remove(gatt.Device.Address); } else { //only for on auto-reconnect (device is not in operation registry) device = new Device(gatt.Device, gatt, this, 0); } ConnectedDeviceRegistry[gatt.Device.Address] = device; DeviceConnected(this, new DeviceConnectionEventArgs() { Device = device }); break; // disconnecting case ProfileState.Disconnecting: Mvx.Trace("Disconnecting"); break; } }
public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState) { base.OnConnectionStateChange(gatt, status, newState); IDevice device = null; if (status != GattStatus.Success) { Mvx.TaggedError("OnConnectionStateChange", "GattCallback error: {0}", status); DeviceConnectionError(this, new DeviceConnectionEventArgs(){ Device = device }); // 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 { Mvx.Trace("GattCallback state: {0}", newState.ToString()); } switch (newState) { // disconnected case ProfileState.Disconnected: if (DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device)) { Mvx.Trace("Disconnected by user"); //Found so we can remove it DeviceOperationRegistry.Remove(gatt.Device.Address); ConnectedDeviceRegistry.Remove(gatt.Device.Address); gatt.Close(); DeviceDisconnected(this, new DeviceConnectionEventArgs { Device = device }); break; } //connection must have been lost, bacause our device was not found in the registry but was still connected if (ConnectedDeviceRegistry.TryGetValue(gatt.Device.Address, out device)) { Mvx.Trace("Disconnected by lost connection"); ConnectedDeviceRegistry.Remove(gatt.Device.Address); gatt.Close(); DeviceConnectionLost(this, new DeviceConnectionEventArgs() { Device = device }); break; } gatt.Close(); // Close GATT regardless, else we can accumulate zombie gatts. Mvx.Trace("Disconnect. Device not found in registry. Not raising disconnect/lost event."); break; // connecting case ProfileState.Connecting: Mvx.Trace("Connecting"); break; // connected case ProfileState.Connected: Mvx.Trace("Connected"); //Try to find the device in the registry so that the same instance is updated if (DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device)) { ((Device)device).Update(gatt.Device, gatt, this); //Found so we can remove it DeviceOperationRegistry.Remove(gatt.Device.Address); } else { //only for on auto-reconnect (device is not in operation registry) device = new Device(gatt.Device, gatt, this, 0); } ConnectedDeviceRegistry[gatt.Device.Address] = device; DeviceConnected(this, new DeviceConnectionEventArgs() { Device = device }); break; // disconnecting case ProfileState.Disconnecting: Mvx.Trace("Disconnecting"); break; } }