我刚刚开始学习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>
我的bluetooth-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"
});
}
);
}
}
您正在为阵列分配外设。您应该使用TypeScript进行严格的类型检查,以确保不会发生像这样的简单错误。
onDiscovered: (peripheral: Peripheral) => {
observablePeripheralArray.push(peripheral);
}
此外,我不明白为什么你警告“错误发生!”当你发现一个新的外围设备。