Currently, I am using the NativeScript framework along with the nativescript-bluetooth plugin to develop a BLE searching application. However, I am facing a challenge in updating the view while the device is being searched for. Below is the relevant code snippet:
app.modules.ts
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { AppComponent } from "./app.component";
@NgModule({
declarations: [AppComponent],
bootstrap: [AppComponent],
imports: [NativeScriptModule],
schemas: [NO_ERRORS_SCHEMA],
})
export class AppModule {}
app.component.ts
import {Component} from "@angular/core";
let ble = require("nativescript-bluetooth");
@Component({
selector: "my-app",
styleUrls: ['app.css'],
templateUrl: "app.component.html"
})
export class AppComponent {
log: string = "";
startScanning() {
ble.startScanning({
serviceUUIDs: [],
seconds: 5,
onDiscovered: (peripheral) => {
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() {
ble.stopScanning().then(() => {
this.updateLog("Stopped");
})
}
updateLog(message) {
console.log(message);
this.log += `\n${message}`;
}
}
app.component.html
<ActionBar title="My App">
</ActionBar>
<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>
Even though it is expected to display real-time updates in the log while scanning, there seems to be a delay of 5 seconds before it actually reflects the changes due to the completion of ble.startScanning
. This issue might not be directly related to the BLE plugin but rather could point towards Javascript Promise and NativeScript peculiarities.
Your feedback would be highly appreciated.