don / ionic-ble-examples

Ionic Framework Bluetooth Low Energy Examples
102 stars 70 forks source link

Not triggering subscribe in scan function #17

Open raghavendra-synapptra opened 5 years ago

raghavendra-synapptra commented 5 years ago

@tearforfear007 Thanks for this,

Subscribe is not triggering in scan function. can any body help me on this. Using Angular 7 and ionic 4 Kindly find the below code

import { Component, NgZone } from '@angular/core'; import { BLE } from '@ionic-native/ble/ngx'; import { Observable } from "rxjs" // import { NavController } from 'ionic-angular'; // import { ToastController } from 'ionic-angular';

@Component({ selector: 'app-home', templateUrl: 'home.page.html', styleUrls: ['home.page.scss'], }) export class HomePage {

devices: any[] = []; statusMessage: string;

constructor( // public navCtrl: NavController, // private toastCtrl: ToastController, private ble: BLE, private ngZone: NgZone) { }

ionViewDidEnter() { console.log('ionViewDidEnter'); this.scan(); }

onDeviceDiscovered(device) {
console.log('Discovered ' + JSON.stringify(device, null, 2));
this.ngZone.run(() => {
  this.devices.push(device);
});

}

scan() { this.setStatus('Scanning for Bluetooth LE Devices'); this.devices = []; // clear list

this.ble.scan([], 5).subscribe(

  // device => this.onDeviceDiscovered(device), 
  (device) => {
    console.log("inside subscribe")
    this.onDeviceDiscovered(device)}, 
  (error) => {this.scanError(error)}
);

// setTimeout(this.setStatus.bind(this), 5000, 'Scan complete');

}

// If location permission is denied, you'll end up here scanError(error) { this.setStatus('Error ' + error); // let toast = this.toastCtrl.create({ // message: 'Error scanning for Bluetooth low energy devices', // position: 'middle', // duration: 5000 // }); // toast.present(); }

setStatus(message) { console.log(message); this.ngZone.run(() => { this.statusMessage = message; }); }

}