Central: Receiving change notifications

In addition to one-way read/write from central, BLE has a system that informs change of value from peripheral devices to central devices; it is called Notify.

What is Notify?

Notify is a function allows peripheral to inform central that the value of this characteristics has changed. You can use this by following steps.

  1. Set from central to peripheral that you want to monitor this characteristics.
  2. Peripheral can send Notify to central if the value changes (actually, even if it doesn't change).
  3. Central receives it and then reads the value to get the latest information.

Monitoring registration and data reception

Use characteristic.registerNotifyWait() to register characteristics for Notify. The function specified as an argument is performed when Notify comes from peripheral.

// Javascript Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var peripheral = await obniz.ble.scan.startOneWait({ localName: "Blank" });
  if(peripheral) {
    console.log("found");
    await peripheral.connectWait();
    console.log("connected");
    var service = peripheral.getService("1111");
    var char = service.getCharacteristic("2222");
    char.onregisternotify = async () => {
      console.log("register finshed")
    }
    await char.registerNotifyWait((data) => {
      console.log("notify with data " + data.join(','));
    });
  }
}

It is also possible to describe in callback format using characteristic.registerNotify() and characteristic.onregisternotify.

Unregister

Use characteristic.unRegisterNotifyWait() when you don't have to receive Notify.

// Javascript Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var peripheral = await obniz.ble.scan.startOneWait({ localName: "Blank" });
  if(peripheral) {
    console.log("found");
    await peripheral.connectWait();
    console.log("connected");
    var service = peripheral.getService("1111");
    var char = service.getCharacteristic("2222");
    await char.registerNotifyWait(async (data) => {
      console.log("notify with data " + data.join(','));
   await char.unregisterNotifyWait();
    });
  }
}

It is also possible to describe in callback format using characteristic.unRegisterNotify() and characteristic.onunregisternotify.