polar 3.6.0 polar: ^3.6.0 copied to clipboard
This is a Dart plugin wrapper for the Polar SDK on Android and iOS
import 'package:flutter/material.dart';
import 'package:polar/polar.dart';
void main() {
runApp(const MyApp());
}
/// Example app
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const identifier = '1C709B20';
final polar = Polar();
final logs = ['Service started'];
PolarExerciseEntry? exerciseEntry;
@override
void initState() {
super.initState();
// polar
// .searchForDevice()
// .listen((e) => log('Found device in scan: ${e.deviceId}'));
polar.heartRateStream.listen((e) => log('Heart rate: ${e.data.hr}'));
polar.batteryLevelStream.listen((e) => log('Battery: ${e.level}'));
polar.streamingFeaturesReadyStream.listen((e) {
debugPrint('streamingFeaturesReady: ${e.features}');
if (e.features.contains(DeviceStreamingFeature.ecg)) {
polar
.startEcgStreaming(e.identifier)
.listen((e) => log('ECG data: ${e.samples}'));
}
if (e.features.contains(DeviceStreamingFeature.acc)) {
polar
.startAccStreaming(e.identifier)
.listen((e) => log('ACC data: ${e.samples}'));
}
});
polar.deviceConnectingStream.listen((_) => log('Device connecting'));
polar.deviceConnectedStream.listen((_) => log('Device connected'));
polar.deviceDisconnectedStream.listen((_) => log('Device disconnected'));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Polar example app'),
actions: [
PopupMenuButton(
itemBuilder: (context) => RecordingAction.values
.map((e) => PopupMenuItem(value: e, child: Text(e.name)))
.toList(),
onSelected: handleRecordingAction,
child: const IconButton(
icon: Icon(Icons.fiber_manual_record),
disabledColor: Colors.white,
onPressed: null,
),
),
IconButton(
icon: const Icon(Icons.stop),
onPressed: () {
log('Disconnecting from device: $identifier');
polar.disconnectFromDevice(identifier);
},
),
IconButton(
icon: const Icon(Icons.play_arrow),
onPressed: () {
log('Connecting to device: $identifier');
polar.connectToDevice(identifier);
},
),
],
),
body: ListView(
padding: const EdgeInsets.all(10),
shrinkWrap: true,
children: logs.reversed.map(Text.new).toList(),
),
),
);
}
void log(String log) {
// ignore: avoid_print
print(log);
setState(() {
logs.add(log);
});
}
Future<void> handleRecordingAction(RecordingAction action) async {
switch (action) {
case RecordingAction.start:
log('Starting recording');
await polar.startRecording(
identifier,
exerciseId: 'test',
interval: RecordingInterval.interval_1s,
sampleType: SampleType.rr,
);
log('Started recording');
break;
case RecordingAction.stop:
log('Stopping recording');
await polar.stopRecording(identifier);
log('Stopped recording');
break;
case RecordingAction.status:
log('Getting recording status');
final status = await polar.requestRecordingStatus(identifier);
log('Recording status: $status');
break;
case RecordingAction.list:
log('Listing recordings');
final entries = await polar.listExercises(identifier);
log('Recordings: $entries');
exerciseEntry = entries.first;
break;
case RecordingAction.fetch:
log('Fetching recording');
if (exerciseEntry == null) {
log('Exercises not yet listed');
await handleRecordingAction(RecordingAction.list);
}
final entry = await polar.fetchExercise(identifier, exerciseEntry!);
log('Fetched recording: $entry');
break;
case RecordingAction.remove:
log('Removing recording');
if (exerciseEntry == null) {
log('No exercise to remove. Try calling list first.');
return;
}
await polar.removeExercise(identifier, exerciseEntry!);
log('Removed recording');
break;
}
}
}
enum RecordingAction {
start,
stop,
status,
list,
fetch,
remove,
}