Sensor Class

A sensor object of a document (https://w3c.github.io/sensors/#the-sensor-interface): the base of Accelerometer, Gyroscope, Magnetometer, AmbientLightSensor, OrientationSensor, etc. Sensor.Start connects it to the sensor of its type of the DeviceSensorProvider of the browsing context (DeviceSensorsOptions.Provider) after checking the permissions; the reading event is fired when a new reading is available, at most at the requested frequency, while the document is visible and focused.

public abstract class Sensor : EventTarget
Inheritance
EventTarget Sensor

Properties

NameDescription
ActivatedGets whether the sensor is activated: it is started and connected to a sensor of the device.
HasReadingGets whether the sensor has a reading.
OnActivateGets or sets the event handler of the activate event.
OnErrorGets or sets the event handler of the error event.
OnReadingGets or sets the event handler of the reading event.
TimestampGets the time of the latest reading, in milliseconds relative to the time origin of the window, or null.

Activated

Sensor.cs

Gets whether the sensor is activated: it is started and connected to a sensor of the device.

public bool Activated { get; }

HasReading

Sensor.cs

Gets whether the sensor has a reading.

public bool HasReading { get; }

OnActivate

Sensor.cs

Gets or sets the event handler of the activate event.

public Action<Event>? OnActivate { get; set; }

OnError

Sensor.cs

Gets or sets the event handler of the error event.

public Action<Event>? OnError { get; set; }

OnReading

Sensor.cs

Gets or sets the event handler of the reading event.

public Action<Event>? OnReading { get; set; }

Timestamp

Sensor.cs

Gets the time of the latest reading, in milliseconds relative to the time origin of the window, or null.

public double? Timestamp { get; }

Methods

NameDescription
Start()Starts the sensor (https://w3c.github.io/sensors/#sensor-start): the permissions of the sensor type are requested, then the sensor connects to the sensor of its type of the device, and the activate event is fired. The error event is fired with a NotAllowedError when a permission is denied, and with a NotReadableError when the device has no working sensor of the type.
Stop()Stops the sensor (https://w3c.github.io/sensors/#sensor-stop): it no longer has a reading.

Start()

Sensor.cs

Starts the sensor (https://w3c.github.io/sensors/#sensor-start): the permissions of the sensor type are requested, then the sensor connects to the sensor of its type of the device, and the activate event is fired. The error event is fired with a NotAllowedError when a permission is denied, and with a NotReadableError when the device has no working sensor of the type.

public void Start()

Stop()

Sensor.cs

Stops the sensor (https://w3c.github.io/sensors/#sensor-stop): it no longer has a reading.

public void Stop()