ble-server/ble/index.js

25 lines
658 B
JavaScript
Raw Normal View History

2017-09-05 10:36:35 +00:00
const config = require('../config.json')
2017-09-04 10:34:44 +00:00
const bleno = require('bleno')
const adapterName = 'BLE_Printer'
const serverUUID = config.ble.uuid.main
2017-09-04 10:34:44 +00:00
const MainService = require('./main-service')
bleno.on('stateChange', state => {
console.log(`bt device state ${state}`)
2017-09-05 10:36:35 +00:00
if (state === 'poweredOn') {
bleno.startAdvertising(adapterName, [serverUUID])
2017-09-04 10:34:44 +00:00
} else {
bleno.stopAdvertising()
}
})
bleno.on('advertisingStart', function (error) {
2017-09-05 10:36:35 +00:00
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'))
2017-09-04 10:34:44 +00:00
if (!error) {
bleno.setServices([new MainService()], error => {
console.log('set service', error)
})
}
})