Я только начал использовать NativeScript и создал простое приложение, в котором я пытаюсь сканировать устройства Bluetooth.
Я использовал плагин "nativescript-bluetooth" , но startScanning ()не находит какое-либо устройство (или, может быть, находит только устройства LE bluetooth).
Однако я хочу найти все доступные устройства.
В моем коде неправильно указано smt?Есть ли какой-нибудь другой плагин или способ его достижения?
my bluetooth-page.component.html file:
<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>
my 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"
});
}
);
}
}