Created
August 25, 2019 14:29
-
-
Save keidaroo/39bf502401170ccb1dbfd7ef45dd8c4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const bleno = require('bleno'); | |
const BlenoPrimaryService = bleno.PrimaryService; | |
const SERVECE_UUID = `D5875408-FA51-4763-A75D-7D33CECEBC31`; //適宜書き換え | |
const CHARACTERISTIC_UUID = `A4F01D8C-A037-43B6-9050-1876A8C23584`; //適宜書き換え | |
const DEVICE_NAME = `n0bisuke-BLE-device`; //デバイス名 | |
console.log(`bleno - ${DEVICE_NAME}`); | |
let counter = 0; | |
setInterval(()=>{counter++}, 1000); //値の変化を見るために毎秒インクリメント | |
const Characteristic = bleno.Characteristic; | |
const characteristic = new Characteristic({ | |
uuid: CHARACTERISTIC_UUID, | |
properties: ['read'], | |
onReadRequest: (offset, callback) => { //Central側からReadリクエストが来ると発火 | |
console.log(counter); //READリクエストでカウントを表示 | |
const result = Characteristic.RESULT_SUCCESS; | |
const data = new Buffer.from(`${counter}`); | |
callback(result, data); | |
} | |
}); | |
bleno.on('stateChange', (state) => { | |
console.log(`on -> stateChange: ${state}`); | |
if (state === 'poweredOn') { | |
bleno.startAdvertising(DEVICE_NAME, [SERVECE_UUID]); | |
} else { | |
bleno.stopAdvertising(); | |
} | |
}); | |
bleno.on('advertisingStart', (error) => { | |
console.log(`on -> advertisingStart: ${(error ? 'error ' + error : 'success')}`); | |
if(error) return; | |
const blenoService = new BlenoPrimaryService({ | |
uuid: SERVECE_UUID, | |
characteristics: [characteristic] | |
}); | |
bleno.setServices([blenoService]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment