Reputation: 9229
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?
Upvotes: 1
Views: 3455
Reputation: 302
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";
});
});
Upvotes: 1
Reputation: 68
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);
});
Upvotes: 1
Reputation: 679
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/
Upvotes: 0