Bluetooth serial list() doesnt show any connected devices


#1

so i finally got the function to run however it doesnt display any connected devices. currently i have a hc-06 Bluetooth module connected. but it does not display any data of any device. from the Docs https://github.com/don/BluetoothSerial#list apparently i get four bits of data. id, class, address and name. i upload it to ionic view and test my app. it comes back with the successful list function however nothing is displayed.

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


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'

})
export class HomePage {
  infomation: any = [];

constructor(public alertCtrl: AlertController, public navCtrl: NavController){}

ionViewDidLoad(){

    this.infomation = {id: "hey it works"};


    BluetoothSerial.isEnabled().then(() => {
      console.log("bluetooth is enabled all G");
    }, () => {
      console.log("bluetooth is not enabled trying to enable it");
          BluetoothSerial.enable().then(() => {
              console.log("bluetooth got enabled hurray");
                }, () => {
                  console.log("user did not enabled");
          })
    });
}

 search(){
    BluetoothSerial.list().then((data) => {
        JSON.stringify( data );
        console.log("hurray" + data);
        this.showDevices("Found Devices", data.id, data.class, data.address, data.name);
      },
      (error) => {
      console.log("could not find paired devices because: " + error);
      this.showError(error);
    });
  }

  discover(){
    BluetoothSerial.discoverUnpaired().then ((device) => {
          BluetoothSerial.setDeviceDiscoveredListener().subscribe( (device) => {
            JSON.stringify( device );
            this.infomation = {id: device.id};
            this.showDevices("Unparied devices", device.id,device.class,device.address,device.name);
            console.log(device.id);

      });
  },
      (error) => {
        this.showError(error);
        console.log("could not find unparied devices " + error);
  });
}

  showDevices(title, id, type, address, name) {
    let alert = this.alertCtrl.create({
      title: title,
      message: id + type + address + name,
      buttons: ['OK']
    });
    alert.present();
  }

  showError(info) {
    let alert = this.alertCtrl.create({
      title: "Error",
      message: info,
      buttons: ['OK']
    });
    alert.present();
  }

}


any help would be great thank you.


#2

i have also just discovered that even if i turn off my bluetooth connection. it will still use the devices found alert even though it should technically fail i would think. and my enable bluetooth function doesnt propt me to enable it.


#3

Are you aware that you are importing the Ionic Native plugin but are using the Cordova plugin directly instead of the Ionic Native wrapper?


#4

no I guess not. what is the diffrence


#5

This is https://ionicframework.com/docs/native/ble/
It offers a wrapper around the direct Cordova plugin’s methods to better match with how Ionic/Angular does things.
Unfortunately there are no real Usage examples on that site… but you would do this https://ionicframework.com/docs/native/ble/#Usage and then use the methods on this.BLE.
Maybe googling or searching here in the forum you can find better examples.