Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 230902

How callbacks work

$
0
0

Thanks for the reply. The list is working properly I grabbed that code from an older example. But I got it to work thanks to some YouTube videos.

Thanks to Paul Halliday here:
Halliday Async

and Max here:
Max - Callback & Promises

scan(){
    console.log('Scan button pressed')
    this.setStatus('Scanning...') ;
    this.devices = [];  // clear list

    this.ble.startScan([]).subscribe(
    device => this.foundDevice(device), 
    error => this.onError(error)
    );

   this.scanTimer = setTimeout(() => {this.stopscan()}, 20000 );  
  
  }

  async stopscan(){
    const a = await this.ble.stopScan();
    this.setStatus(this.status.rdy);
    
  }

  async deviceSelected(device){
    this.devices = [];
    clearTimeout(this.scanTimer);
    const a = await this.ble.stopScan();    
    this.setStatus('Connecting to ' + device.name);
    this.ble.connect(device.id).subscribe(
      peripheral => this.onConnected(peripheral),
      peripheral => {console.log('Disconnected')});  
                   
  }

  onConnected(peripheral){
    this.ngZone.run(() => {
      this.btP.connectedTo = peripheral;
      this.setStatus('Connected to ' + this.btP.connectedTo.name);      
    });    
            
  }//onConnected

The use of the async allowed me to clean up my code quite a bit, I’m sure there are better ways but its better than it was. It really came down to the absence of the this.ngZone.run .


Viewing all articles
Browse latest Browse all 230902

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>