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

Trying to connect to 2 tags at once #37

Open
hardillb opened this issue Apr 9, 2015 · 43 comments
Open

Trying to connect to 2 tags at once #37

hardillb opened this issue Apr 9, 2015 · 43 comments

Comments

@hardillb
Copy link
Contributor

hardillb commented Apr 9, 2015

As mentioned over in the Node-Red side of things, I'm having issues connecting to multiple tags at once.

The second discover never happens. I'm wondering if the discovery phase is shutdown by the connect call. If so I guess the only way to do it is to call discover all, save the list of discovered tags and then connect after a suitable time out period?

var SensorTag = require('sensortag');

SensorTag.discoverByUuid("34b1f7d136b9",function(tag){
    console.log("found " + tag.uuid);
    tag.connect(function(){
        tag.discoverServicesAndCharacteristics(function(){
            tag.notifySimpleKey(function(err){
                console.log(err);
            });
            tag.on('simpleKeyChange', function(left, right){
                console.log("button " + left + " 34b");
            });
        })
    });


});

SensorTag.discoverByUuid('1cba8c20caf3',function(tag){
    console.log("found " + tag.uuid);
    tag.connect(function(){
        tag.discoverServicesAndCharacteristics(function(){
            tag.notifySimpleKey(function(err){
                console.log(err);
            });
            tag.on('simpleKeyChange', function(left, right){
                console.log("button " + left + " 1cb");
            });
        })
       });
});
@sandeepmistry
Copy link
Owner

@hardillb I can't seem to reproduce locally with 2 CC2650 sensor tags (I only own one CC2540 SensorTag, so can't test with two of those). Tried on my Mac and Raspberry Pi.

As well, a slightly modified version:

var SensorTag = require('sensortag');

var UUID_1 = '68c90b06bc81';
var UUID_2 = '68c90b044580';

SensorTag.discoverByUuid(UUID_1, function(tag1) {
    console.log('1: found ->' + tag1.uuid);

    tag1.connectAndSetUp(function() {
        console.log('1 -> connect and set up');

        tag1.notifySimpleKey(function(err) {
            console.log('1: notifySimpleKey' + (err ? err : 'success'));
        });
    });

    tag1.on('simpleKeyChange', function(left, right) {
        console.log('1: button ->' + right);
    });

    tag1.once('disconnect', function() {
        console.log('1: disconnected');
    });
});

SensorTag.discoverByUuid(UUID_2, function(tag2) {
    console.log('2: found ->' + tag2.uuid);

    tag2.connectAndSetUp(function() {
        console.log('2: connect and set up');

        tag2.notifySimpleKey(function(err) {
            console.log('2: notifySimpleKey' + (err ? err : 'success'));
        });
    });

    tag2.on('simpleKeyChange', function(left, right) {
        console.log('2: button ->' + right);
    });

    tag2.once('disconnect', function() {
        console.log('2: disconnected');
    });
});

Could you please provide debug logs: DEBUG=* node <files>.js

@hardillb
Copy link
Contributor Author

I still get the same with your code and 2 version 1 SensorTags.

The debug log is here: https://gist.github.com/hardillb/d17331c148128334eae0

@sandeepmistry
Copy link
Owner

@hardillb any change in behaviour if you swap uuid's/BT address order?

The debug log only shows one discover event. To get more info, you could add some log statements in noble's startScanning and stopScanning methods, to verify stop scanning isn't been called.

@hardillb
Copy link
Contributor Author

Changing the order doesn't change anything (apart from which tag gets to connect, it's always the first one).

I'll try and find some time later to have a poke lower down the stack

@sandeepmistry
Copy link
Owner

@hardillb any progress on this?

I'm wondering if something in the Bluetooth stack (hardware, driver, kernel version) doesn't allow for scanning while connected. What adapter and kernel version are you using?

@hardillb
Copy link
Contributor Author

Sorry, not been in the office long enough to play with this again recently. I'll try and have another look this week.

@hardillb
Copy link
Contributor Author

This is all running on Fedora Linux 20 with the following

kernel: Linux bagend 3.19.5-100.fc20.x86_64 #1 SMP Mon Apr 20 19:51:16 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

bluez: 5.18

hardware: Broadcom Corp. BCM20702 Bluetooth 4.0

not had chance to add anything to the code yet

@sandeepmistry
Copy link
Owner

@hardillb thanks, one thing to try is scan only (no connecting) - something like:

var SensorTag = require('sensortag');

SensorTag.discoverByUuid("34b1f7d136b9",function(tag){
    console.log("found " + tag.uuid);
});

SensorTag.discoverByUuid('1cba8c20caf3',function(tag){
    console.log("found " + tag.uuid);
});

@sandeepmistry
Copy link
Owner

