我正在使用插件nativescript-bluetooth
,我想显示设备。
我尝试了这段代码,但对我来说却行不通。
Component.ts
export class ItemsComponent implements OnInit {
log: string = "";
constructor(private zone: NgZone) { }
ngOnInit(): void {
}
startScanning() {
bluetooth.startScanning({
serviceUUIDs: [],
seconds: 5,
onDiscovered: (peripheral) => {
console.log(peripheral)
this.zone.run(() => {
this.updateLog(`Device Name: ${peripheral.name}`);
})
if (peripheral.name == "SWING") {
this.stopScanning();
this.updateLog(`Device Name: ${peripheral.name}`);
}
}
}).then(() => {
this.updateLog("scanning completed");
}, (err) => {
console.error(`Error on scanning: ${err}`);
})
}
stopScanning() {
bluetooth.stopScanning().then(() => {
this.updateLog("Stopped");
})
}
updateLog(message) {
console.log(message);
this.log += `\n${message}`;
}
}
Component.html
<StackLayout>
<Button class="btn btn-primary btn-active" id="appButton" text="Search Device" (tap)="startScanning()"></Button>
<TextView text="{{ log }}" style="height: 100%;background-color: #282a37;color: #fff;" editable="false"></TextView>
</StackLayout>
请,您能问我任何想法,为什么此代码不起作用,还是可以发布任何有效的示例?
我需要您的支持! thnx