aidlab_sdk 1.7.1 aidlab_sdk: ^1.7.1 copied to clipboard
Aidlab Flutter SDK. For more information please visit https://www.aidlab.com/developer
import 'package:aidlab_sdk/aidlab_sdk.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'line_chart.dart';
AidlabManager? aidlabManager;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Aidlab Flutter SDK Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const DeviceListScreen(),
);
}
}
class DeviceListScreen extends StatefulWidget {
const DeviceListScreen({super.key});
@override
_DeviceListScreenState createState() => _DeviceListScreenState();
}
class _DeviceListScreenState extends State<DeviceListScreen>
implements AidlabManagerDelegate {
final List<Device> _devices = [];
bool _isScanning = false;
@override
void initState() {
super.initState();
Permission.bluetooth.request();
Permission.locationWhenInUse.request();
aidlabManager = AidlabManager(this);
}
@override
void dispose() {
aidlabManager?.dispose();
aidlabManager = null;
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Devices'),
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _devices.length,
itemBuilder: (context, index) {
final device = _devices[index];
return ListTile(
title: Text(device.address),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
DeviceDetailScreen(device: device),
),
);
},
);
},
),
),
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: _isScanning
? ElevatedButton(
onPressed: () => stopScan(),
style:
ElevatedButton.styleFrom(backgroundColor: Colors.red),
child: const Text('Stop scan'),
)
: ElevatedButton(
onPressed: () => startScan(),
child: const Text('Start scan'),
),
),
],
),
);
}
void startScan() {
aidlabManager?.scan(ScanMode.aggressive);
setState(() {
_isScanning = true;
});
}
void stopScan() {
aidlabManager?.stopScan();
setState(() {
_isScanning = false;
_devices.clear();
});
}
@override
void didDiscover(Device device, int rssi) {
setState(() {
if (!_devices.any((d) => d.address == device.address)) {
_devices.add(device);
} else {
_devices.removeWhere((d) => d.address == device.address);
_devices.add(device);
}
});
}
@override
void onBluetoothStarted() {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text("Bluetooth started")));
}
@override
void onDeviceScanStarted() {}
@override
void onDeviceScanStopped() {}
@override
void onScanFailed(int errorCode) {
/// Show toast
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Scan failed: $errorCode")));
}
}
class DeviceDetailScreen extends StatefulWidget {
final Device device;
const DeviceDetailScreen({super.key, required this.device});
@override
_DeviceDetailScreenState createState() => _DeviceDetailScreenState();
}
class _DeviceDetailScreenState extends State<DeviceDetailScreen>
implements DeviceDelegate {
/// Device information
String _firmwareRevision = "Unknown";
String _hardwareRevision = "Unknown";
String _serialNumber = "Unknown";
String _heartRate = "Unknown";
String _skinTemperature = "Unknown";
String _respirationRate = "Unknown";
String _rr = "Unknown";
String _activity = "Unknown";
int _steps = 0;
String _soundVolume = "Unknown";
String _batteryLevel = "Unknown";
String _signalQuality = "Unknown";
String _bodyPosition = "Unknown";
String _wearState = "Unknown";
String _syncState = "Unknown";
String _exercise = "Unknown";
final List<double> _ecg = [];
final List<double> _respiration = [];
/// Connect to device when the screen is created
@override
void initState() {
super.initState();
widget.device.connect(this);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Device'),
),
body: SingleChildScrollView(
child: Column(
children: [
ListTile(
title: const Text('Device Address'),
trailing: Text(widget.device.address),
),
ListTile(
title: const Text('Device Name'),
trailing: Text(widget.device.name ?? "Unknown"),
),
ListTile(
title: const Text('Firmware revision'),
trailing: Text(_firmwareRevision),
),
ListTile(
title: const Text('Hardware revision'),
trailing: Text(_hardwareRevision),
),
ListTile(
title: const Text('Serial number'),
trailing: Text(_serialNumber),
),
ListTile(
title: const Text('Battery Level'),
trailing: Text(_batteryLevel),
),
ListTile(
title: const Text('Signal Quality'),
trailing: Text(_signalQuality),
),
ListTile(
title: const Text('Heart Rate'),
trailing: Text(_heartRate),
),
ListTile(
title: const Text('Skin Temperature'),
trailing: Text(_skinTemperature),
),
ListTile(
title: const Text('Respiration Rate'),
trailing: Text(_respirationRate),
),
ListTile(
title: const Text('RR'),
trailing: Text(_rr),
),
ListTile(
title: const Text('Activity'),
trailing: Text(_activity),
),
ListTile(
title: const Text('Steps'),
trailing: Text(_steps.toString()),
),
ListTile(
title: const Text('Sound Volume'),
trailing: Text(_soundVolume),
),
ListTile(
title: const Text('Body Position'),
trailing: Text(_bodyPosition),
),
ListTile(
title: const Text('Wear State'),
trailing: Text(_wearState),
),
ListTile(
title: const Text('Sync State'),
trailing: Text(_syncState),
),
ListTile(
title: const Text('Exercise'),
trailing: Text(_exercise),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: MediaQuery.of(context).size.width,
height: 100,
child: LineChart(_ecg)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: MediaQuery.of(context).size.width,
height: 100,
child: LineChart(_respiration)),
),
ListTile(
title: const Text('Disconnect'),
onTap: () => disconnect(),
tileColor: Colors.redAccent,
),
],
),
),
);
}
void disconnect() {
widget.device.disconnect();
}
@override
void didConnect(Device device) {
/// Update device information
setState(() {
_firmwareRevision = device.firmwareRevision ?? "Unknown";
_hardwareRevision = device.hardwareRevision ?? "Unknown";
_serialNumber = device.serialNumber ?? "Unknown";
});
List<DataType> dataTypes = [
DataType.ECG,
DataType.RESPIRATION,
DataType.SKIN_TEMPERATURE,
DataType.ACTIVITY,
DataType.STEPS,
DataType.HEART_RATE,
DataType.RR,
// DataType.SOUND_VOLUME,
DataType.RESPIRATION_RATE,
DataType.BODY_POSITION,
DataType.PRESSURE,
];
device.collect(dataTypes, []);
}
@override
void didDetectUserEvent(Device device, int timestamp) {}
@override
void didDisconnect(Device? device, DisconnectReason reason) {
Navigator.pop(context);
}
@override
void didDetectExercise(Device device, Exercise exercise) {
setState(() {
_exercise = exercise.toString().split('.').last;
});
}
@override
void didReceiveAccelerometer(
Device device, int timestamp, double ax, double ay, double az) {}
@override
void didReceiveActivity(Device device, int timestamp, ActivityType activity) {
setState(() {
_activity = activity.toString().split('.').last;
});
}
@override
void didReceiveBatteryLevel(Device device, int stateOfCharge) {
setState(() {
_batteryLevel = "$stateOfCharge%";
});
}
@override
void didReceiveBodyPosition(
Device device, int timestamp, BodyPosition bodyPosition) {
setState(() {
_bodyPosition = bodyPosition.toString().split('.').last;
});
}
@override
void didReceiveCommand(Device device) {}
@override
void didReceiveECG(Device device, int timestamp, double value) {
setState(() {
_ecg.add(value);
while (_ecg.length > 500) {
_ecg.removeAt(0);
}
});
}
@override
void didReceiveError(String error) {
/// Show toast
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error)));
}
@override
void didReceiveGyroscope(
Device device, int timestamp, double qx, double qy, double qz) {}
@override
void didReceiveHeartRate(Device device, int timestamp, int heartRate) {
setState(() {
_heartRate = "$heartRate";
});
}
@override
void didReceiveMagnetometer(
Device device, int timestamp, double mx, double my, double mz) {}
@override
void didReceiveMessage(Device device, String process, String message) {}
@override
void didReceiveOrientation(
Device device, int timestamp, double roll, double pitch, double yaw) {}
@override
void didReceiveQuaternion(Device device, int timestamp, double qw, double qx,
double qy, double qz) {}
@override
void didReceiveRespiration(Device device, int timestamp, double value) {
setState(() {
_respiration.add(value);
while (_respiration.length > 500) {
_respiration.removeAt(0);
}
});
}
@override
void didReceiveRespirationRate(Device device, int timestamp, int value) {
setState(() {
_respirationRate = "$value";
});
}
@override
void didReceiveRr(Device device, int timestamp, int rr) {
setState(() {
_rr = "$rr";
});
}
@override
void didReceiveSignalQuality(Device device, int timestamp, int value) {
setState(() {
_signalQuality = "$value";
});
}
@override
void didReceiveSkinTemperature(Device device, int timestamp, double value) {
setState(() {
_skinTemperature = "${value.toStringAsFixed(1)} °C";
});
}
@override
void didReceiveSoundFeatures(
Device device, int timestamp, List<double?> values) {}
@override
void didReceiveSoundVolume(Device device, int timestamp, int value) {
setState(() {
_soundVolume = "$value dB";
});
}
@override
void didReceiveSteps(Device device, int timestamp, int steps) {
setState(() {
_steps += steps;
});
}
@override
void didReceivePressure(Device device, int timestamp, int value) {}
@override
void didReceivePressureWearState(Device device, WearState wearState) {}
@override
void didReceiveUnsynchronizedSize(
Device device, int unsynchronizedSize, double syncBytesPerSecond) {}
@override
void syncStateDidChange(Device device, SyncState state) {
setState(() {
_syncState = state.toString().split('.').last;
});
}
@override
void wearStateDidChange(Device device, WearState wearState) {
setState(() {
_wearState = wearState.toString().split('.').last;
});
}
@override
void didReceivePastAccelerometer(
Device device, int timestamp, double ax, double ay, double az) {}
@override
void didReceivePastActivity(
Device device, int timestamp, ActivityType activity) {}
@override
void didReceivePastBodyPosition(
Device device, int timestamp, BodyPosition bodyPosition) {}
@override
void didReceivePastECG(Device device, int timestamp, double value) {}
@override
void didReceivePastGyroscope(
Device device, int timestamp, double qx, double qy, double qz) {}
@override
void didReceivePastHeartRate(Device device, int timestamp, int heartRate) {}
@override
void didReceivePastMagnetometer(
Device device, int timestamp, double mx, double my, double mz) {}
@override
void didReceivePastOrientation(
Device device, int timestamp, double roll, double pitch, double yaw) {}
@override
void didReceivePastPressure(Device device, int timestamp, int values) {}
@override
void didReceivePastQuaternion(Device device, int timestamp, double qw,
double qx, double qy, double qz) {}
@override
void didReceivePastRespiration(Device device, int timestamp, double value) {}
@override
void didReceivePastRespirationRate(Device device, int timestamp, int value) {}
@override
void didReceivePastRr(Device device, int timestamp, int rr) {}
@override
void didReceivePastSignalQuality(Device device, int timestamp, int value) {}
@override
void didReceivePastSkinTemperature(
Device device, int timestamp, double value) {}
@override
void didReceivePastSoundFeatures(
Device device, int timestamp, List<double?> values) {}
@override
void didReceivePastSoundVolume(
Device device, int timestamp, int soundVolume) {}
@override
void didReceivePastSteps(Device device, int timestamp, int value) {}
@override
void didReceivePastUserEvent(Device device, int timestamp) {}
}