sipp11 5 years ago
parent
commit
602e6e27d6
  1. 11
      app/src/main/AndroidManifest.xml
  2. 116
      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. 148
      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" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </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> </application>
</manifest> </manifest>

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

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

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

@ -11,16 +11,152 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
> app:layout_constraintTop_toTopOf="parent">
<TextView <LinearLayout
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/sensor_list" android:orientation="vertical">
android:text="(placeholder)">main</TextView>
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(placeholder)">main
</TextView>
</LinearLayout>
</ScrollView> </ScrollView>

Loading…
Cancel
Save