obniz.js / obnizOS 3にはいろいろな新機能があります。
- 起動画面がかっこよくなる!
- BLEがより使いやすく!
- (1Yのみ)Sleep機能が使えるように!
これらの新機能を使うにはファームウェアとプログラムのアップデート両方が必要です。
※ファームウェアの更新方法は、遠隔OSアップデート(OTA) を参照してください。
コンテンツ
読み込むobniz.jsのバージョン変更
HTMLタグ内の[email protected]のところを3.0.Xに変更します
変更前
<script src="https://unpkg.com/[email protected]/obniz.js" crossorigin="anonymous"></script>
変更後
<script src="https://unpkg.com/[email protected]/obniz.js" crossorigin="anonymous"></script>
BLEの初期化を追加
BLEを使っているプログラムを書いている場合は、BLEの初期化が必要になりました。
await obniz.ble.initWait()を最初に呼び出してください
変更前
obniz.onconnect = async function () {
obniz.ble.scan.start();
.
.
.
}
変更後
obniz.onconnect = async function () {
await obniz.ble.initWait();
obniz.ble.scan.start();
.
.
.
}
BLE のCCCD設定の削除
obniz 2.x.xではBLEのnotify機能をするときはCCCD Descriptor(0x2902)を使用する必要がありました。
obniz 3.0.0以降は自動で作成されるようになりました。そのため、Descriptor関連はまるっと削除します。
obnizをPeripheralとして使用する場合
変更前
var characteristic = new obniz.ble.characteristic({
uuid: 'FFF1',
data: [0x0e, 0x00],
properties : ["read","write","notify"], // add notify properties
descriptors: [
{
uuid: '2902', //CCCD
data: [0x00, 0x00], //2byte
},
],
});
var service = new obniz.ble.service({
uuid: 'FFF0',
characteristics: [characteristic],
});
obniz.ble.peripheral.addService(service);
// after central connected
characteristic.notify();
変更後
var characteristic = new obniz.ble.characteristic({
uuid: 'FFF1',
data: [0x0e, 0x00],
properties : ["read","write","notify"], // add notify properties
descriptors: [], // CCCDはpropertiesをみて自動で付与されます
});
var service = new obniz.ble.service({
uuid: 'FFF0',
characteristics: [characteristic],
});
obniz.ble.peripheral.addService(service);
// after central connected
characteristic.notify();
obnizをCentralとして使用する場合
変更前
var target = {
localName: "obniz-notify"
};
var peripheral = await obniz.ble.scan.startOneWait(target);
var connected = await peripheral.connectWait();
if(connected){
let char = peripheral.getService('fff0').getCharacteristic( 'fff1');
let cccd = char.getDescriptor("2902");
let result = await cccd.writeWait([0x01, 0x00]); // register cccd for remote peripheral
console.log(await cccd.readWait()); // check cccd
char.registerNotify( function(data){
console.log("notify with data " + data.join(','));
});
}
変更後
var target = {
localName: "obniz-notify"
};
var peripheral = await obniz.ble.scan.startOneWait(target);
var connected = await peripheral.connectWait();
if(connected){
let char = peripheral.getService('fff0').getCharacteristic( 'fff1');
// delete cccd setting
char.registerNotify( function(data){
console.log("notify with data " + data.join(','));
});
}
BLEのセキュリティ設定の削除
obniz 2.x.xではBLEのセキュリティレベルを設定する関数がありました。obniz 3.0.0以降は自動でセキュリティ対応されるようになりましたので、こちらもまるっと削除します。
変更前
obniz.ble.security.onerror = function() {
console.error('security set params error');
obniz.reboot();
};
security.setModeLevel(1, 2); //LE Security Mode 1, Level 2
変更後
// nothing todo
以上で移行は完了です!
obniz 3.0.Xには1Yで使えるSleep機能なども入っているので、ぜひ使ってみてください。