public async void LoadCharacteristics() { IEnumerable <ICharacteristic> characteristics = await ServiceInstance.GetCharacteristicsAsync(); foreach (ICharacteristic characteristic in characteristics) { if (characteristic.Id == TemperatureCharacteristicId) { characteristic.ValueUpdated += (sender, e) => { byte[] tempBytes = e.Characteristic.Value; Temperature = (sbyte)(tempBytes.First()); System.Diagnostics.Debug.WriteLine("Temperature: " + " " + Temperature); }; MarkCharacteristicForUpdate(characteristic); } else if (characteristic.Id == TemperaturePeriodCharacteristicId) { byte[] val = await characteristic.ReadAsync(); int period = ConversionHelpers.ByteArrayToShort16BitLittleEndian(val); TemperaturePeriod = period; System.Diagnostics.Debug.WriteLine("Temperature period: " + " " + Temperature); } } StartUpdates(); }
public async void LoadCharacteristics() { IEnumerable <ICharacteristic> characteristics = await ServiceInstance.GetCharacteristicsAsync(); foreach (ICharacteristic characteristic in characteristics) { if (characteristic.Id == MagnetometerCharacteristicId) { characteristic.ValueUpdated += (sender, e) => { byte[] rawBytes = e.Characteristic.Value; if (rawBytes.Length != 6) { throw new InvalidDataException("Magnetometer characteristic should have 6 bytes"); } short rawX = ConversionHelpers.ByteArrayToShort16BitLittleEndian(new byte[] { rawBytes[0], rawBytes[1] }); short rawY = ConversionHelpers.ByteArrayToShort16BitLittleEndian(new byte[] { rawBytes[2], rawBytes[3] }); short rawZ = ConversionHelpers.ByteArrayToShort16BitLittleEndian(new byte[] { rawBytes[4], rawBytes[5] }); X = ((double)rawX) / 1000.0; Y = ((double)rawY) / 1000.0; Z = ((double)rawZ) / 1000.0; }; MarkCharacteristicForUpdate(characteristic); } else if (characteristic.Id == MagnetometerBearingCharacteristicId) { characteristic.ValueUpdated += (sender, e) => { byte[] rawBytes = e.Characteristic.Value; if (rawBytes.Length != 2) { throw new InvalidDataException("Magnetometer Bearing characteristic should have 2 bytes"); } MagnetometerBearing = ConversionHelpers.ByteArrayToShort16BitLittleEndian(rawBytes); }; MarkCharacteristicForUpdate(characteristic); } else if (characteristic.Id == MagnetometerPeriodCharacteristicId) { byte[] val = await characteristic.ReadAsync(); int period = ConversionHelpers.ByteArrayToShort16BitLittleEndian(val); MagnetometerPeriod = period; } } StartUpdates(); }