Scanning Ble Peripheral and Connecting to It

BLE master: scanning while connected

Yes, a central can scan while connections to other peripherals are established. Please note that an individual peripheral may stop advertising when it is connected to a central. This may add confusion when testing.

Here is a minimal demonstration using noble:

var noble = require('noble');
var connectedIDs = {};

noble.on('stateChange', (state) => {
if (state == 'poweredOn') {
noble.startScanning([], true);
}
});

noble.on('discover', (peripheral) => {
if (connectedIDs[peripheral.id] == 'known') {
console.log(new Date() + ' ' + peripheral.id + ' discovered again');
} else {
console.log(new Date() + ' ' + peripheral.id + ' discovered first time')
connectedIDs[peripheral.id] = 'known';
// periodically connect to the same peripheral so we get the error
// message telling us that we are already connected
setInterval( () => {
peripheral.connect( (err) => {
if (err) {
console.log(new Date() + ' ' + peripheral.id + ' ' + err);
return;
}
console.log(new Date() + ' ' + peripheral.id + ' connected');
});
}, 1000);
}
});

This code example connects to all peripherals it discovers and simultaneously logs the id of peripherals it receives advertisement data from.

Output (truncated some columns and lines):

10:51:06 8652... Error: Peripheral already connected
10:51:06 567b... Error: Peripheral already connected
10:51:06 f0ba... Error: Peripheral already connected
10:51:06 d095... Error: Peripheral already connected
10:51:06 3800... Error: Peripheral already connected
10:51:07 6c20... discovered first time
10:51:07 f0ba... Error: Peripheral already connected
10:51:07 d095... Error: Peripheral already connected
10:51:07 3800... Error: Peripheral already connected
10:51:07 6c20... discovered again
10:51:08 8652... Error: Peripheral already connected
10:51:08 567b... Error: Peripheral already connected
10:51:08 f0ba... Error: Peripheral already connected
10:51:08 6c20... discovered again
10:51:08 d095... Error: Peripheral already connected
10:51:08 8652... Error: Peripheral already connected
10:51:08 3800... Error: Peripheral already connected
10:51:08 6c20... discovered again
10:51:08 f0ba... Error: Peripheral already connected
10:51:08 d095... Error: Peripheral already connected
10:51:08 3800... Error: Peripheral already connected
10:51:08 6c20... connected
10:51:08 6c20... connected

What happened?

Multiple peripherals have already been connected to noble when 6c20...is discovered the first time. The error messages of the repeating connection attempts show that the peripherals are still connected while scanning. In my example run, I had 6 BLE peripherals connected and 6c20... was the 7th to join the party. I used noble v1.3.0 and node v4.2.4 on a MacBookPro mid-2015 with OS X 10.11.3.

This doesn't answer the second question: Does this behavior depend on the actual modules or on the Bluetooth Low Energy specification?

AFAIK, the BLE specification doesn't specify an amount of connections a central has to support. AFAIK because the BLE Core Spec is rather big and all references to simultaneously and multiple connections I could find state that it depends on the implementation. So, this seems to totally depend on the BLE product which consists of hardware and software. Even if the BLE spec would specify it, I would prefer to check the datasheet of the product. In addition, it depends on the software running on the host, too. Luckily, noble does.

To give an example, let's have a look at the datasheet of Nordic's Soft Device S120: https://www.nordicsemi.com/eng/nordic/download_resource/26275/14/32008006

The S120 soft device is a BLE stack (firmware) for Nordic's NRF51 series of BLE ICs (most known example is the nRF51822).

Here is quote from the datasheet linked above:

The SoftDevice supports eight concurrent master connections and an
additional Scanner/Initiator role. When the maximum number of
simultaneous connections are established, the Scanner role will be
supported for new device discovery though the initiator is not
available at that time.

BLE peripheral: scanning while connected

The answer to this differs depending on the version of Bluetooth that your device is on.

For Bluetooth v4.0:

A peripheral cannot scan at all, whether in a connection or not. The peripheral can only send out adverts or accept incoming connections. For more information you can have a look at this document, page 18:-

http://chapters.comsoc.org/vancouver/BTLER3.pdf

For Bluetooth v4.1 onwards:

A device can be in central and peripheral role at the same time. By implication, this means that if a peripheral is connected to a central device, it can still scan for devices in it's "central mode". More information can be found in this link, page 5:-

http://www.ietf.org/proceedings/89/slides/slides-89-6lo-4.pdf

So in conclusion, if your device is BT v4.1 or newer, then this is dependant on the chip manufacturer, and if it is v4.0, then no, because the Bluetooth specification does not allow it.

Continue scanning for other BLE devices while connecting to one

My experience shows that at least some Huawei devices have issues when you connect to a peripheral while the scanning is in progress (I confirmed it on P8 Lite and P9) with two different peripherals.

When the scanning was in progress I was receiving a COMMAND_DISALLOWED for the LE_CREATE_CONNECTION command. Communication between the host and BLE chip was extracted from HCI Snoop Log. The only workaround that worked for me was to stop the scan for a period of establishing the connection.

We do not have any statistics to prove it - unfortunately.



Related Topics



Leave a reply



Submit