angular - 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');
}
});
}
解决方案
推荐阅读
- xml - Sed commdand - 当字符串多次出现时,查找和替换字符串模式不起作用
- reactjs - react组件中onChange方法过多导致的Boilerplate如何消除
- typo3 - 根据 1 级具有不同 2 级的排版菜单
- karate - 如何从不同的场景访问场景的响应?
- jquery - 粘贴在 Mac/Safari 中的 asp.net Web 应用程序的十进制字段中不起作用
- node.js - 无法从 AWS s3 上的浏览器上传文件
- mysql - 返回第一个错误的 MySQL 错误处理
- java - MAVEN 如何加载“相同”的依赖 JAR 文件?
- r - Calculate the percentage of missing values per month in a dataframe
- c# - 使用 C# 关闭浏览器选项卡时如何获取时间?换句话说,是否有任何查询可以获取浏览器上选项卡的开始时间和结束时间?