onConnect method
Callback on connect. Returns the DeviceStatus
of the device.
Is to be overridden in sub-classes and implement device-specific connection.
Implementation
@override
Future<DeviceStatus> onConnect() async {
try {
// listen for connection events
manager?.connectionEvents.listen((event) async {
debug('$runtimeType - $event');
switch (event.type) {
case ConnectionType.connected:
status = DeviceStatus.connected;
await manager?.setSamplingRate(configuration?.samplingRate ?? 10);
// when connected, listen for battery events
_batterySubscription = manager!.eSenseEvents.listen((event) {
if (event is BatteryRead) {
_voltageLevel = event.voltage;
if (batteryLevel != null) {
_batteryEventController.add(batteryLevel!);
}
}
});
// set up a timer that asks for the voltage level
_batteryTimer = Timer.periodic(const Duration(minutes: 2), (_) {
if (status == DeviceStatus.connected) {
manager?.getBatteryVoltage();
}
});
break;
case ConnectionType.unknown:
status = DeviceStatus.unknown;
break;
case ConnectionType.device_found:
status = DeviceStatus.connecting;
break;
case ConnectionType.device_not_found:
case ConnectionType.disconnected:
status = DeviceStatus.disconnected;
_voltageLevel = null;
_batteryTimer?.cancel();
_batterySubscription?.cancel();
break;
}
});
// try to scan for eSense device and connect to it
manager?.connect();
} catch (error) {
warning(
'$runtimeType - Error connecting to eSense device id: $id - $error');
return DeviceStatus.error;
}
return DeviceStatus.connecting;
}