/
main.dart
176 lines (161 loc) · 4.98 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import 'package:flutter/material.dart';
import 'package:polar/polar.dart';
import 'package:uuid/uuid.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.batteryLevel.listen((e) => log('Battery: ${e.level}'));
polar.deviceConnecting.listen((_) => log('Device connecting'));
polar.deviceConnected.listen((_) => log('Device connected'));
polar.deviceDisconnected.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);
streamWhenReady();
},
),
],
),
body: ListView(
padding: const EdgeInsets.all(10),
shrinkWrap: true,
children: logs.reversed.map(Text.new).toList(),
),
),
);
}
void streamWhenReady() async {
await polar.sdkFeatureReady.firstWhere(
(e) =>
e.identifier == identifier &&
e.feature == PolarSdkFeature.onlineStreaming,
);
final availabletypes =
await polar.getAvailableOnlineStreamDataTypes(identifier);
debugPrint('available types: $availabletypes');
if (availabletypes.contains(PolarDataType.hr)) {
polar
.startHrStreaming(identifier)
.listen((e) => log('Heart rate: ${e.samples.map((e) => e.hr)}'));
}
if (availabletypes.contains(PolarDataType.ecg)) {
polar
.startEcgStreaming(identifier)
.listen((e) => log('ECG data received'));
}
if (availabletypes.contains(PolarDataType.acc)) {
polar
.startAccStreaming(identifier)
.listen((e) => log('ACC data received'));
}
}
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: const Uuid().v4(),
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');
// H10 can only store one recording at a time
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,
}