Coming Soon!

#06 キャラクタリスティックから値の読み込み

この動画について

実際にデータにアクセスしていきます。

キャラクタリスティックのデータ読み込みコード

'use strict';

const noble = require('noble');
const knownDevices = [];
const peripheralUUID = `7c67048222df46849621b62620924821`;
const serviceUUIDs = ['180d'];
const characteristicUUIDs = ['2a39'];

//discovered BLE device
const discovered = (peripheral) => {
    const device = {
        name: peripheral.advertisement.localName,
        uuid: peripheral.uuid,
        rssi: peripheral.rssi
    };
    knownDevices.push(device);
    console.log(`${knownDevices.length}:${device.name}(${device.uuid}) RSSI${device.rssi}`);

    if(peripheralUUID !== peripheral.uuid) return;
    noble.stopScanning();
    console.log(`device found.`);
    peripheral.connect((err) => {
        if(err) console.log(err);
        console.log(`connect device. coneccting service & characteristics`);        

        peripheral.discoverSomeServicesAndCharacteristics(serviceUUIDs, characteristicUUIDs, (error, services, characteristics) => {
            console.log(`connect characteristics. wait for READ VALUE......`);
            characteristics[0].read((err,data) => {
                console.log(`connect characteristic data.`);
             });
        });
    })
}

//BLE scan start
const scanStart = () => {
    if(noble.state !== 'poweredOn'){
        noble.on('stateChange', scanStart)
    }else{
        noble.startScanning();
        noble.on('discover', discovered); //Discover
    }
}
scanStart();
最終更新日

コメント