Peripheral: Advertisement

Use obniz as a peripheral device of BLE. In other words, obniz is connected by computers or smartphones.

Generally, resister services and characteristics, and then send the advertisement according to them, assuming that it will be connected. However, advertisement is one of the strongest feature of BLE, and it makes it possible to run for a long time with battery operation because data such as temperature can be sent without connection. It can also be used in case the existence is all you need, such as a lost tag. For those reasons, in obniz, you don't need characteristics to use only the information of advertisement.

Two types of advertisement

There are two types of advertisement of BLE, normal one and scan response.

BLE advertisement continues to send out by itself, but some data may not be sent because its capacity is small. So the central side can ask peripherals whether there is more data, and then, what the peripheral returns is a scan response.

LocalName is usually sent as a scan response. The important thing sent in an advertisement is UUID of its services. It can offer the information about the services it has.

Sending advertisement

You can set both the advertisement and the scan response of it to ble.advertisement.

Specify the advertisement in json format by ble.advertisement.setAdvData(). When you need a scan response, specify it in json, using ble.advertisement.setScanRespData(). Advertisement starts by ble.advertisement.start() and continues to send data until ble.advertisement.end() is called.

In the following way, the device transmits that it has the service whose UUID is 1234 by advertisement, and sends the device name "obniz BLE" in the scan response.

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  obniz.ble.advertisement.setAdvData({
    serviceUuids: ["1234"]
  });

  obniz.ble.advertisement.setScanRespData({
    localName : "obniz BLE",
  });

  obniz.ble.advertisement.start();
}

The transmission interval of advertisement is fixed at 1.28 seconds.

Advanced settings

You can set not only serviceUuids or localName but also other information of advertisement.

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  obniz.ble.advertisement.setAdvData({
    flags: ["general_discoverable_mode","br_edr_not_supported"],
    manufacturerData:{
      companyCode : 0x004C,
      data : [0x02,0x15, 0xC2, 0x8f, 0x0a, 0xd5, 0xa7, 0xfd, 0x48, 0xbe, 0x9f, 0xd0, 0xea, 0xe9, 0xff, 0xd3, 0xa8, 0xbb,0x10,0x00,0x00,0x10,0xFF],
    },
  });
  obniz.ble.advertisement.setScanRespData({
    localName : "obniz BLE",
  });

  obniz.ble.advertisement.start();
}

Settings by binary string

There are two advertisement that can be directly registered in binary format without using json, setAdvDataRaw() and setScanRespDataRaw().

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  obniz.ble.advertisement.setAdvDataRaw([0x02, 0x01, 0x1A, 0x07, 0x09, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65 ]);
  obniz.ble.advertisement.setScanRespDataRaw([0x07, 0x09, 0x53, 0x61, 0x6D, 0x70, 0x6C, 0x65 ]);

  obniz.ble.advertisement.start();
}