简体   繁体   中英

ble trouble with IONIC 2

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?

You need to add import as following:

import {BLE} from 'ionic-native';

and use it like this:

 BLE.scan([], 5).subscribe(device => {
      console.log(JSON.stringify(device));
    }, error => {
      console.log(error);
    });

First add top your page in TS

import {BLE} from 'ionic-native'

Just use in your TS page

    this.platform.ready().then(() => {

        BLE.enable();

        BLE.startScan([]).subscribe(device => {

            console.log(JSON.stringify(device));              
        },
            err => {
                //this.message = "Error";
            });
    });

在使用ble之前,您需要先将其导入。

Try this? I'm also stumbling on a ionic 2 project that will involve BLE API use, but I haven't started doing it yet. http://www.joshmorony.com/using-cordova-plugins-in-ionic-2-with-ionic-native/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM