sipp11 5 years ago
parent
commit
602e6e27d6
  1. 11
      app/src/main/AndroidManifest.xml
  2. 102
      app/src/main/java/com/example/sensorz/MainActivity.kt
  3. 27
      app/src/main/java/com/example/sensorz/MyBroadcastReceiver.kt
  4. 92
      app/src/main/java/com/example/sensorz/SensorService.java
  5. 142
      app/src/main/res/layout/activity_main.xml

11
app/src/main/AndroidManifest.xml

@ -16,6 +16,17 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.sensorz.SensorService"
android:enabled="true"></service>
<receiver android:name="MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.sensorz.intent.REBOOT" />
</intent-filter>
</receiver>
</application>
</manifest>

102
app/src/main/java/com/example/sensorz/MainActivity.kt

@ -1,39 +1,70 @@
package com.example.sensorz
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import androidx.appcompat.app.AppCompatActivity
import android.net.ConnectivityManager
import android.os.Bundle
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.widget.TextView
import java.lang.NumberFormatException
import java.lang.StringBuilder
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity(), SensorEventListener {
lateinit var mSensorManager: SensorManager
lateinit var mAccelerometer: Sensor
lateinit var mGyrometer: Sensor
// private var mSensorManager : SensorManager ?= null
// private var mAccelerometer : Sensor ?= null
lateinit var mIntentService: Intent
lateinit var mSensorService: SensorService
lateinit var mBcReceiver : MyBroadcastReceiver
private fun isMyServiceRunning(serviceClass: Class<*>): Boolean {
val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.name == service.service.className) {
Log.i("isMyServiceRunning?", true.toString() + "")
return true
}
}
Log.i("isMyServiceRunning?", false.toString() + "")
return false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mBcReceiver = MyBroadcastReceiver()
val filter = IntentFilter().apply {
addAction("com.example.sensorz.intent.REBOOT")
}
// val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION).apply {
//// addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED)
// }
registerReceiver(mBcReceiver, filter)
val tv: TextView = findViewById(R.id.sensor_list)
mSensorService = SensorService(applicationContext)
mIntentService = Intent(applicationContext, mSensorService::class.java)
if (!isMyServiceRunning(mSensorService.javaClass)) {
startService(mIntentService)
}
mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensorList: List<Sensor> = mSensorManager.getSensorList(Sensor.TYPE_ALL) as List<Sensor>
val text = StringBuilder()
for (x in sensorList)
text.append(x.name).append("\n")
tv.setText(text)
tv.text = text
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
@ -41,6 +72,7 @@ class MainActivity : AppCompatActivity(), SensorEventListener {
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
val txt = sensor.toString() + " | " + accuracy.toString()
Log.i("SENSOR", txt)
@ -51,25 +83,49 @@ class MainActivity : AppCompatActivity(), SensorEventListener {
// with t, the low-pass filter's time-constant
// and dT, the event delivery rate
fun Double.format(digits: Int) = java.lang.String.format("%.${digits}f", this)
val accelGravity = arrayListOf<Double>(0.toDouble(), 0.toDouble(), 0.toDouble())
when (event.sensor?.type) {
Sensor.TYPE_ACCELEROMETER -> {
val accelLin = arrayOf(0.toDouble(), 0.toDouble(), 0.toDouble())
val alpha = 0.8f
accelGravity[0] = alpha * accelGravity[0] + (1 - alpha) * event.values[0]
accelGravity[1] = alpha * accelGravity[1] + (1 - alpha) * event.values[1]
accelGravity[2] = alpha * accelGravity[2] + (1 - alpha) * event.values[2]
accelLin[0] = event.values[0] - accelGravity[0]
accelLin[1] = event.values[1] - accelGravity[1]
accelLin[2] = event.values[2] - accelGravity[2]
findViewById<TextView>(R.id.accel_x).text = accelLin[0].format(3)
findViewById<TextView>(R.id.accel_y).text = accelLin[1].format(3)
findViewById<TextView>(R.id.accel_z).text = accelLin[2].format(3)
}
val alpha = 0.8.toFloat()
val gravity = arrayOf(0.toFloat(), 0.toFloat(), 0.toFloat())
val linear_acceleration = arrayOfNulls<Float>(3)
gravity[0] = alpha * gravity[0] + (1 - alpha) * event!!.values[0]
gravity[1] = alpha * gravity[1] + (1 - alpha) * event!!.values[1]
gravity[2] = alpha * gravity[2] + (1 - alpha) * event!!.values[2]
Sensor.TYPE_GYROSCOPE -> {
findViewById<TextView>(R.id.gyro_x).text = event.values[0].toDouble().format(3)
findViewById<TextView>(R.id.gyro_y).text = event.values[1].toDouble().format(3)
findViewById<TextView>(R.id.gyro_z).text = event.values[2].toDouble().format(3)
}
}
linear_acceleration[0] = event.values[0] - gravity[0]
linear_acceleration[1] = event.values[1] - gravity[1]
linear_acceleration[2] = event.values[2] - gravity[2]
val txt = "CHANGE: " + event.sensor.name + linear_acceleration[0] + ", " + linear_acceleration[1] + ", " + linear_acceleration[2]
Log.i("SENSOR", txt)
// val alpha = 0.8.toFloat()
// val gravity = arrayOf(0.toFloat(), 0.toFloat(), 0.toFloat())
// val linear_acceleration = arrayOfNulls<Float>(3)
//
// gravity[0] = alpha * gravity[0] + (1 - alpha) * event!!.values[0]
// gravity[1] = alpha * gravity[1] + (1 - alpha) * event!!.values[1]
// gravity[2] = alpha * gravity[2] + (1 - alpha) * event!!.values[2]
//
// linear_acceleration[0] = event.values[0] - gravity[0]
// linear_acceleration[1] = event.values[1] - gravity[1]
// linear_acceleration[2] = event.values[2] - gravity[2]
//
// val txt = "CHANGE: " + event.sensor.name + linear_acceleration[0] + ", " + linear_acceleration[1] + ", " + linear_acceleration[2]
// Log.i("SENSOR", txt)
}
override fun onResume() {
super.onResume()
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL)
@ -80,5 +136,11 @@ class MainActivity : AppCompatActivity(), SensorEventListener {
super.onPause()
mSensorManager.unregisterListener(this)
}
override fun onDestroy() {
stopService(mIntentService)
unregisterReceiver(mBcReceiver)
super.onDestroy()
}
}

