Not reciveing my bluetooth device


#1

ive been trying to use the scan function of the BLE plugin. how ever while thought it apparently does scan im not receiving any data. i tried connecting it directly but that’s not working either.

i am trying to connect to a bluetooth module if that helps.

please help. there is so little support about Bluetooth on ionic. the documents are not too useful.

home.ts

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';
import { Platform } from 'ionic-angular';
import { AndroidPermissions } from '@ionic-native/android-permissions';

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

})
export class HomePage {
  scanning: boolean = false;
  data: any[] = [];
  permissions: any[] = ["BLUETOOTH", "BLUETOOTH_ADMIN", "BLUETOOTH_PRIVILEGED"];

constructor(platform: Platform,
            private androidPermissions: AndroidPermissions,
            private ble: BLE,
            public alertCtrl: AlertController,
            public navCtrl: NavController){
              this.ble.isEnabled().then(() => {
                console.log('hurray it bluetooth is on');
            }, (error) => {
                console.log(error);
                  this.ble.enable().then(resp => {
                    console.log("bluetooth is enabled now");
                }, (error) => {
                  console.log('bluetooth was not enabled');
              });
            });
      }

  settings(){
    this.ble.showBluetoothSettings().then((rspo)=>{
      console.log("accesed");
    }, (error) => {
      this.normalAlert("settings error", error);
    })
  }

  connect(){
    this.ble.connect("00:15:83:35:73:CC").subscribe((rspo)=>{
      this.normalAlert("connected to HC-06 device", rspo);
    }, (error) => {
      this.normalAlert("error", error);
    })
  }

    search(){
    
      this.ble.startScan([]).subscribe(
        rspo => {
          this.scanning = true;
          this.data = rspo.id;
      }, (error) => {
        this.scanning = false;
        let alert = this.alertCtrl.create({
          title: 'devices not found',
          message: error,
          buttons: ['ok']
        });
        alert.present();
    })
  }

  stop(){
    this.ble.stopScan();
    this.scanning = false;
  }


    discover(){

    }

/*
    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);
  });
}
*/


  normalAlert(title:string, message:string) {
    let alert = this.alertCtrl.create({
      title: title,
      message: message,
      buttons: ['OK']
    });
    alert.present();
  }
}

home.html

<ion-header>
  <ion-navbar>
    <ion-title>
      Bluetooth Light control
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
    <button ion-button (click) = "settings()">bluetooth settings</button>
    <button ion-button (click) = "connect()">connect to hc-06</button>
    <button ion-button (click) = "search()">start scan</button>
    <button ion-button (click) = "stop()">stop scan</button>

    <p>
      scaning: {{scanning}}<br>
      here is a list of id's...
    </p>

    <ion-list>
      <ion-item *ngFor="let device of data">
        {{device.id | json}}
      </ion-item>
    </ion-list>