save sensor data as json file

This commit is contained in:
2024-12-04 14:57:43 +08:00
parent 20711ea05c
commit 5d1099086b
9 changed files with 144 additions and 31 deletions

View File

@@ -8,15 +8,14 @@ plugins {
android {
namespace = "tw.moonjuice.light_sesnor_capture"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
ndkVersion "25.1.8937393"
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
jvmTarget = 17
}
defaultConfig {

View File

@@ -1,4 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:label="light_sesnor_capture"
android:name="${applicationName}"

View File

@@ -1,20 +1,29 @@
package tw.moonjuice.light_sesnor_capture;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class MainActivity extends FlutterActivity implements EventChannel.StreamHandler, SensorEventListener {
public class MainActivity extends FlutterActivity implements EventChannel.StreamHandler, SensorEventListener, MethodChannel.MethodCallHandler {
private Sensor mLightSensor;
private SensorManager mSensorManager;
private EventChannel.EventSink mEventSink;
@@ -30,24 +39,39 @@ public class MainActivity extends FlutterActivity implements EventChannel.Stream
super.configureFlutterEngine(flutterEngine);
EventChannel eventChannel = new EventChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "tw.moonjuice.light_sensor.stream");
eventChannel.setStreamHandler(this);
MethodChannel methodChannel = new MethodChannel(flutterEngine.getDartExecutor(), "tw.moonjuice.light_sensor.method");
methodChannel.setMethodCallHandler(this);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mLightSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(this);
super.onStop();
}
@Override
public void onListen(Object arguments, EventChannel.EventSink events) {
mEventSink = events;
mSensorManager.registerListener(this, mLightSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
public void onCancel(Object arguments) {
mEventSink = null;
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (mEventSink != null) {
mEventSink.success(sensorEvent.values[0]);
final Map data = new HashMap();
data.put("lux", sensorEvent.values[0]);
data.put("timestamp", sensorEvent.timestamp);
mEventSink.success(data);
}
}
@@ -55,4 +79,22 @@ public class MainActivity extends FlutterActivity implements EventChannel.Stream
public void onAccuracyChanged(Sensor sensor, int i) {
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
switch (call.method) {
case "getExternalDownloadsPath":
result.success(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
break;
case "start":
Toast.makeText(getContext(), "start", Toast.LENGTH_SHORT).show();
result.success("success");
break;
default:
Toast.makeText(getContext(), "stop", Toast.LENGTH_SHORT).show();
result.success("success");
break;
}
}
}

View File

@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-all.zip

View File

@@ -18,8 +18,8 @@ pluginManagement {
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "8.1.0" apply false
id "org.jetbrains.kotlin.android" version "1.8.22" apply false
id "com.android.application" version "8.3.2" apply false
id "org.jetbrains.kotlin.android" version "2.0.20" apply false
}
include ":app"