Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize callback is only called once and not on every BT status change. #343

Open
corneyl opened this issue Sep 6, 2016 · 4 comments
Labels

Comments

@corneyl
Copy link

corneyl commented Sep 6, 2016

When I call the initialize function, the callback is called as expected, but when I disable or enable Bluetooth, it's not called again. I tried enabling BT using the device settings and using the enable() function, disabling only using the device settings.

I'm using the following code:

var that = this;
// Init bluetooth, and define the status receiver function
bluetoothle.initialize(function(result) {
    if (result.status === "enabled") {
        console.log("Bluetooth is enabled.");

        // Always call handleScan() to start scanning.
        that.handleScan();
    }
    else {
        // Bluetooth is disabled, try to enable
        console.log("Bluetooth is disabled.");
    }

    // Broadcast the enabled and initialised event
    if(!that.initialised) {
        that.initialised = true;
        that.ev.emit('initialised');
    }
    that.enabled = (result.status == 'enabled');
    that.ev.emit('enabled', that.enabled);

}, this.handleError, {
    request: false,
    statusReceiver: true,
    restoreKey : "abc123"
});

And the following device:

  • Samsung Note 10.1 (SM-P600)
  • Android 5.1.1
@randdusing
Copy link
Owner

I no longer have a 5.* device, but this is working fine on my Nexus 5X and Samsung Tab A running 6.*. Can you try using the example app that comes with the Angular wrapper? That's the quickest way to rule out custom code issues.

@sunny-mittal
Copy link

sunny-mittal commented Jan 18, 2017

I'm having this issue as well on my Galaxy S7 running Android 6.0.1. Oddly, it used to work just fine and I haven't modified the plugin or my initialize code at all, so I'm guessing it's a state that's possible to get into but whose cause is unclear. If anyone encounters and resolves this, please post here :)

UPDATE: Just tried with the same app running on iOS 10 and the callback fires for each state change. I wrote the code using my phone as the test device so it certainly used to work.

UPDATE TWO: When I don't use my app code, the callback does fire each time. I'm very confused. Here's the code I used to test in the chrome console vs my app code:

bluetoothle.initialize(function(res) { console.warn(res) }, { request: true }) // manual test
bluetoothle.initialize(function(res) { console.warn(res) }, { request: dev === 'android' ? true : false }) // from the app

I can't think why the request object would change anything but I'll try it without and report back. And yes, dev is defined in the app, so I'm not getting any TypeErrors :)

@randdusing
Copy link
Owner

Are you always calling initialize multiple times? That may be leading to an inconsistent state.

@sunny-mittal
Copy link

sunny-mittal commented Jan 19, 2017

I wrote a wrapper for the initialize call and I have yet to figure out why the callback doesn't fire on occasion but, of course, when trying to repro and debug the issue, it stopped being an issue, so for the time being, I'm going to call it good. My wrapper looks like this, for anyone interested:

        function initialize(status) {
            var dev = ble.sandbox.device.os;
            function check(val) {
                if (val === 'enabled' || val === true) {
                    sessionStorage.setItem('btEnabled', 'true');
                    ble.sandbox.emit('btEnabled');
                } else {
                    ble.sandbox.emit('btDisabled');
                    sessionStorage.removeItem('btEnabled');
                }
            }
            if (!status) {
                ble.initialize(function(res) {
                    check(res.status);
                }, { request: dev === 'android' ? true : false });
            } else {
                isEnabled().then(check);
            }
        }

I turned all of the calls I use into promises so this particular piece of code receives the result of "isInitialized" and handles it accordingly. :) In any case, I can confirm it's not an issue with the plugin; I'm able to test in an isolated environment and observe proper functionality on 7 devices. I only posted here to get feedback from others in case they observed it as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants