public async Task SetupNotifications(string bleDeviceId, Dictionary <string, string> telemetryMap)
        {
            BLEDevice = await BLEService.Connect(bleDeviceId);

            TelemetryMap = telemetryMap;
            foreach (var gatt in telemetryMap)
            {
                var telemetryField = gatt.Value;
                var pair           = new GattPair(gatt.Key);
                var service        = await BLEDevice.GetServiceAsync(pair.ServiceId);

                if (service != null) // service could be null if mapping has old values related to other devices
                {
                    var characteristic = await service.GetCharacteristicAsync(pair.CharacteristicId);

                    if (characteristic != null) // like above but for characteristic
                    {
                        if (telemetryField != null)
                        {
                            await BLEService.EnableNotification(characteristic);
                        }
                        else
                        {
                            await BLEService.DisableNotification(characteristic);
                        }
                    }
                }
            }
            MessagingCenter.Send(new ResultMessage <IDevice>(BLEDevice), Constants.BLE_DEVICE_READY);
        }
 public async void OnDataAvailable(object sender, CharacteristicUpdatedEventArgs e)
 {
     var pair         = new GattPair(e.Characteristic);
     var measureField = TelemetryMap[pair.GattKey];
     var value        = e.Characteristic.GetValue();
     //XamarinDevice.BeginInvokeOnMainThread(() =>
     //{
     //    FormattedText.Spans.Add(new Span { Text = $"Sending {measureField}={value}\n", ForegroundColor = Color.Green });
     //});
     //TODO only if device connected
     await DeviceClient.SendTelemetry($"{{\"{measureField}\":{value}}}", null);
 }
        private async void SaveMapping()
        {
            foreach (var service in Services)
            {
                foreach (var characteristic in service.Characteristics)
                {
                    var pair = new GattPair(service.Id, characteristic.Id);
                    MappingStorage.Current.Add(pair.GattKey, characteristic.SelectedMeasure?.FieldName);
                }
            }
            MappingStorage.Current.Save();
            await IoTCentral.Current.StartService(Device.Id.ToString(), MappingStorage.Current.GetAll());

            await Navigation.NavigateTo(new DeviceViewModel(Navigation));
        }