27
app/src/main/java/com/example/sensorz/MyBroadcastReceiver.kt

@ -0,0 +1,27 @@
package com.example.sensorz
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
private const val TAG = "MyBroadcastReceiver"
class MyBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
StringBuilder().apply {
append("Action: ${intent.action}\n")
append("URI: ${intent.toUri(Intent.URI_INTENT_SCHEME)}\n")
toString().also { log ->
Log.d(TAG, log)
Toast.makeText(context, log, Toast.LENGTH_LONG).show()
}
}
val intent = Intent(context, SensorService::class.java)
context.startService(intent)
}
}

92
app/src/main/java/com/example/sensorz/SensorService.java

@ -0,0 +1,92 @@
package com.example.sensorz;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import java.util.Timer;
import java.util.TimerTask;
public class SensorService extends Service {
public int counter = 0;
public SensorService(Context applicationContext) {
super();
Log.i("HERE", "here I am!");
}
public SensorService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("EXIT", "ondestroy!");
Intent bIntent = new Intent(this, MyBroadcastReceiver.class);
bIntent.setAction("com.example.sensorz.intent.REBOOT");
sendBroadcast(bIntent);
// Intent bIntent = new Intent(this, Rebot)
// Intent broadcastIntent = new Intent(this, Senso.class);
// sendBroadcast(broadcastIntent);
// Intent bIntent = new Intent("this.is.godsent");
// sendBroadcast(bIntent);
stoptimertask();
}
private Timer timer;
private TimerTask timerTask;
long oldTime = 0;
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//schedule the timer, to wake up every 1 second
timer.schedule(timerTask, 1000, 1000); //
}
/**
* it sets the timer to print the counter every x seconds
*/
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
Log.i("in timer", "in timer ++++ " + (counter++));
}
};
}
/**
* not needed
*/
public void stoptimertask() {
Log.i("EXIT", "stop Timeer Task!");
//stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

142
app/src/main/res/layout/activity_main.xml

@ -11,16 +11,152 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Accelerometer (m/s2)" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#75A7A7A7"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:layout_weight="1"
android:text="X:" />
<TextView
android:id="@+id/accel_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text=""
android:textAlignment="textEnd" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#75A7A7A7"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:layout_weight="1"
android:text="Y:" />
<TextView
android:id="@+id/accel_y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text=""
android:textAlignment="textEnd" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#75A7A7A7"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:layout_weight="1"
android:text="Z:" />
<TextView
android:id="@+id/accel_z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text=""
android:textAlignment="textEnd" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Gyro (rad/s)" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#75A7A7A7"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:layout_weight="1"
android:text="X:" />
<TextView
android:id="@+id/gyro_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text=""
android:textAlignment="textEnd" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#75A7A7A7"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:layout_weight="1"
android:text="Y:" />
<TextView
android:id="@+id/gyro_y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text=""
android:textAlignment="textEnd" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#75A7A7A7"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:layout_weight="1"
android:text="Z:" />
<TextView
android:id="@+id/gyro_z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text=""
android:textAlignment="textEnd" />
</LinearLayout>
<TextView
android:id="@+id/sensor_list"
android:text="(placeholder)">main</TextView>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(placeholder)">main
</TextView>
</LinearLayout>
</ScrollView>

Loading…
Cancel
Save