Ble not found, using BLE plugin with ionic 2


#1

I am creating an app with ionic 2 and am trying to work with the ble-plugin. I ran the installation:

$ cordova plugin add cordova-plugin-ble-central

then wrote the following in my page’s TS:

import {Page, Alert, NavController} from ‘ionic-angular’;

@Page({
  templateUrl: 'build/pages/hello-ionic/hello-ionic.html'
})

export class HelloIonicPage {

        constructor(public nav: NavController) { }
        bleScan() {
            ble.scan([], 5, function(device) {
                console.log(JSON.stringify(device));
            }, failure);
        }
}

However, ble isn’t recognised so my code is throwing errors. Do I need to inject a dependancy or something, why isn’t this working?

I have looked at a number of examples (aimed at ionic 1), and can’t see any references to imports etc.


#2

Are you using Ionic Native (the new plugin wrapper for Ionic 2) ? see here


#3

Thanks for this, I was not and have followed the linked tutorial. This is now working, although the plugin seems to differ from the docs in its repo. This function is fine,

   newDevice(){
        BLE.scan([this.GasSenseStrip.service], 5)
    }

but if I add two more functions ( a success callback and a failure callback, I get overloaded errors. e.g.

   newDevice(){
        BLE.scan([this.GasSenseStrip.service], 5, this.success, this.failure)
    };
    
    success(peripheral){//console log code etc.};
    failure(){//more relevant code};

Why is this? I am happy to use the syntax which isn’t throwing errors, but if so, what happens to the results?


#4

because the scan method returns Promise. So you have syntax error


#5

Thank you, so what might a demo look like, here is a rough guess…

BLE.scan([this.GasSenseStrip.service], 5)
            .then(function(data) {
                // promise fulfilled
                if (data.peripheral === 'what_we_were_expecting?') { //does peripherial exist?
                    // Display / Connect etc. ?
                }
            }, function(error) {
            //console.log("error" + error);
            });

Are there any docs of what is returned, or examples of how to deal with this?


#6

TypeScript uses types, so you can see the type from d.ts
Or, if you use something like intellij, you can use ctrl + mouse hover on method - it is shows a tooltip with types.

PS: maybe I am wrong, and the static method scan returns Observable. Just check it.