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.
// Javascript Example
console.log(characteristics.properties); // => ['read', 'write', 'notify']
characteristics.canWrite(); // => true
Write
- writeWait(number array): Write data array.
- writeNumberWait(number): Receive numbers and write them as a data array.
- writeTextWait(): UTF8-encode character strings and write them as a data array.
- readWait(): Read data in an array.
// Javascript Full Example
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.
// Javascript Full 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");
try{
await peripheral.connectWait();
console.log("connected");
var service = peripheral.getService("1111");
var char = service.getCharacteristic("2222");
var data = await char.readWait();
console.log(data); // => ex [0x02, 0xFF];
}catch(e){
console.log("connection failed");
}
}
}