0
votes

I'm new with Ionic 2 and I need to create an application using Bluetooth functions to connect with other devices like a sensor.

Now I want to call functions when I click the button to scan, connect, disconnect devices but my function doesn't works.

I'm using this plugin of cordova: https://github.com/don/cordova-plugin-ble-central#scan

.ts file

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


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

  constructor(public navCtrl: NavController) {}

  ionViewDidLoad() {
    console.log('Hello SyncPage Page');
  }

    testing(){
        console.log('Test button');    
    }

    BLE.scan([], 5, function(device) {
    console.log(JSON.stringify(device));
        }, failure);



      BLE.startScan([], function(device) {
    console.log(JSON.stringify(device));
        }, failure);


}

.html file

<ion-header>

  <ion-navbar>
      <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Sync</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>
    <ion-buttons end>
      <button ion-button (click)="testing()"><ion-icon name="add"></ion-icon>Test</button>
    </ion-buttons>  
    <ion-buttons end>
      <button ion-button (click)="scan()"><ion-icon name="add"></ion-icon>Scan</button>
    </ion-buttons>  
    <ion-buttons end>
      <button ion-button (click)="startScan()"><ion-icon name="add"></ion-icon>Startscan</button>
    </ion-buttons>  

</ion-content>
1
"Does not work" is very vague. Please clarify what happens exactly (any logs?). What device are you using for testing? - vzsg
I use node.js ionic serve in brower but doesn't run duplicate identifier, BLE not recognized etc - gladius
You need to test on a real device. BLE doesn't work in the browser, neither in simulators. - vzsg
There are errors with the functions: duplicate identifiere 'failure' - gladius

1 Answers

0
votes

It is 'ble' and not 'BLE'. You may even wanna change the import statement. I hope it works.