update status

This commit is contained in:
Jay
2017-09-05 18:36:35 +08:00
parent 2c89f32c8d
commit 1941ab0c52
18 changed files with 603 additions and 67 deletions
+7 -7
View File
@@ -12,7 +12,7 @@ const notifyDesciptor = new bleno.Descriptor({
})
class DataCharacteristic extends bleno.Characteristic {
constructor() {
constructor () {
super({
uuid: config.uuid.service.func.data,
properties: ['write', 'writeWithoutResponse', 'notify'],
@@ -23,15 +23,15 @@ class DataCharacteristic extends bleno.Characteristic {
this._notifyFunc = null
}
onWriteRequest(data, offset, withoutResponse, callback) {
onWriteRequest (data, offset, withoutResponse, callback) {
console.log(`offset :: ${offset} , withoutRes :: ${withoutResponse}`)
console.log(`get data`, data)
if (data.length == 1 && data[0] == 0x00) {
if (data.length === 1 && data[0] === 0x00) {
tmp = []
idx = 0
failFlag = false
} else if (data.length == 1 && data[0] == 0xff) {
} else if (data.length === 1 && data[0] === 0xff) {
console.log(Buffer.from(tmp).toString())
if (!failFlag) {
localEvent.emit('print', Buffer.from(tmp).toString())
@@ -53,17 +53,17 @@ class DataCharacteristic extends bleno.Characteristic {
}
}
sendNotidy(str) {
sendNotidy (str) {
if (!this._notifyFunc) return
this._notifyFunc(Buffer.from(str))
}
onSubscribe(maxValueSize, cb) {
onSubscribe (maxValueSize, cb) {
console.log(maxValueSize)
this._notifyFunc = cb
}
onUnsubscribe() {
onUnsubscribe () {
this._notifyFunc = null
}
}
+5 -8
View File
@@ -1,24 +1,21 @@
const config = require('../config.json')
const bleno = require('bleno')
const adapterName = 'BLE_Printer'
const serverUUID = config.uuid.main
const localEvent = require('./localEvent')
const fs = require('fs')
const localEvent = require('./localEvent') //eslint-disable-line
const MainService = require('./main-service')
bleno.on('stateChange', state => {
console.log(`bt device state ${state}`)
if (state == 'poweredOn') {
bleno.startAdvertising(adapterName, [serverUUID], (error) => {
})
if (state === 'poweredOn') {
bleno.startAdvertising(adapterName, [serverUUID])
} else {
bleno.stopAdvertising()
}
})
bleno.on('advertisingStart', function (error) {
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'))
if (!error) {
bleno.setServices([new MainService()], error => {
+1 -1
View File
@@ -6,7 +6,7 @@ const GdataCharacteristic = require('./gdata-characteristic')
const dataCharacteristic = new GdataCharacteristic()
class MainService extends bleno.PrimaryService {
constructor() {
constructor () {
super({
uuid: config.uuid.service.id,
characteristics: [
-24
View File
@@ -1,24 +0,0 @@
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