BLE isConnected return null

I’m using the BLE central cordova plugin and I’m able to successfully connect, send commands, etc. However when I call the isConnected command it always returns null. Not sure what I’m missing? Here is my code:

  isConnected(device) : any {
    console.log('checking for connection on device: ' + device.id);
    BLE.isConnected(device.id)
      .then(data => {
        this.notificationData = data;
        console.log('data from is connected:' + data);
      });
  }

There is some discussion about isConnected on plugin page, but i don’t think any data is returned. If device is connected success callback is called, if it is not failiure callback is called.

I only use this method before i call disconnect.

As is mentioned here https://github.com/don/cordova-plugin-ble-central/issues/377 this method reports what app or plugin “thinks”. So app may think that device is connected, but that may not be true anymore. It is confusing :slight_smile:

thanks for the info, that does help, but according to that thread the function should still be returning something. I’ve also been able to successfully call disconnect().

Either there is something wrong with my code above or something wrong with the plugin.

For android it calls success callback without parameters

Thanks again for your help! That pointed me in the right direction. So this seems to work:

  isConnected(device): any {
    console.log('checking for connection on device: ' + device.id);
    this.isConnectedStatus = true;
    BLE.isConnected(device.id)
      .then(function (success) {
        //success
      }, function (error) {
        console.log(error);
        this.isConnectedStatus = false;
      });
  }

Is there a reason you’re not using ionic-native?

ignorance, if there is a better way, please let me know. thx!

ionic-native BLE. Generally, using ionic-native shims for plugins makes for simpler integration, because lots of callback and zone gnarliness is handled for you.

ah ok, yup that’s the one I’m using. technically its ble central. from the docs:

$ ionic plugin add cordova-plugin-ble-central
$ npm install --save @ionic-native/ble

Your syntax in the OP is off, then. Instead of static methods on the BLE class, you want to be injecting an instance and calling object methods on it.

Would you be able to provide an example by chance? There isn’t a single example of a working isConnected call to be found for ble central anywhere online and the docs are outdated. Thanks!