summaryrefslogtreecommitdiff
path: root/lib/esense_input.dart
blob: 705bd5f9c21b82af10eef05f4e284fc90218e4ed (plain)
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
import 'dart:async';
import 'dart:io';

import 'package:esense_flutter/esense.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class ESenseInput {
  static final instance = ESenseInput._();

  ESenseManager eSenseManager = ESenseManager('unknown');
  ValueNotifier<String> deviceStatus = ValueNotifier('');

  String eSenseDeviceName = '';
  bool connected = false;
  bool sampling = false;

  ESenseInput._() {
    _listenToESense();
  }

  Future<void> _askForPermissions() async {
    if (!Platform.isAndroid && !Platform.isIOS) return;
    if (!(await Permission.bluetoothScan.request().isGranted &&
        await Permission.bluetoothConnect.request().isGranted)) {
      print(
          'WARNING - no permission to use Bluetooth granted. Cannot access eSense device.');
    }
    // for some strange reason, Android requires permission to location for Bluetooth to work.....?
    if (Platform.isAndroid) {
      if (!(await Permission.locationWhenInUse.request().isGranted)) {
        print(
            'WARNING - no permission to access location granted. Cannot access eSense device.');
      }
    }
  }

  StreamSubscription _listenToESense() {
    // if you want to get the connection events when connecting,
    // set up the listener BEFORE connecting...
    return eSenseManager.connectionEvents.listen((event) {
      print('CONNECTION event: $event');

      // when we're connected to the eSense device, we can start listening to events from it
      // if (event.type == ConnectionType.connected) _listenToESenseEvents();

      connected = false;
      switch (event.type) {
        case ConnectionType.connected:
          deviceStatus.value = 'connected';
          connected = true;
          break;
        case ConnectionType.unknown:
          deviceStatus.value = 'unknown';
          break;
        case ConnectionType.disconnected:
          deviceStatus.value = 'disconnected';
          sampling = false;
          break;
        case ConnectionType.device_found:
          deviceStatus.value = 'device_found';
          break;
        case ConnectionType.device_not_found:
          deviceStatus.value = 'device_not_found';
          break;
      }
    });
  }

  Future<void> connectToESense(String deviceName) async {
    if (!connected) {
      await _askForPermissions();
      print('Trying to connect to eSense device namend \'$deviceName\'');
      eSenseDeviceName = deviceName;
      eSenseManager.deviceName = deviceName;
      connected = await eSenseManager.connect();
      print('Trying to connect to eSense device namend \'${eSenseManager.deviceName}\'');

      deviceStatus.value = connected ? 'connecting...' : 'connection failed';
      print(deviceStatus.value);
    }
  }
}