Magnetometer
Tip
This article covers Uno-specific information for Magnetometer. For a full description of the feature and instructions on using it, see Magnetometer Class.
- The
Windows.Devices.Sensors.Magnetometer
class allows measuring magnetic force affecting the device.
Supported features
Feature | Windows | Android | iOS | Web (WASM) | macOS | Linux (Skia) | Win 7 (Skia) |
---|---|---|---|---|---|---|---|
GetDefault |
✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
ReadingChanged |
✔ | ✔ | ✔ | ✔ | ✖ | ✖ | ✖ |
ReportInterval |
✔ | ✔ | ✔ | ✔ | ✖ | ✖ | ✖ |
Using Magnetometer with Uno
- The
GetDefault
method is available on all targets and will returnnull
on those which do not supportMagnetometer
or devices which do not have such sensor. - Ensure to unsubscribe from the
ReadingChanged
event when you no longer need the readings, so that the sensor is no longer active to avoid unnecessary battery consumption. ReportInterval
property on WASM is currently not supported directly. Uno uses an approximation in the form of raising theReadingChanged
event, only when enough time has passed since the last report. The event is raised a bit more often to make sure the gap caused by the filter is not too large, but this is in-line with the behavior of Windows'Magnetometer
.DirectionalAccuracy
is not reported on iOS, so it will always returnUnknown
.
Example
Capturing sensor readings
var magnetometer = Magnetometer.GetDefault();
magnetometer.ReadingChanged += Magnetometer_ReadingChanged;
private async void Magnetometer_ReadingChanged(Magnetometer sender, MagnetometerReadingChangedEventArgs args)
{
// If you want to update the UI in some way, ensure the Dispatcher is used,
// as the ReadingChanged event handler does not run on the UI thread.
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
MagneticFieldX = args.Reading.MagneticFieldX;
MagneticFieldY = args.Reading.MagneticFieldY;
MagneticFieldZ = args.Reading.MagneticFieldZ;
DirectionalAccuracy = args.Reading.DirectionalAccuracy;
Timestamp = args.Reading.Timestamp.ToString("R");
});
}
Unsubscribing from the readings
magnetometer.ReadingChanged -= Magnetometer_ReadingChanged;