Skip to content

Wait for le scan to stop before connecting to device#2

Open
steinerj wants to merge 1 commit intosandeepmistry:masterfrom
steinerj:patch-1
Open

Wait for le scan to stop before connecting to device#2
steinerj wants to merge 1 commit intosandeepmistry:masterfrom
steinerj:patch-1

Conversation

@steinerj
Copy link
Copy Markdown

There seems to be a bug in certain Intel BT chipsets which require a LE device scan to be stopped before being able to connect to a device.

Discovered in this issue: #1

There seems to be a bug in certain Intel BT chipsets which require a LE device scan to be stopped before being able to connect to a device.
@steinerj
Copy link
Copy Markdown
Author

Didn't pay attention, apologies... This PR depends on noble/noble-device#25
I'm not sure it makes sense to reflect that in versioning yet, I'll leave that up to you. I don't think this is the ultimate fix either way.

Comment thread lib/bbc-microbit-io.js

this._microbit = microbit;
//Certain Intel BT Chipsets seem to require a scan stop before connections are allowed, otherwise they'll throw a: Connection Rejected due to Limited Resources
BBCMicrobit.stopScanning(() => {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the minimum version of Node.js needed for the () => { syntax? I'm leaning towards just keeping it as an old school function here. Thoughts?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK Node 4.0.0. for arrow functions. But happy to change it...

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please.

Comment thread lib/bbc-microbit-io.js
this.emit('ready');
this._microbit.connectAndSetUp(function(error) {
if (error) {
console.error(error);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we emit the error here instead of logging it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whichever suits your style really :) I had a "throw" initially but felt that was too strong, but thought an emit might be "too weak". Can change.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think emit is the J5 way, it should bubble up the error.

Comment thread lib/bbc-microbit-io.js

this._microbit = microbit;
//Certain Intel BT Chipsets seem to require a scan stop before connections are allowed, otherwise they'll throw a: Connection Rejected due to Limited Resources
BBCMicrobit.stopScanning(() => {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a few days so I can't visualise the entire call stack anymore. But if I remember correctly, the discover event listeners are triggered before scanning actually stops on a device level (the actual race condition). The proper solution would probably be to wait for a stopScanning-callback within util.js and then emit discover. But I was hesitant to change a lot in util.js because I didn't have the time to fully understand the effects on other libs depending on noble-device. Also this seems to be an Intel-specific problem, so even more limited scope.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I see what's needed, we're not waiting for the stop scan to complete.

I think this change would be better to have in noble-device.

Change

constructor.discover = function(callback) {
      var onDiscover = function(device) {
        constructor.stopDiscoverAll(onDiscover);

        callback(device);
      };

      callback._nobleDeviceOnDiscover = onDiscover;

      constructor.discoverAll(onDiscover);
    };

to something like

constructor.discover = function(callback) {
      var onDiscover = function(device) {
        constructor.stopDiscoverAll(onDiscover, function() {
          callback(device);
        ).bind(this));
      };

      callback._nobleDeviceOnDiscover = onDiscover;

      constructor.discoverAll(onDiscover);
    };

We'll need to change stopDiscoverAll to include an additional callback parameter. What do you think? Do you have time to PR this?

Comment thread lib/bbc-microbit-io.js

this._microbit = microbit;
//Certain Intel BT Chipsets seem to require a scan stop before connections are allowed, otherwise they'll throw a: Connection Rejected due to Limited Resources
BBCMicrobit.stopScanning(() => {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the minimum version of Node.js required to use the () => { syntax? I'm leaning towards just using the old school function syntax here.

Comment thread lib/bbc-microbit-io.js
this.emit('ready');
this._microbit.connectAndSetUp(function(error) {
if (error) {
console.error(error);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we emit the error here instead of logging it?

@sandeepmistry
Copy link
Copy Markdown
Owner

@steinerj thanks for submitting! I've made a few review comments above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants