2017-08-09 10:15:07 +00:00
|
|
|
const bleno = require('bleno')
|
2017-08-10 12:18:43 +00:00
|
|
|
const config = require('./config.json')
|
2017-08-09 10:15:07 +00:00
|
|
|
const adapterName = 'BLE_Printer'
|
2017-08-10 12:18:43 +00:00
|
|
|
const serverUUID = config.uuid.main
|
2017-08-23 06:51:42 +00:00
|
|
|
const localEvent = require('./localEvent')
|
|
|
|
const fs = require('fs')
|
2017-08-09 10:15:07 +00:00
|
|
|
const MainService = require('./main-service')
|
|
|
|
|
2017-08-23 06:51:42 +00:00
|
|
|
const printer = require('./PrinterDev')
|
|
|
|
|
2017-08-09 10:15:07 +00:00
|
|
|
bleno.on('stateChange', state => {
|
|
|
|
console.log(`bt device state ${state}`)
|
|
|
|
if (state == 'poweredOn') {
|
|
|
|
|
|
|
|
bleno.startAdvertising(adapterName, [serverUUID], (error) => {
|
|
|
|
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
bleno.stopAdvertising()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
bleno.on('advertisingStart', function (error) {
|
|
|
|
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
bleno.setServices([new MainService()], error => {
|
|
|
|
console.log('set service', error)
|
|
|
|
})
|
|
|
|
}
|
2017-08-10 12:18:43 +00:00
|
|
|
})
|
2017-08-23 06:51:42 +00:00
|
|
|
|
|
|
|
localEvent.on('print', async (str) => {
|
|
|
|
let status = false
|
|
|
|
if (printer.isOpen) {
|
|
|
|
try {
|
|
|
|
status = await printer.printerString(str)
|
|
|
|
} catch (err) {
|
|
|
|
status = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
localEvent.emit('printResult', status)
|
|
|
|
})
|