Barometer

**Overview**

  • Provides atmospheric pressure readings in hPa.
  • Use cases include altitude calculations and weather-related features.
  • **Platform Support**

  • **Supported:** Android, iOS.
  • **Not Supported:** Web, Desktop.
  • **iOS Specific:** Custom sampling intervals are ignored.
  • **iOS Configuration Requirement**

  • To access motion data on iOS, add NSMotionUsageDescription to your pyproject.toml file.
  • Example:
      [tool.flet.ios.info]
      NSMotionUsageDescription = "This app requires access to the barometer to provide altitude information."
      
  • Failure to include this will cause the app to crash when accessing motion data.
  • **Inheritance**

  • Inherits from flet.Service.
  • **Properties**

  • cancel_on_error: bool
  • - Default: True - Description: Determines if the stream subscription should be cancelled upon encountering the first sensor error.
  • enabled: bool
  • - Default: True - Description: Controls whether the sensor should be actively sampled. Setting to False stops the data stream.
  • interval: Duration | None
  • - Default: None (platform-dependent, typically 200ms. iOS ignores custom intervals.) - Description: Specifies the desired sampling interval using a flet.Duration object.

    **Events**

  • on_error: EventHandler[SensorErrorEvent] | None
  • - Description: Triggered when the platform reports a sensor error. The event.message attribute contains the error description.
  • on_reading: EventHandler[BarometerReadingEvent] | None
  • - Description: Triggered when a new barometer reading is available. The event object contains: - pressure: float (in hPa) - timestamp: int (microseconds since epoch)

    **Example Usage**

    import flet as ft

    def main(page: ft.Page): def handle_reading(e: ft.BarometerReadingEvent): reading.value = f"{e.pressure:.2f} hPa" page.update()

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

    page.services.append( ft.Barometer( on_reading=handle_reading, on_error=handle_error, interval=ft.Duration(milliseconds=500), ) )

    page.add( ft.Text("Atmospheric pressure (hPa)."), reading := ft.Text("Waiting for data..."), )

    ft.run(main)