0

What is the right way to connect to multiple peripherals and reconnect in when they are disconnecting?

I am aiming connecting to multiple PolartH7 using GATT profile and starting with this sample: https://github.com/cfreeman/WeatherMachine2-hrm

Situation is that peripherals are disconnecting either every 60 seconds or because they are getting out of range and I am unsure what is the right why to handle functions onDiscovered, onConnected and onDisconnected in above case. Anybody can help or address me to sample code (even if it is not golang)?

BTW, what is the maximum number of PolarH7 I can connect to?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
aAWnSD
  • 124
  • 10

1 Answers1

0

I would introduce a pool which stores done channels for each peripheral. Please, see this PR and my comment to it.

Andrey Dyatlov
  • 1,628
  • 1
  • 10
  • 12
  • Thanks. There actually some problems with this 1) needed to allow multiple connections and continue scanning: `//if strings.ToUpper(p.ID()) != strings.ToUpper(...` 2) when a periph disconnects it does not reconnected. Tried to add `p.Device().Connect(p)` at the end of `onDisconnect()` but does not work. – aAWnSD Dec 27 '18 at 16:51
  • I believe that the even the [initial example](https://github.com/cfreeman/WeatherMachine2-hrm) supports multiple connections and it does continue scanning. The main problem with the initial example is that when one peripheral is disconnected, all others will be disconnected too, as you are using a single `done` channel. The pool of done channels I introduced [can solve the problem of duplicated connections as well](https://github.com/adyatlov/WeatherMachine2-hrm/pull/1/files#diff-ab8ca7316e9cc288e39ff47eb2e8ee28R69). – Andrey Dyatlov Dec 28 '18 at 17:50
  • As far as I understand, when a peripheral disconnects, it will be automatically rediscovered, you should not reconnect to it explicitly. If you added `p.Device().Connect(p)` to the initial example, then, I believe, the program will exit anyway. – Andrey Dyatlov Dec 28 '18 at 17:53
  • nope. Even with only one peripheral, your and the original code stops getting data ending in with `INFO: Disconnected from BLE peripheral.` at the first disconnect from peripheral. – aAWnSD Dec 30 '18 at 12:03
  • @aAWnSD, as far as I understand, the device scans for peripherals, and when it finds one, it connects to it. As a debug step, please add a debug logging to the `onPeriphDiscovered` function and figure out if the device which is not disconnected is getting discovered again. – Andrey Dyatlov Dec 31 '18 at 18:10