Skip to content

About

Demo for sensors and camera for the Mobile Coding class @USTP.

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Android Sensors Showcase & CameraX Demo (MVVM Architecture)

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.


Architecture Overview

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)        │
  └───────────────────────────────────────────────────────────┘

Showcased Features

  1. Ambient Light Sensor (TYPE_LIGHT): Live illuminance (lux) meter with dynamic light/dark status indicators.
  2. Accelerometer (TYPE_ACCELEROMETER): Acceleration values (m/s²), total g-force magnitude, and motion state.
  3. Gyroscope (TYPE_GYROSCOPE): Angular velocity around X, Y, Z axes (rad/s).
  4. Magnetometer (TYPE_MAGNETIC_FIELD): Geomagnetic field strength (μT) and total field vector.
  5. CameraX Preview & Capture (CameraX): Live camera viewfinder (PreviewView) with collapsible power-saving controls and photo capture saved to app storage (.jpg).
  6. Proximity Sensor (TYPE_PROXIMITY): Included as a Student Hands-On Exercise.

Student Exercise: Implement the Proximity Sensor

The Proximity Sensor feature is provided as a hands-on exercise for students to practice extending an existing MVVM codebase.

Integration with Room Database Persistence

In previous sessions, you learned local SQLite database persistence using Room (as demonstrated in Ramon's mp-mc-sample repository).

Connecting Sensors with Room

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:

  1. Persisting Sensor Logs: You can define a Room @Entity (e.g., SensorLogEntity) and @Dao (matching the pattern from mp-mc-sample) to record sensor snapshots or photo timestamps over time.
  2. Repository Integration: Inject the Room DAO into SensorRepository.kt. When onSensorChanged() or onPhotoCaptured() receives new data, the repository can write historical entries directly to Room.
  3. Architecture Benefit: Because MVVM decouples data sources from the UI, adding Room database persistence inside SensorRepository.kt requires zero changes to SensorViewModel.kt or SensorScreen.kt.

How to Run & Test on Emulator

  1. Clone this repository and open it in Android Studio.
  2. Run the app on a Pixel 6A Virtual Device or better (API 30+ recommended).
  3. 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.

Project Structure

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

License & Credits

Developed for educational purposes at USTP. Free for student use and adaptation.

About

Demo for sensors and camera for the Mobile Coding class @USTP.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages