蓝牙GATT数据在飞行中被损坏

我正在使用贵族来消费我的外围设备。 我的onReadRequest()callback中返回的数据缓冲区在获得我的高贵的Characteristic.read()时会以某种方式被破坏。 我在我的onReadRequest()callback之前转储缓冲区,当我在Characteristic.read()得到它时,立即转储它:

消费者

 ... Characteristic.read((err, data) => { if (err) { reject(err); } else { console.log(data); } }); ... 

制片人

 ... const bleno = require('bleno'); const flags = { a: 1 << 0, b: 1 << 1, c: 1 << 2 }, numBytesInReturnData = 18, supportedFlags = flags.a | flags.b | flags.c, accelPrecision = Math.pow(10, 5), bPrecision = Math.pow(10, 3); module.exports = class DataCharacteristic extends bleno.Characteristic { constructor(pin) { super({ uuid: '1234', properties: ['read'], descriptors: [new bleno.Descriptor({ uuid: '2901', value: 'blah' })] }); this._hardware = hardware; this._hasData = false; this._dataBuffer = new Buffer.allocUnsafe(numBytesInReturnData).fill(0); } onReadRequest(offset, callback) { if (!offset) { this._hasData = false; this._dataBuffer.fill(0); Promise.all([ this._hardware.getA), this._hardware.getB(), this._hardware.getC(), ]).then((responses) => { this._dataBuffer.writeUInt8(supportedFlags, 0); this._dataBuffer.writeUInt8(responses[0], 1); this._dataBuffer.writeInt32LE(responses[0] * aPrecision, 2, true); this._dataBuffer.writeInt32LE(responses[2].x * accelPrecision, 6, true); //accel this._dataBuffer.writeInt32LE(responses[2].y * accelPrecision, 10, true); this._dataBuffer.writeInt32LE(responses[2].z * accelPrecision, 14, true); console.log(this._dataBuffer); this._hasData = true; callback(this.RESULT_SUCCESS, this._dataBuffer); }) .catch(err => { console.error('Error when reading hardware data', err); callback(this.RESULT_UNLIKELY_ERROR); }); } else if (this._hasData) { //reading a second time for rest of data callback(this.RESULT_SUCCESS, this._dataBuffer.slice(offset)); } else { callback(this.RESULT_ATTR_NOT_LONG); } } } 

制片人outupt

<Buffer 07 00 00 00 00 00 c2 c9 ff ff e4 2c 00 00 1c 47 0f 00>

消费者超越

<Buffer 07 00 00 00 00 00 c2 c9 ff ff 5f 34 00 00 5f 43 0f 00>

正如你所看到的,只有部分缓冲区被破坏。 为什么会发生?

外设通知或指示后可能会立即读取? 如果是这样,那么传递给你的callback数据实际上可能是来自上次通知或指示的数据。 在这里看到这个问题。