BLE在Ionic2中起作用

时间:2016-11-27 10:52:53

标签: android ionic-framework bluetooth bluetooth-lowenergy ionic2

我是Ionic 2的新手,我需要使用蓝牙功能创建一个应用程序来连接传感器等其他设备。

现在我想在单击按钮扫描,连接,断开设备时调用函数,但我的功能不起作用。

我正在使用这个cordova插件: https://github.com/don/cordova-plugin-ble-central#scan

.ts文件

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文件

<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 个答案:

答案 0 :(得分:0)

' ble '而不是' BLE '。您甚至可能想要更改import语句。我希望它有效。