BLE scan returns [Object object] instead of any devices


#1

im trying to use the scan function to pick up devices. everything seems to work but it doesnt return anything. ive tried it on my phone through ionic view. but still returns [object object]

the goal is the data to be displayed in the alert message.

home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {BLE} from '@ionic-native/ble';
import { AlertController } from 'ionic-angular';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  devices: any;

  constructor(public alert: AlertController, public ble: BLE, public navCtrl: NavController) { }

  showResults(){

  let notice = this.alert.create({
  title: 'devices found',
  message: this.devices,
  buttons: ['OK']
  });
  notice.present();
  }

  enable(){

  this.devices = this.ble.scan([],5);
  this.showResults();

  }
  }

any help would be appreciated :slight_smile:


#2

scan returns an Observable. Type this.devices to match it. As I said on another thread recently, you’ll help yourself a lot if you never declare a variable as any ever again. Then use Observable operators or a subscription to handle the values the Observable emits.


#3

I am so happy not to be the only person tilting at this particular windmill. JavaScript is a horrendous language for many reasons, but TypeScript’s addition of compile-time type checking ameliorates a big one, and it is really short-sighted not to take advantage of it. Not only do you turn runtime bugs into compile-time bugs (which is a huge win in and of itself), but you get an opportunity to make your code self-documenting, which is a major benefit to whoever is trying to maintain it. That is likely to be you in the future.


#4

what is a observer and how do i use/work with them can you provide xample code or atless what i should type into mine


#5

Explanation of Overservables: https://gist.github.com/staltz/868e7e9bc2a7b8c1f754

(@Sujan12 please consider putting this link in some kind of FAQ sticky. It seems as though there are a lot of new posters who have never heard of Observables.)