2016-12-01 130 views
0

我正在尝试构建一个简单的蓝牙串行应用程序来连接到我的arduino。我正在使用ionic2来制作Android应用程序。现在,我所要做的就是列出所有可用的蓝牙设备。下面是我的代码:Bluetooth Serial Ionic 2

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

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

public working:string; 
public var2: string ; 

bluetoothSerial.isEnabled(
    function() { 
     this.working= "Bluetooth is enabled"; 
    }, 
    function() { 
     this.working="Bluetooth is *not* enabled"; 
    } 
); 

public lists = []; 

bluetoothSerial.discoverUnpaired(function(devices) { 
    this.lists.push(devices); 
}, function(){ this.var2 = 'could not find any bluetooth device';}); 

    constructor() { } 

} 

每当我做离子成为我做很多的错误,主要是因为蓝牙无法识别(函数实现缺失)。它也不允许我构建应用程序。

请帮忙。

谢谢你这么多

回答

2

望着文档

isEnabled()

平台:Android的iOS版的Windows Phone

报告是否已启用蓝牙

返回:Promise回报一个承诺

几件事情。你不能这样称呼你的方法。 你应该利用你的BluetoothSerial 你应该把它放在一个函数

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

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

public working:string; 
public var2: string ; 
public lists = []; 

constructor(){ 
    this.getAlBluetoothDevices(); 
} 

// put BluetoothSerial inside a function, can't be called different 
getAllBluetoothDevices(){ 
    // async so keep everything in this method 
    BluetoothSerial.isEnabled().then((data)=> { 
     // not sure of returning value, probably a boolean 
     console.log("dont know what it returns"+data); 

     // returns all the available devices, not just the unpaired ones 
     BluetoothSerial.list().then((allDevices) => { 
      // set the list to returned value 
      this.lists = allDevices; 
      if(!this.lists.length > 0){ 
       this.var2 = "could not find any bluetooth devices"; 
      } 
     }); 
    }); 
    } 
} 

}

+0

非常感谢你的伴侣!我非常感谢你的时间和帮助。我会尝试你的给定的代码,并会返回给你更新。谢谢 –

+0

当然,我自己并没有使用这个库。查看他们的ionic2文档,如果你有一些业余时间:) https://ionicframework.com/docs/v2/native/bluetooth-serial/ – Ivaro18

+0

嘿队友,你的代码通过删除'this.getAlBluetoothDevices()完美地工作。 '从constrcutor(),我不知道为什么它是问题。另外BS.list只列出配对的设备不是不配对的(这也在BS文档中提及)。但是,它的工作伙伴...接下来的事情我会尝试连接到一个Arduino设备,看看它是否工作。 –

0

下面的代码是用于扫描蓝牙设备

startScanning(){ 
     this.isScanning =true; 
      this.bluetoothSerial.isEnabled().then(()=>{ 
      this.bluetoothSerial.discoverUnpaired().then((allDevices)=>{ 
       this.devices=allDevices; 
       this.isScanning =false; 
      }); 
     }); 

而且这个代码是用于连接到设备

onDeviceReady(){ 
    let device = this.navParams.get('device'); 


     this.bluetoothSerial.connect(device.id).subscribe((data)=>{ 
      let alert = this.alertCtrl.create({ 
       title: 'Bluetooth', 
       subTitle: data, 
       buttons: ['ok'] 
       }); 
       alert.present(); 
     },error=>{ 
      let alert = this.alertCtrl.create({ 
       title: 'Bluetooth', 
       subTitle: error, 
       buttons: ['ok'] 
       }); 
       alert.present(); 
     }); 

    } 

注意:这对我很有用