Skip to content
Shiny.NET

Sensors

NuGet package Shiny.AppDeviceBridge.Maui

The sensors bridge streams the device’s motion and environment sensors to the page. It ships in Shiny.AppDeviceBridge.Maui and is added by AddSensorsBridge().

Terminal window
dotnet add package Shiny.AppDeviceBridge.Maui
builder.UseAppDeviceBridge(
bridge => bridge.AddSensorsBridge(),
webApp => { /* … */ }
);
Client C# TypeScript
ISensorsBridge AddSensorsBridgeClient() SensorsBridge

The accelerometer (g), gyroscope (rad/s), magnetometer (µT), compass (degrees from magnetic north), barometer (hPa) and orientation (a quaternion), over .NET MAUI Essentials.

  • Start, then listen. POST sensors/compass with { "speed": "UI", "minIntervalMs": 16 } starts the sensor; readings arrive only as events. Posting again with another speed restarts it; another interval changes only what gets through.
  • Throttled. Readings closer together than minIntervalMs (16 by default, at least 5) are dropped before they reach the page, so Fastest can’t flood the event stream. sensors.shake is never dropped, and fires only while the accelerometer runs.
  • Stopped for you. A sensor stops once nothing listens to its event (the accelerometer keeps running while sensors.shake has a listener), so a page that navigates away or closes doesn’t leave one draining the battery. DELETE sensors stops them all.
  • Platforms. Whatever Essentials offers on Android, iOS, Mac Catalyst and Windows, and only the sensors the device has. GET sensors says which; starting one that isn’t there answers 501. The macOS (AppKit) and Linux (GTK4) heads have none.
const sensors = new SensorsBridge();
sensors.onCompass(({ heading }) => needle.style.rotate = `${-heading}deg`);
await sensors.start("Compass", { speed: "UI", minIntervalMs: 50 });
GET/_bridge/sensors#

Every sensor: whether the device has it, and whether it is running.

C#ISensorsBridge.GetStatusAsyncTSSensorsBridge.getStatus
Response
200 SensorStatus
SensorStatus · 1 field
sensorsSensorInfo[]
POST/_bridge/sensors/{sensor}#

Starts a sensor, or changes the speed of a running one.

C#ISensorsBridge.StartAsyncTSSensorsBridge.start
ParameterInType
sensorpathSensorrequired
Body
application/json · SensorStartRequest
SensorStartRequest · 2 fields
speedSensorSpeedHow often the platform samples.
minIntervalMsnumberReadings closer together than this are dropped before they reach the page, which is what keeps a fast sensor from flooding the event stream. 16 (about 60 a second) by default; never less than 5.
Response
200 SensorInfo
SensorInfo · 5 fields
sensorSensor
supportedbooleanFalse when the device has no such sensor, or the platform offers none; starting it fails with 501.
runningbooleanTrue while it is started and reporting.
speedSensorSpeed | null
minIntervalMsnumber | nullThe least time between readings the page is sent, while running.
DELETE/_bridge/sensors/{sensor}#

Stops a sensor. Stopping one that is not running is not an error.

C#ISensorsBridge.StopAsyncTSSensorsBridge.stop
ParameterInType
sensorpathSensorrequired
Response
204 No content
DELETE/_bridge/sensors#

Stops every sensor.

C#ISensorsBridge.StopAllAsyncTSSensorsBridge.stopAll
Response
204 No content
EVENTsensors.accelerometer#

C#ISensorsBridge.OnAccelerometerAsyncTSSensorsBridge.onAccelerometer
Payload
VectorReading
xnumber
ynumber
znumber
timestampstring
EVENTsensors.gyroscope#

C#ISensorsBridge.OnGyroscopeAsyncTSSensorsBridge.onGyroscope
Payload
VectorReading
xnumber
ynumber
znumber
timestampstring
EVENTsensors.magnetometer#

C#ISensorsBridge.OnMagnetometerAsyncTSSensorsBridge.onMagnetometer
Payload
VectorReading
xnumber
ynumber
znumber
timestampstring
EVENTsensors.compass#

C#ISensorsBridge.OnCompassAsyncTSSensorsBridge.onCompass
Payload
CompassReading
headingnumberDegrees clockwise from magnetic north, 0 to 360.
timestampstring
EVENTsensors.barometer#

C#ISensorsBridge.OnBarometerAsyncTSSensorsBridge.onBarometer
Payload
BarometerReading
pressureHPanumber
timestampstring
EVENTsensors.orientation#

C#ISensorsBridge.OnOrientationAsyncTSSensorsBridge.onOrientation
Payload
OrientationReading
xnumber
ynumber
znumber
wnumber
timestampstring
EVENTsensors.shake#

The device was shaken. Raised only while the accelerometer is running.

C#ISensorsBridge.OnShakeAsyncTSSensorsBridge.onShake
Payload
ShakeDetected
timestampstring

JSON property names are camelCase and enums travel as their names. A ? marks a property the payload may omit;| null one that may be null.

SensorStatusobject#

Every sensor and what it is doing.

sensorsSensorInfo[]
SensorInfoobject#
sensorSensor
supportedbooleanFalse when the device has no such sensor, or the platform offers none; starting it fails with 501.
runningbooleanTrue while it is started and reporting.
speedSensorSpeed | null
minIntervalMsnumber | nullThe least time between readings the page is sent, while running.
Sensorenum#
"Accelerometer""Gyroscope""Magnetometer""Compass""Barometer""Orientation"
SensorSpeedenum#

How often the platform delivers readings. Fastest can be hundreds a second; see minIntervalMs.

"Default""UI""Game""Fastest"
SensorStartRequestobject#
speed?SensorSpeedHow often the platform samples.
minIntervalMs?numberReadings closer together than this are dropped before they reach the page, which is what keeps a fast sensor from flooding the event stream. 16 (about 60 a second) by default; never less than 5.
VectorReadingobject#

An accelerometer, gyroscope or magnetometer reading.

xnumber
ynumber
znumber
timestampstring
CompassReadingobject#
headingnumberDegrees clockwise from magnetic north, 0 to 360.
timestampstring
BarometerReadingobject#
pressureHPanumber
timestampstring
OrientationReadingobject#

The rotation from the reference frame to the device, as a unit quaternion.

xnumber
ynumber
znumber
wnumber
timestampstring
ShakeDetectedobject#

The device was shaken, while the accelerometer is running.

timestampstring

These routes sit under AppDeviceBridgePolicies.Bridges like every other bridge: callers on this device by default. See Security.