From 5d1099086b45c816a1613482517b576be8573998 Mon Sep 17 00:00:00 2001 From: moonjuice Date: Wed, 4 Dec 2024 14:57:43 +0800 Subject: [PATCH] save sensor data as json file --- android/app/build.gradle | 9 ++- android/app/src/main/AndroidManifest.xml | 1 + .../light_sesnor_capture/MainActivity.java | 50 ++++++++++++++-- .../gradle/wrapper/gradle-wrapper.properties | 2 +- android/settings.gradle | 4 +- lib/light_sensor_event.dart | 11 ++++ lib/main.dart | 57 ++++++++++++------- pubspec.lock | 40 +++++++++++++ pubspec.yaml | 1 + 9 files changed, 144 insertions(+), 31 deletions(-) create mode 100644 lib/light_sensor_event.dart diff --git a/android/app/build.gradle b/android/app/build.gradle index 358a900..9601de7 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -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 { diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 182512d..2014f24 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,4 +1,5 @@ + toJson() => { + 'lux': lux, + 'timestamp': timestamp, + }; +} diff --git a/lib/main.dart b/lib/main.dart index e100f84..84859b7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,10 @@ +import 'dart:convert'; +import 'dart:io'; + import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:light_sesnor_capture/light_sensor_event.dart'; +import 'package:intl/intl.dart'; void main() { runApp(const MyApp()); @@ -56,18 +61,10 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { - int _counter = 0; - - void _incrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; - }); - } + int mState = 0; + static const MethodChannel methodChannel = + MethodChannel('tw.moonjuice.light_sensor.method'); + List mLightSensorEvents = []; @override Widget build(BuildContext context) { @@ -107,12 +104,15 @@ class _MyHomePageState extends State { mainAxisAlignment: MainAxisAlignment.center, children: [ Spacer(), - StreamBuilder( + StreamBuilder( stream: streamSensorEventFromNative(), builder: (context, snapshot) { if (snapshot.hasData) { + if (mState == 1) { + mLightSensorEvents.add(snapshot.data); + } return Text( - 'Sensor Data: ${snapshot.data}', + 'Sensor Data: ${jsonEncode(snapshot.data)}', style: Theme.of(context).textTheme.headlineLarge, ); } else { @@ -125,10 +125,13 @@ class _MyHomePageState extends State { children: [ Spacer(), ElevatedButton( - onPressed: onPressed, child: Text('Start Record')), + onPressed: (mState == 0) ? onPressed : null, + child: Text('Start Record'), + ), Spacer(), ElevatedButton( - onPressed: onPressed, child: Text('Stop Record')), + onPressed: (mState == 1) ? onPressed : null, + child: Text('Stop Record')), Spacer(), ], ), @@ -139,12 +142,28 @@ class _MyHomePageState extends State { ); } - void onPressed() {} + Future onPressed() async { + setState(() { + mState = ((mState == 0) ? 1 : 0); + }); + if (mState == 0) { + final String downloadsPath = + await methodChannel.invokeMethod('getExternalDownloadsPath'); + DateTime now = DateTime.now(); + String formattedDate = DateFormat('yyyy-MM-dd_HH-mm-ss').format(now); + File file = File('$downloadsPath/$formattedDate.txt'); + var sink = file.openWrite(); + sink.write(jsonEncode(mLightSensorEvents)); + await sink.flush(); + await sink.close(); + mLightSensorEvents.clear(); + } + } - Stream streamSensorEventFromNative() { + Stream streamSensorEventFromNative() { const eventChannel = EventChannel('tw.moonjuice.light_sensor.stream'); return eventChannel .receiveBroadcastStream() - .map((event) => event.toString()); + .map((event) => LightSensorEvent(event['lux'], event['timestamp'])); } } diff --git a/pubspec.lock b/pubspec.lock index cbb6965..1b52791 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -75,6 +75,30 @@ packages: description: flutter source: sdk version: "0.0.0" + http: + dependency: transitive + description: + name: http + sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010 + url: "https://pub.dev" + source: hosted + version: "1.2.2" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" + url: "https://pub.dev" + source: hosted + version: "4.0.2" + intl: + dependency: "direct main" + description: + name: intl + sha256: "00f33b908655e606b86d2ade4710a231b802eec6f11e87e4ea3783fd72077a50" + url: "https://pub.dev" + source: hosted + version: "0.20.1" leak_tracker: dependency: transitive description: @@ -192,6 +216,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.2" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006 + url: "https://pub.dev" + source: hosted + version: "1.4.0" vector_math: dependency: transitive description: @@ -208,6 +240,14 @@ packages: url: "https://pub.dev" source: hosted version: "14.2.5" + web: + dependency: transitive + description: + name: web + sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb + url: "https://pub.dev" + source: hosted + version: "1.1.0" sdks: dart: ">=3.5.4 <4.0.0" flutter: ">=3.18.0-18.0.pre.54" diff --git a/pubspec.yaml b/pubspec.yaml index 5504400..87cdcfd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,6 +35,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 + intl: ^0.20.1 dev_dependencies: flutter_test: