Central: Read and Write

Characteristics have a value, and some properties allow reading and writing via BLE.

characteristic.properties contains properties that tell you what is permitted. You can check if the property is included using the canWrite() function.

console.log(characteristics.properties); // => ['read', 'write', 'notify']
characteristics.canWrite(); // => true

Write

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

  var target = {
      uuids: ["fff0"],
  };
  var peripheral = await obniz.ble.scan.startOneWait(target);
  if(peripheral){
      await peripheral.connectWait();

      console.log("connected");
      await obniz.wait(1000);

      var dataArray = [0x02, 0xFF];
      await peripheral.getService("FF00").getCharacteristic("FF01").writeWait(dataArray);
      console.log("write success");
  }
}

Read

characteristicの値の読み込みにはreadWait() を利用します。書き込んだのが文字列や数値だったとしてもバイト列として返ってきます。

Use readWait() to read the value of characteristics. Even if what was written is a string or a number, it will be returned as a byte string.

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");
    var connected = await peripheral.connectWait();
    if(connected){
      console.log("connected");
      var service = peripheral.getService("1111");
      var char = service.getCharacteristic("2222");
      var data = await char.readWait();
      console.log(data); // => ex [0x02, 0xFF]; 
    }else{
      console.log("failed");
    }
  }
}