cordova-plugin-bluetoothle icon indicating copy to clipboard operation
cordova-plugin-bluetoothle copied to clipboard

iOS: initialize promise resolve not called

Open rastafan opened this issue 7 years ago • 12 comments

Hi everyone,

we encountered an issue when using this plugin on iOS. We are using Ionic V1. Basically, we put this code at the very start of the app (we tried both in the app.js and at the first page enter):

new Promise(function (resolve) {
	bluetoothle.initialize(resolve, {
		request: true,
		statusReceiver: false
	});
}).then(function (result) {
	if (result.status === "enabled") {
		log("Bluetooth is enabled.");
		log(result);
	} else {
		log("Bluetooth is not enabled:", "status");
		log(result, "status");
	}
}, handleError);

but it never enters the success function, nor the error handler. It appears to be working fine in Android, the only difference is that we call the same code but in the success function of the bluetoothle.requestPermission function.

Any ideas? Did we miss something?

rastafan avatar Mar 31 '18 15:03 rastafan

Are you waiting to call initialize until Cordova is ready?

randdusing avatar Mar 31 '18 18:03 randdusing

Yes, everything is started inside the ionicPlatform.ready() callback. I forgot to say that even though the success and error methods are not triggered, successive scans and read/write operations are most of the time successful, and sometimes it says "Bluetooth0 not enabled". We tested it on iPhone X with iOS 11.3

rastafan avatar Apr 01 '18 17:04 rastafan

Does the example app in the angular wrapped cause the same issue?

randdusing avatar Apr 03 '18 02:04 randdusing

The angular wrapped version seems to work fine, thanks, we totally missed that. We will probably stick to that.

rastafan avatar Apr 04 '18 19:04 rastafan

I have the same issue in iOS. Android works fine.

initialize doesn't generate any callback. I've tried both with and without a promise (not sure why a promise is needed), and with and without restoreKey. This is after deviceready and bluetoothle is initiallized (startScan can be called, but fails). There are also plist settings for location and Bluetooth. There are no error messages.

Oddly, it worked for a while, and then very much not, so I suspected conflict with other apps, and removed all apps that might use BLE in the background. No difference.

I checked the Angular example and couldn't see anything obvious that differed.

This is the code ("initBLE" is shown, but nothing else) :

function initBLE() {
    log("initBLE", true);

    var promise = new Promise(function(resolve) {
        bluetoothle.initialize(resolve, {
            request: true,
            statusReceiver: false,
            restoreKey: "abirotesteddystoneurl"
        });
    });

    promise.then(initializeSuccess, handleError);
}

function initializeSuccess(result) {
    log(result, true);

    if (result.status === "enabled") {
        log("Bluetooth is enabled", true);
    }
    else {
        log("Bluetooth is not enabled", true);
    }
}

function handleError(result) {
    log(result, true);

    if (result.error && result.message) {
        log("Error on " + result.error + ": " + result.message, true);
    }
    else {
        log(result, true);
    }
}

andersborgabiro avatar Jun 17 '18 11:06 andersborgabiro

Something is wrong here:

This works on iOS:

    bluetoothle.initialize(resolve, { });

This does not (no callback):

    bluetoothle.initialize(resolve, {
        request: false,
        statusReceiver: false
    });

andersborgabiro avatar Jun 19 '18 06:06 andersborgabiro

I had a similar issue when porting in the Ionic v1 app some commits from Android to IOS, including a cordova-plugin-bluetoothle upgrade from 2.4.0 to 4.4.4 and the corresponding modifications in calls to library's methods such as initialize. I found out that the problem came from not having upgraded the plugin inside the IOS platform, as I had done previously inside the Android platform, because npm install upgrades the plugins in node_modules honoring package.json, but it doesn't upgrade them inside platforms/ios. You must ionic cordova plugin rm cordova-plugin-bluetoothle and then ionic cordova plugin add cordova-plugin-bluetoothle.

aroncal avatar Oct 17 '18 06:10 aroncal

Our team has the same problem, updating the package didn't work either. At the moment, the only solution we've found is very dirty and involves bypassing the initialize callback, instead resolving the promise after a timeout. The initialization, as we found, does happen, the only thing that doesnt work is the callback.

psanty avatar Mar 19 '19 17:03 psanty

@randdusing This is still a problem, and i suspect this is also the reason why there are issues with the connect callback with autoConnect.

JesperBalslev avatar Feb 03 '20 08:02 JesperBalslev

Confirmed from here too ...

This works:

bluetoothle.initialize(resolve, {
    "request": true
    });

but adding statusReceiver and restoreKey causes the callback to never happen. Works A-OK on Android.

hschultz1960 avatar May 05 '20 22:05 hschultz1960

Any update on this, other than omitting the statusReceiver and restoreKey? This is still an issue in version 6.5.1, and the workaround by @hschultz1960 still works.

SamuelMichelRiad avatar Jul 19 '21 22:07 SamuelMichelRiad

I get the same behavior on ios 14.8.1. And the fix from @hschultz1960 (👍 ) seems to work.

foofel avatar Jun 21 '22 18:06 foofel