https://github.com/ricknout/compose-sensors
A thin wrapper around the Android Sensor APIs, designed to make it easier to work with them in Jetpack Compose.
https://github.com/ricknout/compose-sensors
android android-library android-sensors jetpack-compose
Last synced: 25 days ago
JSON representation
A thin wrapper around the Android Sensor APIs, designed to make it easier to work with them in Jetpack Compose.
- Host: GitHub
- URL: https://github.com/ricknout/compose-sensors
- Owner: ricknout
- License: apache-2.0
- Created: 2023-04-17T11:10:03.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-25T08:26:56.000Z (almost 2 years ago)
- Last Synced: 2025-03-25T03:27:30.641Z (about 1 month ago)
- Topics: android, android-library, android-sensors, jetpack-compose
- Language: Kotlin
- Homepage:
- Size: 2.47 MB
- Stars: 99
- Watchers: 1
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Compose Sensors
This library is a thin wrapper around the [Android Sensor APIs](https://developer.android.com/guide/topics/sensors/sensors_overview),
designed to make it easier to work with them in [Jetpack Compose](https://developer.android.com/jetpack/compose).|  |  |  |
|-----------------------------------------|-------------------------|---------------------------------|## Usage
### Dependency
In module `build.gralde.kts` file:
```kotlin
dependencies {
implementation("dev.ricknout.composesensors:composesensors:$version")
}
```The library is available via `mavenCentral()`. Versions can be found on the [releases page](https://github.com/ricknout/compose-sensors/releases).
### Core APIs
The core APIs provide composable access to the Android sensor framework classes:
```kotlin
// Get the SensorManager
val sensorManager = getSensorManager()// Check if a certain type of sensor is available
val available = isSensorAvailable(type = Sensor.TYPE_ACCELEROMETER)// Get a sensor
val sensor = getSensor(type = Sensor.TYPE_ACCELEROMETER)// Remember a sensor value as State that updates as SensorEvents arrive
val sensorValue by rememberSensorValueAsState(type = Sensor.TYPE_ACCELEROMETER) { event ->
// Transform SensorEvents
}
```### Per-sensor APIs
The per-sensor APIs build on the core APIs and provide convenience by:
* Not having to pass the sensor `type` parameter
* Handling the transformation of `SensorEvent`s for each type of sensor, by using the `SensorValue` class```kotlin
// Check if accelerometer sensor is available
val available = isAccelerometerSensorAvailable()// Get accelerometer sensor
val sensor = getAccelerometerSensor()// Remember accelerometer sensor value as State that updates as SensorEvents arrive
val sensorValue by rememberAccelerometerSensorValueAsState()
// Accelerometer sensor values. Also available: sensorValue.timestamp, sensorValue.accuracy
val (x, y, z) = sensorValue.value
```The following sensor types are available:
| Type | Availability | Sensor | Sensor value |
|-------------------------|---------------------------------------|-------------------------------|------------------------------------------------|
| ⏩ Accelerometer | `isAccelerometerSensorAvailable` | `getAccelerometerSensor` | `rememberAccelerometerSensorValueAsState` |
| 🌡️ Ambient Temperature | `isAmbientTemperatureSensorAvailable` | `getAmbientTemperatureSensor` | `rememberAmbientTemperatureSensorValueAsState` |
| ⏬ Gravity | `isGravitySensorAvailable` | `getGravitySensor` | `rememberGravitySensorValueAsState` |
| 🔄 Gyroscope | `isGyroscopeSensorAvailable` | `getGyroscopeSensor` | `rememberGyroscopeSensorValueAsState` |
| 💡 Light | `isLightSensorAvailable` | `getLightSensor` | `rememberLightSensorValueAsState` |
| ⏪ Linear Acceleration | `isLinearAccelerationSensorAvailable` | `getLinearAccelerationSensor` | `rememberLinearAccelerationSensorValueAsState` |
| 🧲 Magnetic field | `isMagneticFieldSensorAvailable` | `getMagneticFieldSensor` | `rememberMagneticFieldSensorValueAsState` |
| 💨 Pressure | `isPressureSensorAvailable` | `getPressureSensor` | `rememberPressureSensorValueAsState` |
| 🔛 Proximity | `isProximitySensorAvailable` | `getProximitySensor` | `rememberProximitySensorValueAsState` |
| 💧 Relative Humidity | `isRelativeHumiditySensorAvailable` | `getRelativeHumiditySensor` | `rememberRelativeHumiditySensorValueAsState` |Watch this space, more types to come!
## License
```
Copyright 2023 Nick RoutLicensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for
additional information regarding copyright ownership. The ASF licenses this
file to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
```