evothings / cordova-ble

Bluetooth Low Energy plugin for Cordova
http://www.evothings.com/
Apache License 2.0
242 stars 103 forks source link

connectToDevice calls both the success and failure callbacks in some cases #146

Open kapetan opened 6 years ago

kapetan commented 6 years ago

There is an issue where the plugin first calls the success callback and later the failure callback. I would expect that either the success or failure callback fires, not both.

evothings.ble.connectToDevice(
  device,
  function () {
    // called first
  },
  function () {
    // never called
  },
  function (errorCode) {
    // called second
  })

The logic to reproduce:

  1. Try to connect to device
  2. GattHandler#onConnectionStateChange is called with GATT_SUCCESS and STATE_CONNECTED.
  3. GattHandler sends a message back to the JavaScript code which triggers the success callback.

After some time the device is disconnected (e.g. turned off).

  1. GattHandler#onConnectionStateChange is called with !GATT_SUCCESS (the status argument is not GATT_SUCCESS) and STATE_DISCONNECTED.
  2. GattHandler now calls callbackContext.error triggering the failure callback in the JavaScript code.

I've experienced this a couple of times.