简体   繁体   English

Ionic2中的BLE功能

[英]BLE functions in Ionic2

I'm new with Ionic 2 and I need to create an application using Bluetooth functions to connect with other devices like a sensor. 我是Ionic 2的新手,我需要使用蓝牙功能创建一个应用程序以与其他设备(例如传感器)连接。

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 我正在使用Cordova的这个插件: https : //github.com/don/cordova-plugin-ble-central#scan

.ts file .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 file .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>

It is ' ble ' and not ' BLE '. 它是“ ble ”而不是“ BLE ”。 You may even wanna change the import statement. 您甚至可能想要更改导入语句。 I hope it works. 我希望它能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM