Compare commits
17 Commits
f8f1fd23fb
...
b9c66f7533
Author | SHA1 | Date | |
---|---|---|---|
![]() |
b9c66f7533 | ||
![]() |
7e1143880e | ||
![]() |
81c3542654 | ||
![]() |
d33ab299ab | ||
![]() |
8a5d720685 | ||
![]() |
90aea5b1bc | ||
![]() |
8a357e0d26 | ||
![]() |
83268c763b | ||
![]() |
3d02d8d4c0 | ||
![]() |
cafe763c4a | ||
![]() |
2cff334056 | ||
![]() |
6c4905d0e7 | ||
![]() |
402b73e4bd | ||
![]() |
9e5b669986 | ||
![]() |
8275d8bd5a | ||
![]() |
4999f1b998 | ||
![]() |
620528302e |
3
Notiz.txt
Normal file
3
Notiz.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Sensor als Service einrichten, der aus Activity heraus gestartet werden kann.
|
||||
Stichwort: Intent
|
||||
Siehe Skript Teil 1
|
@ -0,0 +1,108 @@
|
||||
package com.example.ueberwachungssystem.Detection;
|
||||
|
||||
import static java.lang.Math.sqrt;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
|
||||
|
||||
/**
|
||||
* Accelerometer inherits some methods from abstract Detector class (more info there)
|
||||
*
|
||||
*
|
||||
* USE FROM MAIN ACTIVITY:
|
||||
*
|
||||
* Accelerometer beschleunigungssensor = new Accelerometer(this);
|
||||
* onCreate:
|
||||
* //Accelerometer Setup
|
||||
* beschleunigungssensor = new Accelerometer(this, logger, textViewLog); //logger and textview only for debugging necessary
|
||||
* beschleunigungssensor.getSensor();
|
||||
*
|
||||
* //Starting Detection:
|
||||
* beschleunigungssensor.startDetection();
|
||||
* //Stopping Detection: also recommended at onPause to avoid unnecessary battery consumption
|
||||
* beschleunigungssensor.stopDetection();
|
||||
*
|
||||
* */
|
||||
|
||||
public class Accelerometer extends Detector implements SensorEventListener {
|
||||
|
||||
public SensorManager sensorManager;
|
||||
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION;
|
||||
private Sensor accelerometer;
|
||||
private Context context;
|
||||
boolean alarm = false;
|
||||
//Preallocate memory for the data of each axis of the acceleration sensor
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float betrag; //Betrag aller drei Achsen sqrt(x*x + y*y + z*z)
|
||||
private DetectionReport detectionReport;
|
||||
|
||||
// In constructor pass Activity, Context and TextView from MainActivity in Accelerometer class
|
||||
public Accelerometer(Context context){
|
||||
super(); //von Detektor
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void getSensor(){
|
||||
sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
|
||||
if(sensorManager.getSensorList(sensorType).size()==0) {
|
||||
accelerometer = null;
|
||||
}
|
||||
else {
|
||||
accelerometer = sensorManager.getSensorList(sensorType).get(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
try {
|
||||
checkAlarm(event);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void checkAlarm (SensorEvent event) throws InterruptedException {
|
||||
x = event.values[0];
|
||||
y = event.values[1];
|
||||
z = event.values[2];
|
||||
betrag = (float) sqrt(x*x + y*y + z*z);
|
||||
float threshold = 1.5F;
|
||||
|
||||
if (!alarm) {
|
||||
if (betrag > threshold) {
|
||||
alarm = true;
|
||||
reportViolation("Bewegung", betrag);
|
||||
}
|
||||
} else {
|
||||
if (betrag < threshold) {
|
||||
alarm = false;
|
||||
} else {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startDetection() {
|
||||
// entspricht void start()
|
||||
//getSensor();
|
||||
if (accelerometer != null) {
|
||||
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopDetection() {
|
||||
// entspricht void stop()
|
||||
sensorManager.unregisterListener(this, accelerometer);
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
plugins {
|
||||
id 'com.android.application' version '7.4.2' apply false
|
||||
id 'com.android.library' version '7.4.2' apply false
|
||||
id 'com.android.application' version '8.0.0' apply false
|
||||
id 'com.android.library' version '8.0.0' apply false
|
||||
}
|
@ -18,4 +18,6 @@ android.useAndroidX=true
|
||||
# Enables namespacing of each library's R class so that its R class includes only the
|
||||
# resources declared in the library itself and none from the library's dependencies,
|
||||
# thereby reducing the size of the R class for that library
|
||||
android.nonTransitiveRClass=true
|
||||
android.nonTransitiveRClass=true
|
||||
android.defaults.buildfeatures.buildconfig=true
|
||||
android.nonFinalResIds=false
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,6 +1,6 @@
|
||||
#Thu May 11 15:04:30 CEST 2023
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
|
||||
distributionPath=wrapper/dists
|
||||
zipStorePath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
|
Loading…
x
Reference in New Issue
Block a user