From 1041190113978d7e9abe3ab2b4f78e54928012f2 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 16 Aug 2017 22:03:35 +0800 Subject: [PATCH] add notify test --- gdata-characteristic.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/gdata-characteristic.js b/gdata-characteristic.js index 1a80861..ce4c359 100644 --- a/gdata-characteristic.js +++ b/gdata-characteristic.js @@ -11,18 +11,21 @@ device.open(()=>{ var tmp = [] +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'], + properties: ['write', 'writeWithoutResponse', 'notify'], descriptors: [ - new bleno.Descriptor({ - uuid: '8888', - value: 'Now' - }) + notifyDesciptor ] }) + this._notifyFunc = null } onWriteRequest(data, offset, withoutResponse, callback) { @@ -36,6 +39,9 @@ class DataCharacteristic extends bleno.Characteristic { //console.log(tmp) console.log(Buffer.from(tmp).toString()) printString(Buffer.from(tmp).toString()) + if(this._notifyFunc) { + this._notifyFunc(Buffer.from([0x00,0x01])) + } } else { tmp = [...tmp, ...data.slice(2, data.length)] } @@ -44,6 +50,15 @@ class DataCharacteristic extends bleno.Characteristic { callback(this.RESULT_SUCCESS) } } + + onSubscribe(maxValueSize, cb) { + console.log(maxValueSize) + this._notifyFunc = cb + } + + onUnsubscribe () { + this._notifyFunc = null + } }