Skip to content

ShadAdman/KSensor

Repository files navigation

Kotlin Gradle License

ksensor Poster

KSensor

KSensor is a Kotlin Multiplatform library for observing device sensors and system states. Each sensor or state is grouped into its own plugin, allowing you to include only the features you need. This prevents pulling in unnecessary code and permissions.

All data emitted by plugins is wrapped in a KSensorResponse<T> which includes:

  • data: The actual sensor or state data.
  • platform: The platform type (Android or iOS).
  • timestamp: The system time when the data was collected.

Permissions Handling

Some plugins require system permissions to function. Each plugin exposes a requiredPermissions list indicating what it needs. KSensor provides a PermissionHandler interface in the Core module to help check and request these permissions across platforms.

Core Permission API

interface PermissionHandler {
    fun hasPermission(permission: Permission): Boolean
    suspend fun requestPermission(permission: Permission): Boolean
}

You must ensure that the necessary permissions are granted before starting sensor observations. Each plugin section below lists its required permissions.

For Android, you must add the permissions to the Manifest file manually.


Core Module

The foundation of the library. It is required for all plugins.

Dependency:

implementation("io.github.shadadman:ksensor-core:version")

Motion Sensors Plugin

Provides access to hardware sensors for tracking movement.

Dependency:

implementation("io.github.shadadman:ksensor-sensors-motion:version")

Required Permissions:

  • Android: ACTIVITY_RECOGNITION (Required for Step Counter)
  • iOS: ACTIVITY_RECOGNITION (Motion & Fitness)

Data Models (Wrapped in KSensorResponse):

  • Accelerometer: Accelerometer(values: Vector3)
  • Gyroscope: Gyroscope(values: Vector3)
  • Step Counter: StepCounter(steps: Int)
  • Motion Detector: MotionDetector(type: MotionType) (Detects Walking, Running, Cycling, etc.)

Environment Sensors Plugin

Provides data from sensors that monitor the ambient environment.

Dependency:

implementation("io.github.shadadman:ksensor-sensors-environment:version")

Required Permissions: None

Data Models (Wrapped in KSensorResponse):

  • Barometer: Barometer(pressure: Float)
  • Light: LightIlluminance(illuminance: Float)
  • Proximity: Proximity(distanceInCM: Float, isNear: Boolean)

Positioning Sensors Plugin

Provides location services and spatial orientation data.

Dependency:

implementation("io.github.shadadman:ksensor-sensors-positioning:version")

Required Permissions:

  • Android/iOS: LOCATION

Data Models (Wrapped in KSensorResponse):

  • Location: Location(latitude: Double?, longitude: Double?, altitude: Double?)
  • Magnetometer: Magnetometer(values: Vector3)
  • Orientation: Orientation(orientation: DeviceOrientation, orientationInt: Int)
  • Location Status: LocationStatus(isLocationOn: Boolean)

Interaction Sensors Plugin

Provides high-level data related to user input gestures.

Dependency:

implementation("io.github.shadadman:ksensor-sensors-interaction:version")

Required Permissions: None

Data Models (Wrapped in KSensorResponse):

  • Touch Gestures: TouchGestures(x: Float, y: Float, type: TouchGestureType)

Network States Plugin

Provides information about the network connectivity of the device.

Dependency:

implementation("io.github.shadadman:ksensor-states-network:version")

Required Permissions: None

Data Models (Wrapped in KSensorResponse):

  • Connectivity: ConnectivityStatus(isConnected: Boolean)
  • Active Network: CurrentActiveNetwork(activeNetwork: ActiveNetwork) (Values: WIFI, CELLULAR, NONE)

System States Plugin

Provides access to general device system states like battery and volume.

Dependency:

implementation("io.github.shadadman:ksensor-states-system:version")

Required Permissions: None

Data Models (Wrapped in KSensorResponse):

  • Battery: BatteryStatus(levelPercent: Int?, chargingState: ChargingState, health: BatteryHealth?, temperatureC: Float?)
  • Volume: VolumeStatus(volumePercentage: Int)
  • Locale: LocaleStatus(languageCode: String, countryCode: String, fullLocaleString: String, displayName: String, isRTL: Boolean)
  • Screen: ScreenStatus(isScreenOn: Boolean)
  • Lock: LockStatus(isDeviceLocked: Boolean)
  • Power Save: PowerSaveStatus(isPowerSaveMode: Boolean)

Bluetooth States Plugin

Provides monitoring for BLE connection and discovery events.

Dependency:

implementation("io.github.shadadman:ksensor-states-bluetooth:version")

Required Permissions:

  • Android/iOS: BLUETOOTH

Data Models (Wrapped in KSensorResponse):

  • BLE Connections: BleConnectionStatus(connectedDevices: List<BleDevice>)
  • BLE Discoveries: BleDiscoversStatus(discoveredDevices: List<BleDevice>)
  • BLE Device: BleDevice(id: String, name: String)

Lifecycle States Plugin

Tracks the visibility and lifecycle state of the application.

Dependency:

implementation("io.github.shadadman:ksensor-states-lifecycle:version")

Required Permissions: None

Data Models (Wrapped in KSensorResponse):

  • App Visibility: AppVisibilityStatus(isAppVisible: Boolean)

Basic Usage

  1. Register your plugin implementation.
  2. Use the KSensor registry to retrieve the plugin and observe its data using Kotlin Flow.

Example to observe using State:

@Composable
fun OrientationSampleUsingState() {
    // Register a plugin
    val plugin = remember {
        KSensor.get<PositioningPlugin>(PluginId.POSITIONING)
            ?: createPositioningPlugin().also { KSensor.register(it) }
    }

    // Use state
    val orientation by plugin.orientation().collectAsState(null)

    println("OrientationData as state: ${orientation?.data}")
}

Example to observe using Effect:

@Composable
fun OrientationSampleUsingEffect() {
    // Register a plugin
    val plugin = remember {
        KSensor.get<PositioningPlugin>(PluginId.POSITIONING)
            ?: createPositioningPlugin().also { KSensor.register(it) }
    }

    // Use effect
    LaunchedEffect(plugin) {
        plugin.orientation().collect {
            println("OrientationData in effect: ${it.data}")
        }
    }
}

License

Copyright (c) 2025 KSensor

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

About

Live Sensors/States data from Android & iOS targets

Topics

Resources

Stars

119 stars

Watchers

3 watching

Forks

Contributors

Languages