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 async void CheckMyDevice(Device device) { await device.UpdateRssiAsync(); }
public override void OnReadRemoteRssi(BluetoothGatt gatt, int rssi, GattStatus status) { base.OnReadRemoteRssi(gatt, rssi, status); Trace.Message("OnReadRemoteRssi: device {0} status {1} value {2}", gatt.Device.Name, status, rssi); IDevice device; if (!_adapter.ConnectedDeviceRegistry.TryGetValue(gatt.Device.Address, out device)) { device = new Device(_adapter, gatt.Device, gatt, this, rssi); Trace.Message("Rssi updated for device not in connected list. This should not happen."); } RemoteRssiRead?.Invoke(this, new RssiReadCallbackEventArgs(device, GetExceptionFromGattStatus(status), rssi)); }