A Core Plugin for Aware Framework on Flutter
- Make a template app using flutter command
$ flutter create --org com.awareframework.accelerometer --template=plugin --platforms=android,ios -i swift -a kotlin awareframework_accelerometer- Add the awareframework_core into your pubspec.yaml
dependencies:
awareframework_core:You can get more information about the package installation via the following link.
- Implement your sensor using the core-library
/// The Accelerometer Sensor classclassAccelerometerSensorextendsAwareSensor {
/// Accelerometer Method ChannelstaticconstMethodChannel _accelerometerMethod =constMethodChannel('awareframework_accelerometer/method');
/// Accelerometer Event ChannelstaticconstEventChannel _accelerometerStream =constEventChannel('awareframework_accelerometer/event');
/// Init Accelerometer Sensor with AccelerometerSensorConfigAccelerometerSensor():this.init(null);
AccelerometerSensor.init(AccelerometerSensorConfig config) :super.init(config){
super.setMethodChannel(_accelerometerMethod);
}
Stream<Map<String,dynamic>> get onDataChanged {
returnsuper.getBroadcastStream( _accelerometerStream, "on_data_changed").map((dynamic event) =>Map<String,dynamic>.from(event));
}
@overridevoidcancelAllEventChannels() {
super.cancelBroadcastStream("on_data_changed");
}
}
////// The Sensor Configuration Parameter class///classAccelerometerSensorConfigextendsAwareSensorConfig {
int frequency =5;
double period =1.0;
double threshold =0.0;
AccelerometerSensorConfig();
@overrideMap<String, dynamic> toMap() {
var map =super.toMap();
map['frequency'] = frequency;
map['period'] = period;
map['threshold'] = threshold;
return map;
}
}
- Add following code into ios/awareframework_accelerometer.podspec
# update author information and url s.dependency 'awareframework_core' s.ios.deployment_target = '10.0' # add other dependencyRun
pod installat example/iosOpen iOS project (example/ios/Runner.xcworkspace) and change a deplyment target to 10.0
import Flutter
import UIKit
import com_awareframework_ios_sensor_accelerometer
import com_awareframework_ios_sensor_core
import awareframework_core
publicclassSwiftAwareframeworkAccelerometerPlugin:AwareFlutterPluginCore,FlutterPlugin,AwareFlutterPluginSensorInitializationHandler,AccelerometerObserver{varaccelerometerSensor:AccelerometerSensor?publicoverrideinit(){
super.init()
super.initializationCallEventHandler =self}publicfunc initializeSensor(_ call:FlutterMethodCall, result:@escapingFlutterResult)->AwareSensor?{ifself.sensor ==nil{iflet config = call.arguments as?Dictionary<String,Any>{self.accelerometerSensor =AccelerometerSensor.init(AccelerometerSensor.Config(config))}else{self.accelerometerSensor =AccelerometerSensor.init(AccelerometerSensor.Config())}self.accelerometerSensor?.CONFIG.sensorObserver =selfreturnself.accelerometerSensor
}else{returnnil}}publicstaticfunc register(with registrar:FlutterPluginRegistrar){letinstance=SwiftAwareframeworkAccelerometerPlugin()
// add own channel
super.setMethodChannel(with: registrar,
instance: instance,
channelName:"awareframework_accelerometer/method");
super.setEventChannels(with: registrar,
instance: instance,
channelNames:["awareframework_accelerometer/event"]);
}publicfunc onDataChanged(data:AccelerometerData){forhandlerinself.streamHandlers {if handler.eventName =="on_data_changed"{
handler.eventSink(data.toDictionary())}}}}import'package:awareframework_core/awareframework_core.dart';
class_MyAppStateextendsState<MyApp> {
AccelerometerSensor sensor;
AccelerometerSensorConfig config;
@overridevoidinitState() {
super.initState();
config =AccelerometerSensorConfig()
..debug =true
..label ="label";
sensor =newSampleSensor(config);
}
@overrideWidgetbuild(BuildContext context) {
returnnewMaterialApp(
home:newScaffold(
appBar:newAppBar(
title:constText('Plugin Example App'),
),
body:newAccelerometerCard(sensor: sensor)
),
);
}
}
author: AWARE Mobile Context Instrumentation Middleware/Framework <yuukin@iis.u-tokyo.ac.jp>homepage: http://www.awareframework.com$ flutter packages pub publish --dry-run
$ flutter packages pub publish