Central: Advanced conditional scanning

Advanced conditional scanning

binary filter

You can filter by the binary partial match of Advertisement/Scan Response.
For example,

  • Flags

    LE General Discoverable Mode

    Simultaneous LE and BR/EDR to Same Device Capable (Controller)

    Simultaneous LE and BR/EDR to Same Device Capable (Host)

  • Local Name

    "Sample"

This would be the following array of advertisements with information

// Javascript Example

[0x02, 0x01, 0x1a, 0x07, 0x09, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65]

The first [0x02, 0x01, 0x1a] is Flags information,
the second [0x07, 0x09, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65] LocalName.

If you want to search only for advertisements with these flags, you can use [0x02, 0x01, 0x1a] with the data of [02, 0x01, 0x1a] as the filter condition.
You can write it here.

// Javascript Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var binaryFilter = [0x02, 0x01, 0x0a];
  var target = {
    binary: [binaryFilter]
  };

  obniz.ble.scan.onfind = function(peripheral){
   console.log(peripheral)
  };

  obniz.ble.scan.onfinish = function(){
   console.log("scan timeout!")
  };

  await obniz.ble.scan.startWait(target, {filterOnDevice:true});
}

When using the binary filter, you need to filter on the device side, so you can use
The filterOnDevice option is set to true.
See also "Filter on device to reduce traffic" below

binary filter allows for partial matches, so it is possible to include "ampl" in localName.
You can also search for a device.

The advertisement when "Sample" is LocalName is
[0x07, 0x09, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65] was.
The "ample" part of this will be [0x61, 0x6d, 0x70, 0x6c], so Set this as a condition.

Translated with www.DeepL.com/Translator (free version)

// Javascript Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var binaryFilter = [0x61, 0x6d, 0x70, 0x6c]; //ample
  var target = {
    binary: [binaryFilter]  
  };

  obniz.ble.scan.onfind = function(peripheral){
   console.log(peripheral)
  };

  obniz.ble.scan.onfinish = function(){
   console.log("scan timeout!")
  };

  await obniz.ble.scan.startWait(target, {filterOnDevice:true});
}

In this case, I know it's rarely a good idea, but other information in the advertisement (such as If an array of [0x61, 0x6d, 0x70, 0x6c] is found in (e.g. UUID)
Please note that this applies to you as well.

Combining multiple conditions

The filter can specify more than one condition.
In that case, all of the conditions are OR searches. Instead of stating multiple conditions to narrow down the device, the
Use this to search for multiple devices at once.

To search for a device whose localName is "Blank" or "Beacon", use The
You can describe multiple conditions enclosed in an Array as follows.

// Javascript Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var target = {
    localName: ["Blank", "Beacon"]  //scan Blank *OR* Beacon
  };

  obniz.ble.scan.onfind = function(peripheral){
    console.log(peripheral.localName)
  };

  obniz.ble.scan.onfinish = async function(peripherals, error){
    console.log("scan timeout!")
  };

  await obniz.ble.scan.startWait(target);
}

In addition to searching by the same local name, you can also add other search criteria, such as a UUID The following information is required.

You can get the results that meet any of the following conditions by writing the following.
Please note that this is all an OR search.

  • localNameが"Blank"
  • localNameが"Beacon"
  • Service UUID has 1111
  • Service UUID has 2222
  • binary include [0x02, 0x01, 0x1a]
  • binary include [0x61, 0x6d, 0x70, 0x6c]
// Javascript Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();

  var target = {
    localName: ["Blank", "Beacon"], 
    uuids: ["1111", "2222"] , 
    binary: [[0x02, 0x01, 0x1a], [0x61, 0x6d, 0x70, 0x6c]]
  };

  obniz.ble.scan.onfind = function(peripheral){
    console.log(peripheral.localName)
  };

  obniz.ble.scan.onfinish = async function(peripherals, error){
    console.log("scan timeout!")
  };

  await obniz.ble.scan.startWait(target);
}

Filtering on devices to reduce traffic

BLE filtering is performed by the SDK (obnizjs) if you don't specify anything.
In other words, the information is delivered from obnizOS to the SDK, and the SDK filters it out, and onfind is called by hand.

Turning on the filterOnDevice setting will not cause communication to the SDK. You can filter within obnizOS.

// Javascript Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var binaryFilter = [0x02, 0x01, 0x0a];
  var target = {
    binary: [ binaryFilter ]
  };

  obniz.ble.scan.onfind = function(peripheral){
   console.log(peripheral)
  };

  obniz.ble.scan.onfinish = function(){
   console.log("scan timeout!")
  };

  await obniz.ble.scan.startWait(target, {filterOnDevice:true});
}