temp
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
const bleno = require('bleno')
|
||||
const config = require('./config.json')
|
||||
const localEvent = require('./localEvent')
|
||||
|
||||
var tmp = []
|
||||
var idx = 0
|
||||
var failFlag = false
|
||||
|
||||
const notifyDesciptor = new bleno.Descriptor({
|
||||
uuid: '8888',
|
||||
value: 0
|
||||
})
|
||||
|
||||
class DataCharacteristic extends bleno.Characteristic {
|
||||
constructor() {
|
||||
super({
|
||||
uuid: config.uuid.service.func.data,
|
||||
properties: ['write', 'writeWithoutResponse', 'notify'],
|
||||
descriptors: [
|
||||
notifyDesciptor
|
||||
]
|
||||
})
|
||||
this._notifyFunc = null
|
||||
}
|
||||
|
||||
onWriteRequest(data, offset, withoutResponse, callback) {
|
||||
console.log(`offset :: ${offset} , withoutRes :: ${withoutResponse}`)
|
||||
console.log(`get data`, data)
|
||||
|
||||
if (data.length == 1 && data[0] == 0x00) {
|
||||
tmp = []
|
||||
idx = 0
|
||||
failFlag = false
|
||||
} else if (data.length == 1 && data[0] == 0xff) {
|
||||
console.log(Buffer.from(tmp).toString())
|
||||
if (!failFlag) {
|
||||
localEvent.emit('print', Buffer.from(tmp).toString())
|
||||
}
|
||||
} else if (!failFlag) {
|
||||
let pidx = ((data[0] & 0xff) << 8) + (data[1] & 0xff)
|
||||
if (idx !== pidx) {
|
||||
// insert fail method
|
||||
failFlag = true
|
||||
this.sendNotidy('fail')
|
||||
}
|
||||
idx++
|
||||
tmp = [...tmp, ...data.slice(2, data.length)]
|
||||
}
|
||||
|
||||
if (!withoutResponse) {
|
||||
console.log('send response')
|
||||
callback(this.RESULT_SUCCESS)
|
||||
}
|
||||
}
|
||||
|
||||
sendNotidy(str) {
|
||||
if (!this._notifyFunc) return
|
||||
this._notifyFunc(Buffer.from(str))
|
||||
}
|
||||
|
||||
onSubscribe(maxValueSize, cb) {
|
||||
console.log(maxValueSize)
|
||||
this._notifyFunc = cb
|
||||
}
|
||||
|
||||
onUnsubscribe() {
|
||||
this._notifyFunc = null
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DataCharacteristic
|
||||
@@ -0,0 +1,28 @@
|
||||
const bleno = require('bleno')
|
||||
const adapterName = 'BLE_Printer'
|
||||
const serverUUID = config.uuid.main
|
||||
const localEvent = require('./localEvent')
|
||||
const fs = require('fs')
|
||||
const MainService = require('./main-service')
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,24 @@
|
||||
const bleno = require('bleno')
|
||||
const config = require('./config.json')
|
||||
const localEvent = require('./localEvent')
|
||||
|
||||
const GdataCharacteristic = require('./gdata-characteristic')
|
||||
const dataCharacteristic = new GdataCharacteristic()
|
||||
|
||||
class MainService extends bleno.PrimaryService {
|
||||
constructor() {
|
||||
super({
|
||||
uuid: config.uuid.service.id,
|
||||
characteristics: [
|
||||
dataCharacteristic
|
||||
]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
localEvent.on('printResult', async state => {
|
||||
let str = state ? 'ok' : 'fail'
|
||||
dataCharacteristic.sendNotidy(str)
|
||||
})
|
||||
|
||||
module.exports = MainService
|
||||
@@ -0,0 +1,24 @@
|
||||
const bleno = require('bleno')
|
||||
const config = require('./config.json')
|
||||
|
||||
class TimeCharacteristic extends bleno.Characteristic {
|
||||
constructor() {
|
||||
super({
|
||||
uuid: config.uuid.service.func.time,
|
||||
properties: ['read'],
|
||||
descriptors: [
|
||||
new bleno.Descriptor({
|
||||
uuid: '88ff',
|
||||
value: 'Now Time'
|
||||
})
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
onReadRequest(offset, callback) {
|
||||
console.log(`offset :: ${offset}`)
|
||||
callback(this.RESULT_SUCCESS, new Buffer(`${Date.now()}`))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TimeCharacteristic
|
||||
Reference in New Issue
Block a user