@hardillb ping ... any progress?

@piekstra
Copy link

I am having similar issues trying to connect to a list of CC2650 sensortags at the same time:
Here is a code-snippet (not actually tested but quite similar to my implementation)

var async = require('async');

var devices = [ "68c90b06a104",
                "68c90b043a01",
                "68c90b043a02",
                "68c90b043a03",
                "68c90b043a04"]

var asynchronous = true;
var SensorTag = require('./index');

if (asynchronous) { 
    async.forEach(devices, function(deviceMac, callback) {
      //directly connect to device
      SensorTag.discoverByUuid(deviceMac, function(err) {
        console.log("found " + deviceMac); 
        callback();
      });  
    }, function (err) {
      console.log('All found!');
      process.exit(0);
    }); 
} 

@hardillb
Copy link
Contributor Author

@sandeepmistry Sorry got side tracked with various things.

I've run the code you suggested and it's discovering both:

$ sudo node test.js
[sudo] password for hardillb: 
found 34b1f7d136b9
found 1cba8c20caf3`

So it looks like it's the connection part

@sandeepmistry
Copy link
Owner

@hardillb cool, so next step would be trying to connect to both after both are discovered ... please try that out and report back.

@hardillb
Copy link
Contributor Author

The following code seams to be working:

var SensorTag = require('sensortag');
var tags = [];

SensorTag.discoverByUuid("34b1f7d136b9",function(tag){
    console.log("found " + tag.uuid);
    tags.push(tag);
});

SensorTag.discoverByUuid('1cba8c20caf3',function(tag){
    console.log("found " + tag.uuid);
    tags.push(tag);
});

setTimeout(function(){
   console.log("timer");
   tags.forEach(function(tag){
      tag.connectAndSetUp(function(){
          console.log("connected " + tag);

         tag.notifySimpleKey(function(err) {
            console.log('1: notifySimpleKey' + (err ? err : 'success'));
         });

     });
     tag.on('simpleKeyChange',function(left,right){
        console.log(left + " - " + right);
     });
   })
},5000);

@martin-doyle
Copy link
Contributor

I am having a similar issue.
I have a setup with 3 SensorTags 2650 and once in 3 starts one of the SensorTags is not discovered. What I found is that the discovery with noble and noble-device tests work fine but the peripheral.advertisement.localName is sometimes "undefined".

Then the function "is" does not recognize the device as a SensorTag:

CC2650SensorTag.is = function(peripheral) {
  var localName = peripheral.advertisement.localName;

  return (localName === 'CC2650 SensorTag') ||
          (localName === 'SensorTag 2.0');
};

@martin-doyle
Copy link
Contributor

I wrote a test which works with my setup: 3 SensorTags 2650 on a Raspberry via a USB Bluetooth dongle.

I set the parameter SensorTag.SCAN_DUPLICATES = true; in order to be able to connect even if the SensorTag does not send the advertisement data the first time. In addition I can reconnect just in case the connection gets lost somehow.
Secondly I stop discovering until the discovered SensorTag is connected in order to make sure it is connected only once. Then I restart discovering.

https://github.com/martin-doyle/SensorTagMQTT/blob/master/testSensorTag.js

@sandeepmistry
Copy link
Owner

@martin-doyle please open a new issue for the discovery issue, I would be happy to merge a pull request for SensorTag.SCAN_DUPLICATES = true; as well.

It would be good to get an hcidump capture from the scenario of trying to connect while scanning is taking place.

@martin-doyle
Copy link
Contributor

@sandeepmistry I am not sure which part you want to pull in. Do you mean the whole test?

@sandeepmistry
Copy link
Owner

@martin-doyle just the SensorTag.SCAN_DUPLICATES = true; part if it fixes #45 for you.

@jeremyjohnson
Copy link

Hi! I have a related question, which may possibly stem from a misunderstanding of how BLE is meant to function. Sorry if this is the wrong place to ask this - if it is, please can you point me at the correct one? Thanks! Following on from hardlib's suggestion of a list of known sensortags, If one wanted to connect to and disconnect from an array of sensortags of known address/ID, is the 'discovery' phase always necessary, or can one go directly to the 'connect' step? To express it in terms of Sandeep's node.js code: in a node.js setup, is using one of the 'discover' factories the only way to instantiate a sensorTag object, or is there some kind of SensorTag.new() pattern where an ID can be assigned from a list of already-known IDs? My issue is that in order for discoverById to succeed, the tag must be forced to advertise (or flashed with new firmware to constantly advertise in the absence of a connection, which would kill the battery)

@hardillb
Copy link
Contributor Author

I think that if the tag is not connected or advertising it is powered down so you couldn't connect to it anyway

@sandeepmistry
Copy link
Owner

@jeremyjohnson your question is a bit off topic to this issue. Gitter is a better place for questions like this: https://gitter.im/sandeepmistry/node-sensortag

However, I agree with @hardillb's answer above. The discover phase is required by this library, as the BLE lib it uses (noble) requires it.

@ScottChapman
Copy link

I may have run into a similar situation. I am only seeing one of my two 2650's discovered with discoverAll.

Is it really discoverAny?

@martin-doyle
Copy link
Contributor

What I found is that discovering stops when some data is sent. This the case when you do connectAndSetUp.
In order to fix that I do the following:

  1. Discover All
  2. On connect stop discovering
  3. When connected start discovering again

You can find a test script at https://github.com/martin-doyle/SensorTagMQTT/blob/master/testSensorTag.js.

I could connect up to 5 SensorTags so far. Please let me know if that works for you.

@ScottChapman
Copy link

So, I;ve tried connecting by ID, address, and all and they all seem to stop once I connect to the first one.

It appears the only way to do this, as you suggest, is to stop/start discovery after each discovery. Will work to incorporate your code into what I am trying.

Thanks!

@martin-doyle
Copy link
Contributor

@ScottChapman Just curious. Did it work?

@ScottChapman
Copy link

@martin-doyle - Yep! I was able to leverage your code for my app. Seems like the pattern could be incorporated into the node-sensortag code to effectively provide expected behavior.

Thanks!

@sandeepmistry
Copy link
Owner

@ScottChapman

Seems like the pattern could be incorporated into the node-sensortag code to effectively provide expected behaviour.

I can put a patch upstream in noble to fix this, would you have time to test if I do this?

@ScottChapman
Copy link

Sure, maybe not right away but would be happy to noodle on it.

@martin-doyle
Copy link
Contributor

@sandeepmistry I would test the patch, too.

@mcleod-ic
Copy link

Q Could the following problem be related to having two 2540 SensorTags?

The problem:
I have two SensorTags at home and the SensorTag node shows up in NodeRed but never loads anything. As soon as one tag is connected, node-red crashes.

Background:
Using Edison with node-red 0.9 and node 0.10 (currently part-way through a remake of node 0.10 to get it to 0.12, as noble seems to want this)
I do see data from the SensorTags on other applications, so I was wondering if the presence of the two together might be resulting in 0 payloads.
I have used hci-tool to scan, pair and trust each of the tags, and they do show up in bluetoothctl

@sandeepmistry
Copy link
Owner

@mcleod-ic I would suggest you create an issue here: https://github.com/node-red/node-red-nodes

@danielbh
Copy link

danielbh commented Apr 21, 2016

@martin-doyle Thanks for the advice. It's my best option at the moment. @sandeepmistry is there any news on if this is fixable? I'd be happy to help test or fix things. Just not sure where to start.

@dbrendon
Copy link

@sandeepmistry Was there ever a patch in noble for handling multiple devices (specifically CC2650)?

@stefanluyten
Copy link

@sandeepmistry Hi - is there a noble patch for handling multiple CC2650?

@sandeepmistry
Copy link
Owner

@danielbh @danielbh @stefanluyten unfortunately I haven't looked at a patch for the noble side yet.

@danielbh
Copy link

This is no longer an issue for me. I realized as long as I do good control flow and dont connect while discovering it works great. Thanks for your hard work!

@danielbh
Copy link

By the way I connect 8 tags at one time. I found it to be the hard limit, but I think you say that in the docs somewhere.

@sandeepmistry
Copy link
Owner

@danielbh cool, I'm up for adding a note about "good flow control" and maximum to the read me. Please start a pull request to get the discussion going. The maximum is a bit tricky, as it's hardware/firmware and OS dependent (https://github.com/sandeepmistry/noble#maximum-simultaneous-connections).

@saurabh-prophecy
Copy link

@sandeepmistry : i am connecting 3-4 sensortags..first time they all got connected but after restarting /rebooting my rpi few of them got disconnected. I think while connecting some of the sensortags they got stuck inside connectAndSetup. what should i do to resolve this???

@sandeepmistry
Copy link
Owner

@saurabh-prophecy your issue seems unrelated to the original topic, please open a new issue with more details for your specific problem.

@inanbayram
Copy link

@danielbh how did you connect 8 tags at one time? After the first tag, I cannot connect the second one :(

@martin-doyle
Copy link
Contributor

Look at the comments about flow control. (9 Feb 2016 and the following)

@mukutprasad1992
Copy link

Hi, I have two noble listeners one is listening only and another one is scan and connect. If Noble is scanning and then connect after that 1 connection attempts never automatically start scanning. I have to start scanning manually "Noble.startScanning()" after every broadcast. Is there any solution to prevent them. Thanks in advance.

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

No branches or pull requests