I’m using the Cordova BLE Plugin and have been testing on Android 5.1.1 and 6+ devices. BLE.enable() seems to work just fine so I know the plugin works, but BLE.scan() is giving me trouble. Here’s the code I have (which is based off of this example):
import {Page} from 'ionic-angular';
import {BLE} from 'ionic-native';
@Page({
templateUrl: 'build/pages/bluetooth/bluetooth.html'
})
export class BluetoothPage {
constructor() {}
scan() {
var foundDevice;
var bleApp = {
init: function() {
statusDiv.innerHTML = "Initializing";
document.addEventListener('deviceready', this.startScan, false);
console.log("Operations completed");
},
startScan: function() {
foundDevice = false;
statusDiv.innerHTML = "Scanning";
function scanFailure(reason) {
statusDiv.innerHTML = "Scan error";
alert("Scan Failed");
}
function scanSuccess(peripheral) {
foundDevice = true;
statusDiv.innerHTML = "Found device";
alert("Device Found");
}
BLE.scan([], 25, scanSuccess, scanFailure);
setTimeout(function() {
if (!foundDevice) {
statusDiv.innerHTML = "No device found";
}
}, 25000);
}
}
bleApp.init();
}
And then a button will simply call scan(). There are no errors with building this, and although the HTML will change text and time out after 25 seconds, it never finds a device (meaning no alert ever appears). My location data is on as well. Any advice?
EDIT: I’m becoming more confused. If I change the success and failure calls in BLE.scan() to just be simple alerts, they are both called one after the other no matter what. Even with Bluetooth off.