cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
547 stars 664 forks source link

eSense - No Sensor Events #51

Closed ggogel closed 4 years ago

ggogel commented 4 years ago

Hello,

I'm currently trying to implement an App for eSense Earbuds with the help of this plugin. I can connect to the Earbuds and read ESenseEvents such as BatteryRead absolutely fine.

What I can not read are any SensorEvents.

I tried these implementations for reading the Stream:

A simple listener: ESenseManager.sensorEvents.listen((event) { print('SENSOR event: $event'); });

and with an await for method, that yields a stream for further usage: Stream<dynamic> getSensorStream() async* { if (ESenseManager.connected) { await for (final event in ESenseManager.sensorEvents) { print('SENSOR event: $event'); yield event; } } }

Now to the strange part of it: If I run the eSense demo app and press the play button to start the sampling, the events also show up in my app. If I click pause the events stop in both apps. If I close the demo app while sampling, the events are still coming in on my app.

What am I missing? What is the demo app doing to trigger the event stream?

bardram commented 4 years ago

Hi @ggogel - good to see you're trying to use the plugin.

It's hard to understand the big picture outside of your code snippets, but here is a few comments.

First - it is important to wait to initiate listening until you have connected to the eSense device. Hence, only start listening when you get a ConnectionType.connected event. Below is an example from the carp_esense_package

 // if you want to get the connection events when connecting, set up the listener BEFORE connecting...
    ESenseManager.connectionEvents.listen((event) {
      print('eSense connection : $event');
      // wait until connection is established before listening to events.
      if (!connected && event.type == ConnectionType.connected) {
        connected = true;

        // this is a hack! - don't know why, but the sensorEvents stream needs a kick in the ass to get started...
        ESenseManager.sensorEvents.listen(null);
        this.restart();
      } else {
        connected = false;
      }
    });

    ESenseManager.connect(deviceName);

Also note the hack -- it seems like calling the listen need to be done twice before starting...?

Second, the way you create a stream by using the async* and yield Dart keywords seems not to be recommended in Dart (anymore). Instead, you should use the Stream libraries in Dart. Especially if you want to add items to a stream, use the StreamController class.

I hope this helps.