aidlab_sdk 0.0.1 aidlab_sdk: ^0.0.1 copied to clipboard
Aidlab sdk for Flutter
import 'package:aidlab_sdk/aidlab_sdk.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'line_chart.dart';
void startService() {
MethodChannel("com.aidlab.flutter_sdk_example/service")
.invokeMethod("startService");
}
void stopService() {
MethodChannel("com.aidlab.flutter_sdk_example/service")
.invokeMethod("stopService");
}
void main() {
runApp(MaterialApp(
home: MyApp()
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> implements AidlabSdkDelegate, AidlabDelegate, AidlabSynchronizationDelegate {
String _aidlabState = "Disconnected";
String _aidlabInfo = "";
String _aidlabWearState = "";
String _heartRate = "";
String _batteryLevel = "";
String _skinTemperature = "";
String _unsynchronizedSize = "";
String _syncState = "";
List<double> _ecg = [];
List<String> _aidlabs = List<String>();
Aidlab currentAidlab;
@override
void initState() {
super.initState();
AidlabSdk.instance.aidlabSdkDelegate = this;
}
@override
void dispose() {
super.dispose();
}
void _setAidlabState(String state) {
setState(() {
_aidlabState = state;
});
}
Future<void> scanForAidlab() async {
if (await Permission.location.request().isGranted) {
_setAidlabState("Scanning");
try {
AidlabSdk.instance.scanForDevices();
} on PlatformException {
_setAidlabState("Scan failed");
}
} else {
_setAidlabState("Bluetooth not granted");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextButton(
onPressed: startService,
child: Text("Start service")
),
TextButton(
onPressed: stopService,
child: Text("Stop service")
),
Column(
children: createAidlabs(),
),
Text("Aidlab state: $_aidlabState"),
Text("Aidlab info - $_aidlabInfo"),
Text("Sync state: $_syncState"),
Text("Unsynchronized size: $_unsynchronizedSize"),
Text("Wear state: $_aidlabWearState"),
Text("Heart rate: $_heartRate"),
Text("Battery level: $_batteryLevel"),
Text("Skin temperature: $_skinTemperature"),
TextButton(
onPressed: () {
scanForAidlab();
},
child: Text('Start scan')),
Expanded(
child: LineChart(_ecg),
),
],
),
)
);
}
List<Widget> createAidlabs() {
return _aidlabs
.map((aidlab) => TextButton(
style: TextButton.styleFrom(
padding: EdgeInsets.all(0),
tapTargetSize: MaterialTapTargetSize.shrinkWrap),
onPressed: () => _onAidlabPressed(aidlab),
child: Text(aidlab),
))
.toList();
}
void _onAidlabPressed(String aidlab) {
AidlabSdk.instance.stopDeviceScan();
setState(() {
_aidlabs.clear();
});
currentAidlab?.disconnect();
currentAidlab = null;
final signals = [
Signal.Ecg,
Signal.Respiration,
Signal.Temperature,
Signal.Motion,
Signal.Battery,
Signal.Activity,
Signal.Orientation,
Signal.Steps,
Signal.HeartRate,
Signal.CMD,
];
AidlabSdk.instance.connect(aidlab, signals, this);
}
// --------- Aidlab SDK delegate -------------
@override
void onAidlabDetected(String address, int rssi) {
setState(() {
if(!_aidlabs.contains(address)) {
_aidlabs.add(address);
}
});
}
@override
void onBluetoothStarted() { }
@override
void onDeviceScanStarted() { }
@override
void onDeviceScanStopped() { }
@override
void onScanFailed(int errorCode) { }
// --------- Aidlab delegate -------------
final commands = [
"sync enable ecg", "sync enable respiration",
"sync enable activity", "sync enable steps",
"sync enable heartRate", "sync enable temperature"
];
void _sendCommands(Aidlab aidlab) {
if(commands.isNotEmpty) {
aidlab.send(commands.first);
commands.removeAt(0);
}
else {
aidlab.startSynchronization();
}
}
@override
void didConnectAidlab(Aidlab aidlab) {
currentAidlab = aidlab;
aidlab.aidlabSynchronizationDelegate = this;
setState(() {
_aidlabState = "Connected";
_aidlabInfo = "serial number: ${aidlab.serialNumber}\n"
"hardware: ${aidlab.hardwareRevision}\n"
"firmware: ${aidlab.firmwareRevision}";
});
_sendCommands(aidlab);
}
@override
void didDetectExercise(Aidlab aidlab, Exercise exercise) {
}
@override
void didDisconnectAidlab(Aidlab aidlab) {
setState(() {
_aidlabState = "Disconnected";
_aidlabInfo = "-";
});
}
@override
void didReceiveAccelerometer(Aidlab aidlab, int timestamp, double ax, double ay, double az) { }
@override
void didReceiveActivity(Aidlab aidlab, int timestamp, ActivityType activity) { }
@override
void didReceiveBatteryLevel(Aidlab aidlab, int stateOfCharge) {
_batteryLevel = "$stateOfCharge";
}
@override
void didReceiveCommand(Aidlab aidlab) {
_sendCommands(aidlab);
}
@override
void didReceiveECG(Aidlab aidlab, int timestamp, List<double> values) {
setState(() {
_ecg.add(values[0]);
while(_ecg.length > 100) {
_ecg.removeAt(0);
}
});
}
@override
void didReceiveError(String error) { }
@override
void didReceiveFirmwareInformation(bool updateAvaible, String updatableVersion) { }
@override
void didReceiveGyroscope(Aidlab aidlab, int timestamp, double qx, double qy, double qz) { }
@override
void didReceiveHeartRate(Aidlab aidlab, int timestamp, List<int> hrv, int heartRate) {
_heartRate = "$heartRate";
}
@override
void didReceiveMagnetometer(Aidlab aidlab, int timestamp, double mx, double my, double mz) { }
@override
void didReceiveMessage(Aidlab aidlab, String process, String message) { }
@override
void didReceiveOrientation(Aidlab aidlab, int timestamp,
double roll, double pitch, double yaw, BodyPosition bodyPosition) { }
@override
void didReceivePressure(Aidlab aidlab, int timestamp, List<int> values) { }
@override
void didReceiveQuaternion(Aidlab aidlab, int timestamp, double qw, double qx, double qy, double qz) { }
@override
void didReceiveRespiration(Aidlab aidlab, int timestamp, List<double> values) { }
@override
void didReceiveRespirationRate(Aidlab aidlab, int timestamp, int value) { }
@override
void didReceiveSkinTemperature(Aidlab aidlab, int timestamp, double value) {
_skinTemperature = "${value.toInt()}";
}
@override
void didReceiveSoundVolume(Aidlab aidlab, int timestamp, int value) { }
@override
void didReceiveSteps(Aidlab aidlab, int timestamp, int steps) { }
@override
void pressureWearStateDidChange(Aidlab aidlab, WearState state) { }
@override
void wearStateDidChange(Aidlab aidlab, WearState wearState) {
_aidlabWearState = "${wearState.toString()}";
}
// --------- Aidlab synchronization delegate -------------
@override
void didReceivePastActivity(Aidlab aidlab, int timestamp, ActivityType activity) { }
@override
void didReceivePastECG(Aidlab aidlab, int timestamp, List<double> values) { }
@override
void didReceivePastHeartRate(Aidlab aidlab, int timestamp, List<int> hrv, int heartRate) { }
@override
void didReceivePastRespiration(Aidlab aidlab, int timestamp, List<double> values) { }
@override
void didReceivePastRespirationRate(Aidlab aidlab, int timestamp, int value) { }
@override
void didReceivePastSkinTemperature(Aidlab aidlab, int timestamp, double value) { }
@override
void didReceivePastSteps(Aidlab aidlab, int timestamp, int value) { }
@override
void didReceiveUnsynchronizedSize(Aidlab aidlab, int unsynchronizedSize) {
setState(() {
_unsynchronizedSize = "$unsynchronizedSize";
});
}
@override
void syncStateDidChange(Aidlab aidlab, SyncState state) {
setState(() {
_syncState = "${state.toString()}";
});
}
}