首页 > 解决方案 > NativeScript 中的 Bluetooth-pulgin 存在问题:ERROR TypeError: ... is not a function?

问题描述

我目前正在尝试使用从 Github 获得的这段代码来学习 NativeScript 中的蓝牙连接。
到目前为止一切都很好。
但是在我运行应用程序后,我收到此错误消息“错误类型错误:bluetooth.startScanning 不是函数”,现在我不知道如何解决这个问题或问题是什么。编写此应用程序的程序员不会出现错误消息。

我会很感激你的帮助。

蓝牙连接.ts

    import { Injectable } from '@angular/core';
    import { BleDevice } from '../models/ble-device.model';
    var bluetooth = require('nativescript-bluetooth');


    @Injectable()
    export class BluetoothConnection {
      bleDevicesAround: Array<BleDevice> = new Array;

      write(bluetoothMessage): void {
          console.log('Writing message: ' + JSON.stringify(bluetoothMessage));
          bluetooth.write(bluetoothMessage)
              .then((result) => console.log("Value written " + JSON.stringify(result)),
              (error) => console.log("Write error: " + error));
      }

      fixPermission(): void {
          bluetooth.hasCoarseLocationPermission()
              .then((granted) => {
                  console.log("Has location permission ? " + granted);

                  if (!granted) {
                      bluetooth.requestCoarseLocationPermission()
                          .then(() => console.log("Location permission requested"));
                  }
              });
      }

      connect(UUID: string): Promise<any> {
          return bluetooth.connect({
              UUID: UUID,
              onConnected: (peripheral) => {
                  console.log("Periperhal connected with UUID: " + peripheral.UUID);
                  peripheral.services.forEach(function (service) {
                      console.log("Service found: " + JSON.stringify(service));
                  });
              },
              onDisconnected: (peripheral) => {
                  console.log("Periperhal disconnected with UUID: " + peripheral.UUID)
              }
          });
      }

      disconnect(UUID: string): void {
          bluetooth.disconnect({ UUID: UUID })
              .then(() => console.log("Disconnected successfully"),
              (err) => console.log("Disconnection error: " + err));
      }

      scan() {
          console.log('Scanning...');
          this.bleDevicesAround = new Array;
         
            return bluetooth.startScanning({
                serviceUUIDs: [],
                seconds: 3,
                onDiscovered: (device) => {
                    const bleDevice = new BleDevice(device.UUID, device.name, device.state);
                    this.bleDevicesAround.push(bleDevice);
                }
            });
      }
    }

使用方法的地方:

bleDevicesAround: Array<BleDevice> = new Array;
connectToMagicBlue() {
            this.bluetoothService.scan().then(() => {
                console.log('Scanning completed');
                this.magicBlue = this.getMagicBlue();
                if (this.magicBlue) {
                    console.log('Magic blue found');
                    this.bluetoothService.connect(this.magicBlue.UUID)
                        .then((device) => console.log('Connected: ' + JSON.stringify(device)));
                } else {
                    console.log('Device not found');
                }
            });
    }

标签: angularnativescriptandroid-bluetoothangular2-nativescript

解决方案


推荐阅读