ionic2中的蓝牙串口,没有设备列表

时间:2017-02-18 12:19:44

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

我在Ionic2应用程序中使用蓝牙序列,我想列出可能连接的设备。

现在在.ts文件中有这个但是没有用。我可以启用蓝牙,但无法查看是否找到设备或列出它们。

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

@Component({
  selector: 'page-page1',
  templateUrl: 'page1.html'
})
export class Page1 {
public working:string;
public var2: string ;
public lists = [];

  constructor(private alertCtrl: AlertController, public platform: Platform, public navCtrl: NavController) {
    platform.ready().then(() => {
      BluetoothSerial.isConnected()
        .then(() => {
          console.log('is connected');
        }, (err) => {
          console.log(' not connected')
        })
        });
      }


       enableBluetooth(){
         BluetoothSerial.enable()
         }
         discoverBluetooth(){   
        /*   BluetoothSerial.setDeviceDiscoveredListener(function(device) {
          console.log('Found: '+device.id);
        });*/
         }
         unpairedBluetooth(){

         BluetoothSerial.discoverUnpaired().then(function(devices) {
               devices.forEach(function(device) {
        console.log(device.id)});
            })

         }


  listDevices(){

           BluetoothSerial.isEnabled().then((data)=> {
                console.log(data);
           BluetoothSerial.list().then((allDevices) => {
            this.lists = allDevices;

                let result = [];
             for (var key in this.lists) {          
               result.push(key);}

           })




})}}

在.html文件中,我已经尝试了几个列表来展示,但这没有任何结果。

<ion-content padding>


    <ion-buttons>
<button ion-button (click) = "enableBluetooth()">Enable!</button>
</ion-buttons>
 <ion-buttons>
<button ion-button (click) = "listDevices()">List devices</button>
</ion-buttons>
Lijst1
<ion-list>
        <ion-item *ngFor="let list of lists">{{list.devices}}</ion-item>
    </ion-list>
    Lijst2
    <ion-list>
        <ion-item *ngFor="let list of lists">{{list.device}}</ion-item>
    </ion-list>
    Lijst3
    <ion-list>
        <ion-item *ngFor="let list of lists">{{list.allDevices}}</ion-item>
    </ion-list>
    Lijst4
    <ion-list>
        <ion-item *ngFor="let key of lists">{{list.allDevices}}</ion-item>
    </ion-list>
    Lijst5
    <ion-list>
        <ion-item *ngFor="let key of lists">{{key}}</ion-item>
    </ion-list>
     Lijst
    <ion-list>
        <ion-item *ngFor="let list of lists">{{list}}</ion-item>
    </ion-list>
 <ion-buttons>
<button ion-button (click) = "unpairedBluetooth()">Unpair</button>
</ion-buttons>
</ion-content>

1 个答案:

答案 0 :(得分:0)

您可以尝试这种方式:

BluetoothSerial.list(function(result){
   console.log(result);
},
function(failure){
   console.log(failure);
});

如果您还没有查看,建议您查看插件的自述文件和示例。它会有很多帮助。 https://github.com/don/BluetoothSerial