bleno如何发布多条BLE特性数据
How do I publish multiple pieces of BLE characteristic data using bleno
我正在尝试学习如何使用 bleno. I would like to discover and read from the peripheral using noble. For example sake, I want to know how I would implement a simple smart scale that reports back weight, BMI etc following the Weight Measurement GATT spec 实现 BLE 外围设备。
我想不通的是,是否可以从一个特征中读取多条信息。 Weight Measurement GATT spec 让它看起来像在一个贵族中 characteristic.read()
你可以同时检索体重、BMI、身高等
例如,这个简单的 bleno 特性:
'use strict';
const bleno = require('bleno');
const flags = {
IMPERIAL_WEIGHT: 1 << 0,
USER_ID_PRESENT: 1 << 2,
BMI_AND_HEIGHT_PRESENT: 1 << 3
};
module.exports.flags = flags;
module.exports.WeightMeasureCharacteristic = class WeightMeasureCharacteristic extends bleno.Characteristic {
constructor(scale) {
super({
uuid: '2A9D',
properties: ['read'],
descriptors: []
});
this._scale = scale;
}
onReadRequest(offset, callback) {
//Not sure what `offset` means here or how it gets populated...Help!
let data = new Buffer.alloc(8); //1(flags)+2(weightImp)+1(userId)+2(BMI)+2(heightImp)
//Write supported value fields as bit flags
data.writeUInt8(flags.IMPERIAL_WEIGHT | flags.USER_ID_PRESENT | flags.BMI_AND_HEIGHT_PRESENT), 0);
//Write out weight (lbs) - offset 1 byte
data.writeUInt16LE(100.01, 1);
//Write out user id - offset 12 bytes (flags+Imperial, no need to include offset for SI or Timestamp since the flags indicated those are not supported)
data.writeUInt8(69, 3);
//Write out BMI - offset 13 bytes (after UserId)
data.writeUInt16LE(18.6, 4);
//Write out Height Imperial - offset 17 bytes (after Height SI)
data.writeUInt16LE(72.2, 6);
callback(this.RESULT_SUCCESS, data);
}
}
如果有人能够 implement/pseudocode onReadRequest()
以上,我认为这会帮助我解决问题。
问题:
- spec 的 "Field Requirement" 列中的
C<number>
值是否表示传递给 onReadRequest()
的 offset
值?如果消费者想要获得 "Weight - SI"(C1
),他们会以某种方式构造一个高贵的 characteristic.read()
来触发 onReadRequest(1,function())
?如果是,characteristic.read()
是如何构造的?
- 如何构造贵族
characteristic.read()
来获得Flags
的价值?
- 我如何构建一个高贵的
characteristic.read()
将 return 我在一次阅读中获得多个(或所有)属性?例如:给我这个外围设备支持的所有值(重量 - SI、BMI 等)。
- 如果我的外围设备支持英制体重、用户 ID、bmi 和身高,我该如何为
onReadRequest()
中的回调填充 data
。我上面说的对吗?
offset
是如何填充的以及它在 onReadRequest(offset,callback)
中是什么意思?
或者,我做错了吗?我应该为每个值指定一个特征吗?例如:体重的单一特征 - SI,以及 BMI 的另一个特征?我想避免这种情况,更愿意保存往返并在一次调用中获取多个值。
尝试回答您的问题:
- 我不确定
C<number>
是什么意思,但我相信每个字段(体重、BMI、身高等)都表示为一组一个或多个八位字节。请注意规范底部如何显示
Note: The fields in the above table are in the order of LSO to MSO.
Where LSO = Least Significant Octet and MSO = Most Significant Octet.
因此,我相信为了获得 "Weight - SI" 字段,您需要执行以下操作:
characteristic.read((err, data) => {
let char_flags = data.readUint8(0); // read first bit
if (!(char_flags & flags.IMPERIAL_WEIGHT)) // if SI weight
let weightSI = data.readUint16LE(1) // read SI weight starting at second bit
});
- 以上回答
- 上面回答了,只要检查flags中是否存在属性,然后从相应的offset中读取值即可。 this 也可能有帮助。
- 上面已经回答了。
我正在尝试学习如何使用 bleno. I would like to discover and read from the peripheral using noble. For example sake, I want to know how I would implement a simple smart scale that reports back weight, BMI etc following the Weight Measurement GATT spec 实现 BLE 外围设备。
我想不通的是,是否可以从一个特征中读取多条信息。 Weight Measurement GATT spec 让它看起来像在一个贵族中 characteristic.read()
你可以同时检索体重、BMI、身高等
例如,这个简单的 bleno 特性:
'use strict';
const bleno = require('bleno');
const flags = {
IMPERIAL_WEIGHT: 1 << 0,
USER_ID_PRESENT: 1 << 2,
BMI_AND_HEIGHT_PRESENT: 1 << 3
};
module.exports.flags = flags;
module.exports.WeightMeasureCharacteristic = class WeightMeasureCharacteristic extends bleno.Characteristic {
constructor(scale) {
super({
uuid: '2A9D',
properties: ['read'],
descriptors: []
});
this._scale = scale;
}
onReadRequest(offset, callback) {
//Not sure what `offset` means here or how it gets populated...Help!
let data = new Buffer.alloc(8); //1(flags)+2(weightImp)+1(userId)+2(BMI)+2(heightImp)
//Write supported value fields as bit flags
data.writeUInt8(flags.IMPERIAL_WEIGHT | flags.USER_ID_PRESENT | flags.BMI_AND_HEIGHT_PRESENT), 0);
//Write out weight (lbs) - offset 1 byte
data.writeUInt16LE(100.01, 1);
//Write out user id - offset 12 bytes (flags+Imperial, no need to include offset for SI or Timestamp since the flags indicated those are not supported)
data.writeUInt8(69, 3);
//Write out BMI - offset 13 bytes (after UserId)
data.writeUInt16LE(18.6, 4);
//Write out Height Imperial - offset 17 bytes (after Height SI)
data.writeUInt16LE(72.2, 6);
callback(this.RESULT_SUCCESS, data);
}
}
如果有人能够 implement/pseudocode onReadRequest()
以上,我认为这会帮助我解决问题。
问题:
- spec 的 "Field Requirement" 列中的
C<number>
值是否表示传递给onReadRequest()
的offset
值?如果消费者想要获得 "Weight - SI"(C1
),他们会以某种方式构造一个高贵的characteristic.read()
来触发onReadRequest(1,function())
?如果是,characteristic.read()
是如何构造的? - 如何构造贵族
characteristic.read()
来获得Flags
的价值? - 我如何构建一个高贵的
characteristic.read()
将 return 我在一次阅读中获得多个(或所有)属性?例如:给我这个外围设备支持的所有值(重量 - SI、BMI 等)。 - 如果我的外围设备支持英制体重、用户 ID、bmi 和身高,我该如何为
onReadRequest()
中的回调填充data
。我上面说的对吗? offset
是如何填充的以及它在onReadRequest(offset,callback)
中是什么意思?
或者,我做错了吗?我应该为每个值指定一个特征吗?例如:体重的单一特征 - SI,以及 BMI 的另一个特征?我想避免这种情况,更愿意保存往返并在一次调用中获取多个值。
尝试回答您的问题:
- 我不确定
C<number>
是什么意思,但我相信每个字段(体重、BMI、身高等)都表示为一组一个或多个八位字节。请注意规范底部如何显示
Note: The fields in the above table are in the order of LSO to MSO. Where LSO = Least Significant Octet and MSO = Most Significant Octet.
因此,我相信为了获得 "Weight - SI" 字段,您需要执行以下操作:
characteristic.read((err, data) => {
let char_flags = data.readUint8(0); // read first bit
if (!(char_flags & flags.IMPERIAL_WEIGHT)) // if SI weight
let weightSI = data.readUint16LE(1) // read SI weight starting at second bit
});
- 以上回答
- 上面回答了,只要检查flags中是否存在属性,然后从相应的offset中读取值即可。 this 也可能有帮助。
- 上面已经回答了。