36. Sensors and location: plat::sensors#
The device's motion sensors and its location, on Android and in the browser; on Windows and Linux
every sensor is Unavailable (see ROADMAP.md §1 for what a native version would
use). A guard starts what a page needs and dropping it stops it; nothing runs without one, and
the library stops everything while the app is in the background (UIC_SUSPEND 2, a hidden tab,
Android's onStop) and starts it again when it is back. Nothing polls: a new reading wakes the UI
thread and arrives as a command.
use plat::sensors::{self, SENSOR_GRAVITY, SENSOR_MAGNET, SensorState};
// on opening the page (dropping the guard stops them)
self.watch = Some(sensors::watch(SENSOR_GRAVITY | SENSOR_MAGNET));
// App::frame: the latest values
if sensors::state(SENSOR_MAGNET) == SensorState::Unavailable { ui.text(TS_BODY, "Kein Kompass"); }
if let Some(a) = sensors::attitude() {
let heading = a.heading.map_or(String::from("-"), |h| format!("{h:.0}"));
ui.text(TS_BODY, &format!("{:.1} {:.1} {heading}", a.pitch, a.roll));
}
// App::command: UIC_SENSOR with the SENSOR_* bits, UIC_LOCATION with the state: a new frame
fn command(&mut self, ui: &mut Ui, id: u32, _arg: u32) -> bool {
if id == UIC_SENSOR || id == UIC_LOCATION {
ui.invalidate();
return true;
}
false
}
| Function | What it does |
|---|---|
sensors::watch(SENSOR_*) -> SensorWatch | the sensors run while the guard lives (counted per sensor); a sensor the device lacks is no error |
sensors::state(bit) -> SensorState | Off, Waiting (started, no reading yet), Running, Paused, Denied (the manifest, Safari's question), Unavailable |
sensors::read(bit) -> Option<Reading> | the last reading: v (x, y, z; the rotation: a quaternion), accuracy 0 to 3, absolute |
sensors::attitude() -> Option<Attitude> | pitch, roll, side (degrees), heading (0 to 360 clockwise from north: where the top points lying flat, the back when upright), absolute, up, from what runs: gravity from the gravity sensor, the rotation estimate or the accelerometer, heading from the rotation estimate, otherwise from gravity and the magnetic field; attitude_of computes the same from given values |
sensors::location_watch() -> LocationWatch | the location runs while the guard lives; the first start may ask the user, so call it from a user action |
sensors::location_state() -> LocationState | Off, Searching, Fix, Paused, Denied (the manifest or the user; also when the user takes it back), Disabled (the system's switch), Unavailable |
sensors::location_fix() -> Option<Fix> | latitude, longitude, what the provider gave of accuracy, altitude, speed, bearing, source, and age_ms() |
Axes are the device's: x to the right of the screen, y to its top, z out of it. The accelerometer and gravity are in m/s^2 (lying flat, screen up: z = +9.81), the gyroscope in rad/s, the magnetic field in microtesla, the rotation a unit quaternion from the device's axes to east, north, up.
| Sensors | Location | |
|---|---|---|
Android (android/GallerySensors.java) | SensorManager at about 30 Hz: accelerometer, gravity, gyroscope, magnetic field, rotation vector (else the game rotation vector, without north) | LocationManager: GPS and network providers, no Google Play services; ACCESS_FINE_LOCATION / ACCESS_COARSE_LOCATION in the app's manifest, asked at the first start; "approximate" gives the network provider only; a permission taken back shows as Denied when the app is back |
Browser (web/sensors.js) | devicemotion, deviceorientation (absolute with deviceorientationabsolute or Safari's webkitCompassHeading), Safari's requestPermission from the click, or the next tap when it wants a gesture of its own; no magnetic field; what gave nothing after 1.5 s is Unavailable until an event says otherwise. The sign of iOS's acceleration is corrected (not run on an iPhone yet) | navigator.geolocation.watchPosition; the permission's change event (taken back: Denied, given again: it runs on); needs a secure context |
| Windows, Linux | Unavailable | Unavailable |
sensors and location are rights of the manifest, with ask lines like the others
(PERMISSIONS.md): without them state says Denied, stderr PERMDENY sensors,
and after an ask line's answer (UIC_PERMISSION) the app starts the guard again.
Scripts feed readings (sensor ..., location ..., §12's table) so pages can be tested anywhere:
apps/androidGallery/tests/level.uis, attitude.uis, compass.uis, gps.uis; in the browser
sensors_web.mjs (Playwright: geolocation, Chromium's virtual sensors, dispatched events), on an
emulator or a real phone android_sensors.py; plattest --test sensors checks the arithmetic
without hardware.
On Android, plat::back_trap(on) makes the back key an Esc for the app (a page goes back on
UIC_ESCAPE); ui.quit() ends the activity and its process there (plat::app_finish, the next
start is a new runtime). The finger is §37.