I’m using the BLE cordova plugin in my new app. My app is written with Ionic v5 and Angular 9.
Here is the code that scan for available devices:
import { Component, OnInit } from '@angular/core';
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';
import { BLE } from '@ionic-native/ble/ngx';
import { LoadingController, Platform } from '@ionic/angular';
@Component({
selector: 'app-test',
templateUrl: './test.page.html',
styleUrls: ['./test.page.scss'],
})
export class TestPage implements OnInit {
devices: any[] = [];
constructor(
private platform: Platform,
private androidPermissions: AndroidPermissions,
private ble: BLE,
private loadingCrl: LoadingController
) { }
ngOnInit() {
this.platform.ready().then(() => {
if (this.platform.is('android') || this.platform.is('ios')) {
if (this.platform.is('android')) {
this.androidPermissions
.requestPermissions([
this.androidPermissions.PERMISSION.BLUETOOTH_ADMIN,
this.androidPermissions.PERMISSION.BLUETOOTH
])
.then(() => this.scan());
} else {
this.scan();
}
}
});
}
scan() {
this.loadingCrl.create({ message: 'Scanning... ' }).then(loading => {
loading.present();
console.log('Scanning...');
this.ble.scan([], 5).subscribe(device => {
console.log(JSON.stringify(device));
this.devices.push(device);
loading.dismiss();
});
});
}
}
The app asks for location permissions but doesn’t ask for bluetooth. The scanning runs forever.
I run my app on an Android 9 device with:
ionic cordova run android -l
1 post - 1 participant