'm using BluetoothLE plugin and I want to get a list of active Bluetooth devices and click to connect with the device using ionic 4
statusMessage: string;
constructor(public bluetoothle: BluetoothLE,
public platform: Platform,
private ngZone: NgZone,
public toastController: ToastController) {
this.platform.ready().then((readySource) => {
console.log('Platform ready from', readySource);
this.bluetoothle.initialize().subscribe(ble => {
console.log('ble', ble.status) // logs 'enabled'
});
});
}
adapterInfo() {
this.bluetoothle.getAdapterInfo().then((success) => {
console.log("adapterInfo: " + success);
this.setStatus(success.name);
})
}
startScan() {
let params = {
"services": [
"180D",
"180F"
],
"allowDuplicates": true,
}
this.bluetoothle.startScan(params).subscribe((success) => {
console.log("startScan: " + success);
this.setStatus(success.address);
}, (error) => {
console.log("error: " + error);
this.scanError(error);
})
}
stopScan() {
this.bluetoothle.stopScan().then((resp) => {
console.log("stopScan: " + resp);
this.setStatus(resp.status);
})
}
retrieveConnected() {
let params = {
"services": [
"180D",
"180F"
]
}
this.bluetoothle.retrieveConnected(params).then((resp) => {
console.log("retrieveConnected: " + resp);
this.setStatus("retrieveConnected");
})
}
// If location permission is denied, you'll end up here
async scanError(error: string) {
this.setStatus('Error ' + error);
const toast = await this.toastController.create({
message: 'Error scanning for Bluetooth low energy devices',
position: 'middle',
duration: 5000
});
toast.present();
}
setStatus(message: string) {
console.log("message: " + message);
this.ngZone.run(() => {
this.statusMessage = message;
});
}
<ion-header>
<ion-toolbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-button (click)="adapterInfo()">AdapterInfo</ion-button>
<ion-button (click)="startScan()">StartScan</ion-button>
<ion-button (click)="stopScan()">StopScan</ion-button>
<ion-button (click)="retrieveConnected()">RetrieveConnected</ion-button>
</ion-content>
<ion-footer>
<ion-toolbar>
<p>{{ statusMessage }}</p>
</ion-toolbar>
</ion-footer>
Please help