Skip to content

provides live Sensors/States data for Android and iOS targets

Notifications You must be signed in to change notification settings

shadmanadman/KSensor

Repository files navigation

Kotlin Gradle License

Getting Started

Adding dependencies

Add it in your commonMain.dependencies :

implementation("io.github.shadmanadman:KSensor:2.0.4")

Sensors Observation

  • Create a list of sensors you need to observe.
val sensors = listof(
SensorType.ACCELEROMETER,
SensorType.GYROSCOPE,
SensorType.MAGNETOMETER,
SensorType.BAROMETER,
SensorType.STEP_COUNTER,
SensorType.LOCATION,
SensorType.DEVICE_ORIENTATION,
SensorType.PROXIMITY,
SensorType.LIGHT,
SensorType.TOUCH_GESTURES)
  • Register sensors for observation.
KSensor.registerSensors(
    types = sensors,
    locationIntervalMillis = 1000L // Optional
).collect { sensorUpdate ->
    when (sensorUpdate) {
        is SensorUpdate.Data -> // println(it.data, it.platformType)
        is SensorUpdate.Error -> // Get errors here
    }
}
  • Unregister sensors when no longer needed.
KSensor.unregisterSensors(sensors)

Each SensorUpdate has a platformType so you know the sensor data comes from Android or iOS.

  • Sensor Data Models, represents the sensorUpdate.data:
Accelerometer(val x: Float, val y: Float,val z: Float)
Gyroscope(val x: Float, val y: Float, val z: Float)
Magnetometer(val x: Float, val y: Float, val z: Float)
Barometer(val pressure: Float)
StepCounter(val steps: Int)
Location(val lat: Double? = null, val lon: Double? = null, val alt: Double? = null)
Orientation(val orientation: DeviceOrientation,val orientationInt: Int = 0)
Proximity(val distanceInCM: Float, val isNear: Boolean)
LightIlluminance(val illuminance: Float)
TouchGestures(val x: Float,val y: Float,val type: TouchGestureType)

States Observation

  • Just like sensors, create a list of states that you need to observe.
val states = listOf(
StateType.APP_VISIBILITY,
StateType.CONNECTIVITY,
StateType.ACTIVE_NETWORK,
StateType.LOCATION,
StateType.SCREEN_STATE
StateType.VOLUME,
StateType.LOCALE)
  • Add observers.
KState.addObserver(types = states).collect{ stateUpdate->
   when(stateUpdate){
	is StateUpdate.Data-> // println(it.data, it.platformType)
	is StateUpdate.Error-> // Get errors here
   }
}
  • Remove observer when no longer needed.
KState.removeObserver(states)

Each StateUpdate has a platformType so you know the state data comes from Android or iOS.

  • State Data Models, represent the StateUpdate.data:
AppVisibilityStatus(val isAppVisible: Boolean)
LocationStatus(val isLocationOn: Boolean)
ScreenStatus(val isScreenOn: Boolean)
CurrentActiveNetwork(val activeNetwork: ActiveNetwork)
ConnectivityStatus(val isConnected: Boolean)
VolumeStatus(val volumePercentage: Int)
LocaleInfo(
val languageCode: String,
val countryCode: String,
val fullLocaleString: String,
val displayName: String,
val isRTL: Boolean
) 

Permissions

If you are observing location you need FINE_LOCATION and COARSE_LOCATION permissions on Android. You can handel this permissions yourself or let the library handle them for you:

    //Put this in AndroidManifest
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  • Inside a composable call:
KSensor.HandelPermissions() { status ->
    when (status) {
        PermissionStatus.Granted -> println("Permission Granted")
        PermissionStatus.Denied -> println("Permission Denied")
    }
}

Note that the iOS location permission is handled by the library itself.

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

provides live Sensors/States data for Android and iOS targets

Topics

Resources

Stars

Watchers

Forks

Contributors 5

Languages