Activity Recognition
Activity recognition plugin for Android and iOS. Only working while App is running (= not terminated by the user or OS).
Add the following entries inside the <manifest> tag:
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Next, add the plugin's service inside the <application> tag:
<receiver android:name="dk.cachet.activity_recognition_flutter.ActivityRecognizedBroadcastReceiver"/>
<service
android:name="dk.cachet.activity_recognition_flutter.ActivityRecognizedService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true"/>
<service android:name="dk.cachet.activity_recognition_flutter.ForegroundService" />
If you update from Android SDK <=28 to >=29 remember to run flutter clean. See e.g. this post on stack overflow.
This package uses the Android Embedding API v2. In order to use this in pre-Flutter 1.12 projects, you need to follow this guide.
An iOS app linked on or after iOS 10.0 must include usage description keys in its Info.plist file for the types of data it needs. Failure to include these keys will cause the app to crash. To access motion and fitness data specifically, it must include NSMotionUsageDescription, like this:
<key>NSMotionUsageDescription</key>
<string>Detects human activity</string>
To use this plugin, you need to also use the permission_handler plugin, or some other way of requesting permission. See the example app.
NOTE: You should NOT use the permission handler plugin for requesting activity recognition on iOS, since it is not needed and will make your iOS app crash.
Each detected activity will have an activity type, which is one of the following:
As well as a confidence expressed in percentages (i.e. a value from 0-100).
Run this command:
With Flutter:
$ flutter pub add activity_recognition_flutter
This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):
dependencies:
activity_recognition_flutter: ^4.0.4
Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:activity_recognition_flutter/activity_recognition_flutter.dart';
import 'dart:io';
import 'package:activity_recognition_flutter/activity_recognition_flutter.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
late Stream<ActivityEvent> activityStream;
ActivityEvent latestActivity = ActivityEvent.empty();
List<ActivityEvent> _events = [];
ActivityRecognition activityRecognition = ActivityRecognition.instance;
@override
void initState() {
super.initState();
_init();
}
void _init() async {
/// Android requires explicitly asking permission
if (Platform.isAndroid) {
if (await Permission.activityRecognition.request().isGranted) {
_startTracking();
}
}
/// iOS does not
else {
_startTracking();
}
}
void _startTracking() {
activityStream =
activityRecognition.startStream(runForegroundService: true);
activityStream.listen(onData);
}
void onData(ActivityEvent activityEvent) {
print(activityEvent.toString());
setState(() {
_events.add(activityEvent);
latestActivity = activityEvent;
});
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Activity Recognition Demo'),
),
body: new Center(
child: new ListView.builder(
itemCount: _events.length,
reverse: true,
itemBuilder: (BuildContext context, int idx) {
final entry = _events[idx];
return ListTile(
leading:
Text(entry.timeStamp.toString().substring(0, 19)),
trailing: Text(entry.type.toString().split('.').last));
})),
),
);
}
}
Author: cachet.dk
Official Website: https://pub.dev/packages/activity_recognition_flutter