Skip to content

Accelerometer

Streams raw accelerometer readings, which describe the acceleration of the device, in m/s^2, including the effects of gravity.

Unlike UserAccelerometer, this service reports raw data from the accelerometer (physical sensor embedded in the mobile device) without any post-processing.

The accelerometer is unable to distinguish between the effect of an accelerated movement of the device and the effect of the surrounding gravitational field. This means that, at the surface of Earth, even if the device is completely still, the reading of Accelerometer is an acceleration of intensity 9.8 directed upwards (the opposite of the graviational acceleration). This can be used to infer information about the position of the device (horizontal/vertical/tilted). Accelerometer reports zero acceleration if the device is free falling.

Note

Supported platforms: Android, iOS, Web. Web ignores requested sampling intervals and iOS apps must declare NSMotionUsageDescription.

Inherits: Service

Properties

Events

Examples#

import flet as ft


def main(page: ft.Page):
    intro = ft.Text("Move your device to see accelerometer readings.")
    reading = ft.Text("Waiting for data...")

    def handle_reading(e: ft.AccelerometerReadingEvent):
        reading.value = f"x={e.x:.2f} m/s^2, y={e.y:.2f} m/s^2, z={e.z:.2f} m/s^2"
        page.update()

    def handle_error(e: ft.SensorErrorEvent):
        page.add(ft.Text(f"Accelerometer error: {e.message}"))

    page.session.store.set(
        "accelerometer_service",
        ft.Accelerometer(
            on_reading=handle_reading,
            on_error=handle_error,
            interval=ft.Duration(milliseconds=100),
            cancel_on_error=False,
        ),
    )

    page.add(intro, reading)


ft.run(main)

Properties#

cancel_on_error class-attribute instance-attribute #

cancel_on_error: bool = True

Whether the stream subscription should cancel on the first sensor error.

enabled class-attribute instance-attribute #

enabled: bool = True

Whether the sensor should be sampled. Disable to stop streaming.

interval class-attribute instance-attribute #

interval: Duration | None = None

Desired sampling interval provided as a Duration. Defaults to 200 ms.

Note that mobile platforms treat this value as a suggestion and the actual rate can differ depending on hardware and OS limitations.

Events#

on_error class-attribute instance-attribute #

on_error: EventHandler[SensorErrorEvent] | None = None

Fired when the platform reports a sensor error (for example when the device does not expose the accelerometer). event.message contains the error text.

on_reading class-attribute instance-attribute #

on_reading: (
    EventHandler[AccelerometerReadingEvent] | None
) = None

Fires when a new reading is available.

event exposes x, y, z acceleration values and timestamp (microseconds since epoch).