public void ReadTemperature(BECharacteristicModel asciChar, int interval) { timer = new Timer(async x => { await asciChar.ReadUTF8Value(); var val = asciChar.CharacteristicValue; GotTemperatureReading(this, val); }, null, 0, interval * 1000); }
public async void ReadCharacteristics() { try { Debug.WriteLine("Started ReadCharacteristics..."); var services = SelectedDevice.ServiceModels; var deviceservice = services.FirstOrDefault(x => x.Uuid.ToString() == HaccpConstant.DeviceServiceUuid); deviceservice.InitializeCharacteristics(); await deviceservice.ReadCharacteristicsAsync(); var batteryService = services.FirstOrDefault(x => x.Name.ToLower().Contains(HaccpConstant.BatteryServiceName)); batteryService.InitializeCharacteristics(); await batteryService.ReadCharacteristicsAsync(); var tempservice = services.FirstOrDefault(x => x.Uuid.ToString() == HaccpConstant.TemperatureServiceUuid); SelectedService = tempservice; tempservice.InitializeCharacteristics(); await tempservice.ReadCharacteristicsAsync(); var settings = new BleSettings(); if (deviceservice != null && deviceservice.CharacteristicModels != null) { var serialNumChar = deviceservice.CharacteristicModels.FirstOrDefault( x => x.Uuid.ToString().Contains(HaccpConstant.SerialNumberUuid)); if (serialNumChar != null) { settings.SNo = serialNumChar.CharacteristicValue; } Debug.WriteLine("settings.SNo {0}", settings.SNo); } if (batteryService != null && batteryService.CharacteristicModels != null) { var batteryChar = batteryService.CharacteristicModels.FirstOrDefault(); if (batteryChar != null) { settings.BatteryLevel = Convert.ToInt32(batteryChar.CharacteristicValue); } Debug.WriteLine("settings.BatteryLevel {0}", settings.BatteryLevel); } if (tempservice != null && tempservice.CharacteristicModels != null) { probChar = tempservice.CharacteristicModels.FirstOrDefault( x => x.Uuid.ToString() == HaccpConstant.ProbeUuid); if (probChar != null && !string.IsNullOrEmpty(probChar.CharacteristicValue)) { await probChar.ReadUTF8Value(); settings.Prob = probChar.CharacteristicValue; Debug.WriteLine("settings.Prob {0}", settings.Prob); } measurementTimeChar = tempservice.CharacteristicModels.FirstOrDefault( x => x.Uuid.ToString() == HaccpConstant.MeasurementtimingCharacteristicsUuid); if (measurementTimeChar != null && !string.IsNullOrEmpty(measurementTimeChar.CharacteristicValue)) { settings.MeasurementLevel = Convert.ToInt32(measurementTimeChar.CharacteristicValue); } Debug.WriteLine("settings.MeasurementLevel {0}", settings.MeasurementLevel); measurementScaleChar = tempservice.CharacteristicModels.FirstOrDefault( x => x.Uuid.ToString() == HaccpConstant.MeasurementScaleUuid); if (measurementScaleChar != null) { settings.Scale = measurementScaleChar.CharacteristicValue == "67" ? (short)0 : (short)1; } Debug.WriteLine("settings.Scale {0}", settings.Scale); autooffChar = tempservice.CharacteristicModels.FirstOrDefault( x => x.Uuid.ToString() == HaccpConstant.AutoOffIntervalUuid); if (autooffChar != null && !string.IsNullOrEmpty(autooffChar.CharacteristicValue)) { settings.AutoOff = Convert.ToInt32(autooffChar.CharacteristicValue); } Debug.WriteLine("settings.AutoOff {0}", settings.AutoOff); sleepChar = tempservice.CharacteristicModels.FirstOrDefault( x => x.Uuid.ToString() == HaccpConstant.SleepTimeUuid); if (sleepChar != null && !string.IsNullOrEmpty(sleepChar.CharacteristicValue)) { settings.Sleep = Convert.ToInt32(sleepChar.CharacteristicValue); } Debug.WriteLine("settings.Sleep {0}", settings.Sleep); disConnectPeripheralChar = tempservice.CharacteristicModels.FirstOrDefault( x => x.Uuid.ToString().Contains(HaccpConstant.DiscoonectPeripheralUuid)); resetAutoOffChar = tempservice.CharacteristicModels.FirstOrDefault( x => x.Uuid.ToString().Contains(HaccpConstant.ResetAutooffUuid)); var aschiChar = tempservice.CharacteristicModels.FirstOrDefault( x => x.Uuid.ToString().Contains(HaccpConstant.AsciiTemperatureUuid)); ReadTemperature(aschiChar, settings.MeasurementLevel); } IsBusy = false; BLESettingsUpdated(this, new BLESettingsUpdated(settings)); } catch (Exception ex) { IsBusy = false; Debug.WriteLine("Error reading characteristics {0}", ex.Message); } }