首页 > 解决方案 > 获取蓝牙设备列表并使用 IONIC cordova bluetoothle 插件进行连接

问题描述

我想获取附近蓝牙设备的列表并与之建立连接,为此我正在使用 IONIC 科尔多瓦蓝牙插件。但它没有按预期工作,我收到一条错误消息,上面写着“扫描蓝牙低功耗设备时出错”。
这是我的代码:

import { Component, OnInit, NgZone } from '@angular/core';
import { BluetoothLE } from '@ionic-native/bluetooth-le/ngx';
import { Platform, ToastController } from '@ionic/angular';

@Component({
  selector: 'app-bluetooth-connect',
  templateUrl: './bluetooth-connect.page.html',
  styleUrls: ['./bluetooth-connect.page.scss'],
})
export class BluetoothConnectPage implements OnInit {
  statusMessage: string;

  constructor(
    public bluetoothle: BluetoothLE,
    public plt: Platform,
    private toastController: ToastController,
    private ngZone: NgZone,
  ) {

    this.plt.ready().then((readySource) => {

      console.log('Platform ready from', readySource);

      this.bluetoothle.initialize().subscribe(ble => {
        console.log('ble', ble.status); // logs 'enabled'
        this.bluetoothle.enable();
      });

    });
  }

  adapterInfo() {
    this.bluetoothle.getAdapterInfo().then((success) => {
      console.log('adapterInfo: ' + success);
      this.setStatus(success.name);
    });
  }

  startScan() {
    const params = {
      services: [
        '180D',
        '180F'
      ],
      allowDuplicates: true,
      scanMode: this.bluetoothle.SCAN_MODE_LOW_LATENCY,
      matchMode: this.bluetoothle.MATCH_MODE_AGGRESSIVE,
      matchNum: this.bluetoothle.MATCH_NUM_MAX_ADVERTISEMENT,
      callbackType: this.bluetoothle.CALLBACK_TYPE_ALL_MATCHES,
    };
    this.bluetoothle.startScan(params).subscribe((success) => {
      console.log('startScan: ' + JSON.stringify(success));
      this.setStatus(success.address);
    }, (error) => {
      console.log('error: ' + error);
      this.scanError(error);
    });
  }  

  handleError(error) {    
    console.log('handle error executed');
  }

  stopScan() {
    this.bluetoothle.stopScan().then((resp) => {
      console.log('stopScan: ' + resp);
      this.setStatus(resp.status);
    });
  }

  retrieveConnected() {
    const params = {
      services: [
        '180D',
        '180F'
      ]
    };

    this.bluetoothle.retrieveConnected(params).then((resp) => {
      console.log('retrieveConnected: ' + resp);
      this.setStatus('retrieveConnected');
    });
  }

  // If location permission is denied, you'll end up here
  async scanError(error: string) {
    this.setStatus('Error ' + error);
    const toast = await this.toastController.create({
      message: 'Error scanning for Bluetooth low energy devices',
      position: 'middle',
      duration: 5000
    });
    toast.present();
  }

  setStatus(message: string) {
    console.log('message: ' + message);
    this.ngZone.run(() => {
      this.statusMessage = message;
    });
  }

  ngOnInit() {
  }

}

这是我的模板代码:

<ion-header>
  <ion-toolbar>
    <ion-title>bluetooth-connect</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
  <ion-button (click)="adapterInfo()">AdapterInfo</ion-button>
  <ion-button (click)="startScan()">StartScan</ion-button>
  <ion-button (click)="stopScan()">StopScan</ion-button>
  <ion-button (click)="retrieveConnected()">RetrieveConnected</ion-button>
</ion-content>

<ion-footer>
  <ion-toolbar>
    <p>{{ statusMessage }}</p>
  </ion-toolbar>
</ion-footer>

请帮忙。

标签: cordovabluetooth-lowenergyionic4

解决方案


推荐阅读