startScanning()在NativeScript应用程序中找不到任何设备

时间:2019-04-10 17:15:20

标签: android bluetooth-lowenergy nativescript angular2-nativescript nativescript-angular

我刚开始学习NativeScript,并制作了一个简单的应用程序,试图扫描蓝牙设备。

我使用了“ nativescript-bluetooth” 插件,但是startScanning()找不到任何设备(或者可能仅找到LE蓝牙设备)。

但是,我想找到所有可用的设备。

我的代码错了吗?还有其他插件或实现方式吗?

我的 bluetooth-page.component.html 文件:

<StackLayout>
  <Label text="Bluetooth Connection!!!"></Label>
  <Button text="Check Bluetooth" (tap)="doIsBluetoothEnabled()" class="button button-positive"></Button>
  <Button text="Enable Bluetooth" (tap)="doEnableBluetooth()" class="button button-positive"></Button>
  <Button text="Scan devices" (tap)="doStartScanning()" class="button button-neutral"></Button>
  <Button text="Stop Scanning" (tap)="doStopScanning()" class="button button-danger"></Button>

  <GridLayout rows="*">
    <ListView items="{{ peripherals }}" itemTap="onPeripheralTap" separatorColor="#90c3d4">
      <ng-template>
        <StackLayout orientation="horizontal" class="padded-label">
          <StackLayout class="padded-label-stack">
            <Label horizontalAlignment="right" width="30" text="{{ RSSI }}" class="rssi-label"></Label>
          </StackLayout>
          <StackLayout class="padded-label-stack">
            <Label text="{{ name }}" class="title-label" textWrap="true"></Label>
            <Label text="{{ UUID }}" class="uuid-label" textWrap="true"></Label>
          </StackLayout>
        </StackLayout>
      </ng-template>
    </ListView>
  </GridLayout>
</StackLayout>

我的 bluetooh-page.component.ts 文件:

import { Component } from "@angular/core";

var dialogs = require("tns-core-modules/ui/dialogs");
var bluetooth = require("nativescript-bluetooth");
var observable = require("tns-core-modules/data/observable");
var observableArray = require("tns-core-modules/data/observable-array");

var observablePeripheralArray = new observableArray.ObservableArray();
var peripherals = observablePeripheralArray;

@Component({
  selector: "ns-home-page",
  templateUrl: "./home-page.component.html",
  styleUrls: ["./home-page.component.css"],
  moduleId: module.id
})
export class HomePageComponent {
  doIsBluetoothEnabled() {
    bluetooth.isBluetoothEnabled().then(function(enabled) {
      dialogs.alert({
        title: "Bluetooth Enabled: ",
        message: enabled ? "Yes" : "No",
        okButtonText: "OK"
      });
    });
  }

  doEnableBluetooth() {
    bluetooth.enable().then(function(enabled) {
      setTimeout(function() {
        dialogs.alert({
          title: "Enable Bluetooth",
          message: enabled ? "Yes" : "No",
          okButtonText: "OK"
        });
      }, 500);
    });
  }

  // this one uses automatic permission handling
  doStartScanning() {
    // reset the array
    observablePeripheralArray.splice(0, observablePeripheralArray.length);
    bluetooth
      .startScanning({
        serviceUUIDs: [], // pass an empty array to scan for all services
        seconds: 10, // passing in seconds makes the plugin stop scanning after <seconds> seconds
        onDiscovered: function(peripheral) {
          dialogs.alert({
            title: "Error occured!",
            message: peripheral.UUID,
            okButtonText: "OK"
          });
          observablePeripheralArray = peripheral;
        }
      })
      .then(
        function() {
          dialogs.alert({
            title: "Scanning is complete",
            message: "Scanning is complete",
            okButtonText: "OK"
          });
        },
        function(err) {
          dialogs.alert({
            title: "Error occured!",
            message: err,
            okButtonText: "OK"
          });
        }
      );
  }

  doStopScanning() {
    bluetooth.stopScanning().then(
      function() {
        dialogs.alert({
          title: "Stop Scanning",
          message: "Scanning is stopped",
          okButtonText: "OK"
        });
      },
      function(err) {
        dialogs.alert({
          title: "Error occured!",
          message: err,
          okButtonText: "OK"
        });
      }
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您正在将外围设备分配给阵列。您应该将TypeScript与严格的类型检查结合使用,以确保不会发生诸如此类的简单错误。

onDiscovered: (peripheral: Peripheral) => {
          observablePeripheralArray.push(peripheral);
        }

此外,我不明白您为什么要警告“发生错误!”当您发现新的外围设备时。