Coming Soon!

#04 ペリフェラルへのアクセス

この動画について

ペリフェラルへのアクセスするコード

'use strict';

const noble = require('noble');
const knownDevices = [];

const peripheralUUID = '7c67048222df46849621b62620924821';

//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(peripheral.uuid !== peripheralUUID) return;
    noble.stopScanning();
    console.log(`device found.`);
    console.log(peripheral.advertisement.serviceUuids)
    peripheral.connect(err => {
        if(err) console.log(err)
        console.log(`connect device. coneccting service & characteristics`);        
    });
}

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

実行

node app.js
最終更新日

コメント