aidlab_sdk 0.0.21 aidlab_sdk: ^0.0.21 copied to clipboard
Official Aidlab SDK for Flutter, for more information please visit https://www.aidlab.com/developer
import 'package:aidlab_sdk/aidlab_sdk.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:permission_handler/permission_handler.dart';
import 'line_chart.dart';
import 'dart:io' show Platform;
void main() async {
await AidlabSdk.instance.init();
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp>
implements
AidlabSdkDelegate,
AidlabDelegate,
AidlabSynchronizationDelegate,
InternalAidlabDelegate {
String _aidlabState = "Disconnected";
String _aidlabInfo = "";
String _aidlabWearState = "";
String _heartRate = "";
String _batteryLevel = "";
String _skinTemperature = "";
String _unsynchronizedSize = "";
String _syncBytesPerSecond = "";
String _syncState = "";
List<double> _ecg = [];
List<String> _aidlabs = [];
int _skipEcgCount = 1;
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 (Platform.isAndroid) {
_scanForAidlabAndroid();
} else {
_scanForAidlabiOS();
}
}
void _scanForAidlabAndroid() async {
if (await Permission.location.request().isGranted) {
_setAidlabState("Scanning");
AidlabSdk.instance.scanForDevices(ScanMode.aggressive);
} else {
_setAidlabState("Bluetooth not granted");
}
}
void _scanForAidlabiOS() {
_setAidlabState("Scanning");
AidlabSdk.instance.scanForDevices(ScanMode.aggressive);
}
@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: [
Column(
children: createAidlabs(),
),
Text("Aidlab state: $_aidlabState"),
Text("Aidlab info - $_aidlabInfo"),
Text("Sync state: $_syncState"),
Text("Unsynchronized size: $_unsynchronizedSize"),
Text("Sync bytes per seconds: $_syncBytesPerSecond"),
Text("Wear state: $_aidlabWearState"),
Text("Heart rate: $_heartRate"),
Text("Battery level: $_batteryLevel"),
Text("Skin temperature: $_skinTemperature"),
TextButton(
onPressed: () {
scanForAidlab();
},
child: Text('Start scan')),
Wrap(
children: [
TextButton(
onPressed: () {
currentAidlab?.send("sync count");
},
child: Text('sync count'),
),
TextButton(
onPressed: () {
currentAidlab?.send("sync clear");
},
child: Text('sync clear'),
),
TextButton(
onPressed: () {
currentAidlab?.startSynchronization();
},
child: Text('sync start'),
),
TextButton(
onPressed: () {
currentAidlab?.stopSynchronization();
},
child: Text('sync stop'),
),
TextButton(
onPressed: () {
currentAidlab?.send('memory -info');
},
child: Text('memory -info'),
),
TextButton(
onPressed: () {
currentAidlab?.send('set alarm');
},
child: Text('set alarm'),
),
TextButton(
onPressed: () {
currentAidlab?.disconnect();
},
child: Text('disconnect'),
),
],
),
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.SoundVolume,
Signal.SoundFeatures,
Signal.Pressure,
];
AidlabSdk.instance.connect(aidlab, signals, this);
}
// --------- Aidlab SDK delegate -------------
@override
void onAidlabDetected(String address, int rssi) {
//
// if(address == currentAidlab?.address) {
// _onAidlabPressed(address);
// }
// return;
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"
];
@override
void didConnectAidlab(Aidlab aidlab) {
print(aidlab.address);
currentAidlab = aidlab;
aidlab.aidlabSynchronizationDelegate = this;
aidlab.internalAidlab.internalDelegate = this;
setState(() {
_aidlabState = "Connected";
_aidlabInfo = "serial number: ${aidlab.serialNumber}\n"
"hardware: ${aidlab.hardwareRevision}\n"
"firmware: ${aidlab.firmwareRevision}";
});
}
@override
void didDetectExercise(Aidlab aidlab, Exercise exercise) {}
@override
void didDisconnectAidlab(Aidlab? aidlab, DisconnectReason reason) {
setState(() {
_aidlabState = "Disconnected";
_aidlabInfo = "Disconnect reason: ${reason.toString().split(".")[1]}";
});
scanForAidlab();
}
@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) {
setState(() {
_batteryLevel = "$stateOfCharge";
});
}
@override
void didReceiveCommand(Aidlab aidlab) {
// _sendCommands(aidlab);
}
@override
void didReceiveECG(Aidlab aidlab, int timestamp, List<double> values) {
_skipEcgCount = (_skipEcgCount + 1) % 10;
if (_skipEcgCount != 0) {
return;
}
setState(() {
_ecg.add(values[0]);
while (_ecg.length > 100) {
_ecg.removeAt(0);
}
});
}
@override
void didReceiveError(String error) {}
@override
void didReceiveGyroscope(
Aidlab aidlab, int timestamp, double qx, double qy, double qz) {}
@override
void didReceiveHeartRate(Aidlab aidlab, int timestamp, int heartRate) {
setState(() {
_heartRate = "$heartRate";
});
}
@override
void didReceiveMagnetometer(
Aidlab aidlab, int timestamp, double mx, double my, double mz) {}
@override
void didReceiveMessage(Aidlab aidlab, String process, String message) {
print("Received message from '$process', message: $message");
}
@override
void didDetectUserEvent(Aidlab aidlab, int timestamp) {}
@override
void didReceiveOrientation(
Aidlab aidlab, int timestamp, double roll, double pitch, double yaw) {}
@override
void didReceiveBodyPosition(
Aidlab aidlab, int timestamp, BodyPosition bodyPosition) {}
@override
void didReceiveRr(Aidlab aidlab, int timestamp, int rr) {}
@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) {
setState(() {
_skinTemperature = "${value.toInt()}";
});
}
@override
void didReceiveSoundVolume(Aidlab aidlab, int timestamp, int value) {}
@override
void didReceiveSoundFeatures(Aidlab aidlab, List<double> values) {}
@override
void didReceiveSteps(Aidlab aidlab, int timestamp, int steps) {}
@override
void wearStateDidChange(Aidlab aidlab, WearState wearState) {
setState(() {
_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 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 didReceivePastAccelerometer(
Aidlab aidlab, int timestamp, double ax, double ay, double az) {}
@override
void didReceivePastBodyPosition(
Aidlab aidlab, int timestamp, BodyPosition bodyPosition) {}
@override
void didReceivePastGyroscope(
Aidlab aidlab, int timestamp, double qx, double qy, double qz) {}
@override
void didReceivePastHeartRate(Aidlab aidlab, int timestamp, int heartRate) {}
@override
void didReceivePastRr(Aidlab aidlab, int timestamp, int rr) {}
@override
void didReceivePastMagnetometer(
Aidlab aidlab, int timestamp, double mx, double my, double mz) {}
@override
void didReceivePastOrientation(
Aidlab aidlab, int timestamp, double roll, double pitch, double yaw) {}
@override
void didReceivePastPressure(Aidlab aidlab, int timestamp, List<int> values) {}
@override
void didReceivePastQuaternion(Aidlab aidlab, int timestamp, double qw,
double qx, double qy, double qz) {}
@override
void didReceivePastSoundVolume(
Aidlab aidlab, int timestamp, int soundVolume) {}
@override
void didDetectPastUserEvent(Aidlab aidlab, int timestamp) {}
@override
void didReceivePastSoundFeatures(Aidlab aidlab, List<double> values) {}
@override
void didReceiveUnsynchronizedSize(
Aidlab aidlab, int unsynchronizedSize, double syncBytesPerSecond) {
setState(() {
_unsynchronizedSize = "$unsynchronizedSize";
_syncBytesPerSecond = "$syncBytesPerSecond";
});
}
@override
void syncStateDidChange(Aidlab aidlab, SyncState state) {
setState(() {
_syncState = "${state.toString()}";
});
}
@override
void didReceiveFirmwareInformation(
bool updateAvailable, String updatableVersion) {}
@override
void didReceivePressure(Aidlab aidlab, int timestamp, List<int> values) {}
@override
void pressureWearStateDidChange(Aidlab aidlab, WearState state) {}
@override
void didReceivePastSignalQuality(Aidlab aidlab, int timestamp, int value) {}
@override
void didReceiveSignalQuality(Aidlab aidlab, int timestamp, int value) {}
}