An educational starter project designed for the Mobile Coding / Mobile Computing course at St. Pölten University of Applied Sciences (USTP).
This project demonstrates how to handle physical hardware sensors (SensorManager), camera inputs (CameraX), and file storage using Google's recommended MVVM (Model-View-ViewModel) architecture with Jetpack Compose.
This application follows the Unidirectional Data Flow pattern presented in class:
┌───────────────────────────────────────────────────────────┐
│ View Layer (Jetpack Compose) │
│ - SensorScreen.kt & CameraPreviewCard.kt │
│ - Collects uiState with collectAsStateWithLifecycle() │
│ - Sends user events up ──────────────────────────────┐ │
└────────────────────────────────────────────────────────┼──┘
│ User Events
▼
┌───────────────────────────────────────────────────────────┐
│ ViewModel Layer (AndroidViewModel) │
│ - SensorViewModel.kt │
│ - Holds & exposes StateFlow<SensorData> │
│ - Survives screen orientation / configuration changes │
└────────────────────────────────────────────────────────┼──┘
│
▼
┌───────────────────────────────────────────────────────────┐
│ Model Layer (Single Source of Truth) │
│ - SensorData.kt (Pure Kotlin Data Class) │
│ - SensorRepository.kt (SensorManager & Storage) │
└───────────────────────────────────────────────────────────┘
- Ambient Light Sensor (
TYPE_LIGHT): Live illuminance (lux) meter with dynamic light/dark status indicators. - Accelerometer (
TYPE_ACCELEROMETER): Acceleration values (m/s²), total g-force magnitude, and motion state. - Gyroscope (
TYPE_GYROSCOPE): Angular velocity around X, Y, Z axes (rad/s). - Magnetometer (
TYPE_MAGNETIC_FIELD): Geomagnetic field strength (μT) and total field vector. - CameraX Preview & Capture (
CameraX): Live camera viewfinder (PreviewView) with collapsible power-saving controls and photo capture saved to app storage (.jpg). - Proximity Sensor (
TYPE_PROXIMITY): Included as a Student Hands-On Exercise.
The Proximity Sensor feature is provided as a hands-on exercise for students to practice extending an existing MVVM codebase.
In previous sessions, you learned local SQLite database persistence using Room (as demonstrated in Ramon's mp-mc-sample repository).
In this project, SensorRepository.kt acts as the single source of truth for live hardware data (SensorManager) and saved photo paths (CameraX). To combine hardware sensors with Room database persistence:
- Persisting Sensor Logs:
You can define a Room
@Entity(e.g.,SensorLogEntity) and@Dao(matching the pattern frommp-mc-sample) to record sensor snapshots or photo timestamps over time. - Repository Integration:
Inject the Room DAO into
SensorRepository.kt. WhenonSensorChanged()oronPhotoCaptured()receives new data, the repository can write historical entries directly to Room. - Architecture Benefit:
Because MVVM decouples data sources from the UI, adding Room database persistence inside
SensorRepository.ktrequires zero changes toSensorViewModel.ktorSensorScreen.kt.
- Clone this repository and open it in Android Studio.
- Run the app on a Pixel 6A Virtual Device or better (API 30+ recommended).
- Open the Extended Controls (
...) window on the emulator toolbar:- Sensors Tab: Adjust the Light slider or tilt the 3D Phone model to see live Accelerometer, Gyroscope, and Light updates.
- Camera: Grant permission on Card 6 to view the 3D Virtual Living Room camera stream and test photo capture.
app/src/main/java/at/ustp/sensorstest/
├── model/
│ ├── SensorData.kt <-- Domain Data Class
│ └── SensorRepository.kt <-- SensorManager & Hardware Logic
├── viewmodel/
│ └── SensorViewModel.kt <-- ViewModel exposing StateFlow
├── ui/
│ ├── SensorScreen.kt <-- Main Jetpack Compose Layout
│ └── CameraPreviewCard.kt <-- Collapsible CameraX Composable
└── MainActivity.kt <-- Entry shell & Logcat sensor listing
Developed for educational purposes at USTP. Free for student use and adaptation.