如何从离子3中的蓝牙读取条形码扫描仪数据

时间:2018-05-30 06:33:49

标签: android bluetooth ionic3

我有一个演示应用,我想在Ionic 3中使用蓝牙插件从条形码扫描器读取/接收数据。我想,只要条形码扫描器扫描代码,数据显示到我的应用程序。 我的.ts文件看起来像这样。

import { Component } from '@angular/core';
import { BluetoothSerial } from '@ionic-native/bluetooth-serial';
import { AlertController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'

})
export class HomePage {

  unpairedDevices: any;
  pairedDevices: any;
  gettingDevices: Boolean;
  inputData: string="";
  data: string="";
  constructor(private bluetoothSerial: BluetoothSerial, private alertCtrl: AlertController) {
    bluetoothSerial.enable();
  }

  startScanning() {
    this.pairedDevices = null;
    this.unpairedDevices = null;
    this.gettingDevices = true;
    this.bluetoothSerial.discoverUnpaired().then((success) => {
      this.unpairedDevices = success;
      this.gettingDevices = false;
      success.forEach(element => {

      });
    },
      (err) => {
        console.log(err);
      })

    this.bluetoothSerial.list().then((success) => {
      this.pairedDevices = success;
    },
      (err) => {

      })
  }
  success = (data) => alert(data);
  fail = (error) => alert(error);

  selectDevice(address: any) {

    let alert = this.alertCtrl.create({
      title: 'Connect',
      message: 'Do you want to connect with?',
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        },
        {
          text: 'Connect',
          handler: () => {
            this.bluetoothSerial.connect(address).subscribe(this.success, this.fail);
                 this.bluetoothSerial.available().then(data =>{

                  console.log("Available " + (data));

                  this.bluetoothSerial.read().then(data =>{

                    console.log("Read " + (data));
                    this.data=this.inputData ;

                  //  this.navCtrl.push(WelcomePage);

                  });
                });
                      }

        }
      ]
    });
    alert.present();

  }



  disconnect() {
    let alert = this.alertCtrl.create({
      title: 'Disconnect?',
      message: 'Do you want to Disconnect?',
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        },
        {
          text: 'Disconnect',
          handler: () => {
            this.bluetoothSerial.disconnect();
          }
        }
      ]
    });
    alert.present();
    }
}

和我的 html 文件看起来像这样

    <ion-list padding>
  <button ion-button block (click)="startScanning()">scan</button>
  <ion-list-header>
    Paired Devices
  </ion-list-header>
  <ion-item *ngFor="let device of pairedDevices">
    {{device.name}}
  </ion-item>
  <button ion-button block (click)="disconnect()">Disconnect</button>
  <ion-list-header>
    availlable Devices
  </ion-list-header>
  <ion-item *ngFor='let device of unpairedDevices'>
    <span (click)="selectDevice(device.address)">
      {{device.name}}
    </span>
  </ion-item>
  <ion-spinner name="crescent" *ngIf="gettingDevices"></ion-spinner>
      <ion-label stacked>display</ion-label>
      <ion-input  type="text" readonly=""  [(ngModel)]="inputData">{{data}}</ion-input>
    </ion-item>    

</ion-list>

我的蓝牙插件工作正常。条形码扫描仪已连接,但是当我扫描条形码时,数据未显示。我不知道为什么。 请帮助我。

1 个答案:

答案 0 :(得分:0)

您不需要使用此插件。只需通过blootooth将其与您的手机配对,它就可以在任何输入上正常工作。