javascript - 如何使用网络蓝牙解析 "Indoor Bike Data"

标签 javascript typescript bluetooth-lowenergy web-bluetooth

我能够使用“nRF Connect for Mobile”获取有效的“室内自行车数据”值。

  • 瞬时速度(公里/小时)
  • 瞬时踏频(/分钟)
  • 阻力水平(无单位)
  • 瞬时功率(W)

问题是当我尝试使用网络蓝牙获取“室内自行车数据”值时,我以DataView格式获取数据,我不确定如何解析该数据可理解的值(value)观。

我阅读了其他一些堆栈溢出答案并进行了一些随机猜测,并能够使用下面的代码获得“阻力级别”

dataView.getInt16(6, true)

不知道为什么使用 6 和 true 能够获得“阻力水平”

我尝试了随机数字,但无法获得有效的查找数字

  1. 瞬时速度(公里/小时)
  2. 瞬时踏频(/分钟)
  3. 瞬时功率(W)

我可以通过解析从室内自行车 BLE 设备获得的 dataView 输入来获取解析上述三个数字的帮助吗?

谢谢!

下面是我如何从室内自行车 BLE 设备获取 dataView 的代码。

const FITNESS_MACHINE_SERVICE_UUID = "00001826-0000-1000-8000-00805f9b34fb";
const INDOOR_BIKE_DATA_UUID = "00002ad2-0000-1000-8000-00805f9b34fb";

const handleClick = async () => {
  const indoorBikeDevice = await navigator.bluetooth.requestDevice({
    filters: [{ name: "MG03" }],
    optionalServices: [FITNESS_MACHINE_SERVICE_UUID],
  });

  if (!indoorBikeDevice.gatt) return;

  const server = await indoorBikeDevice.gatt.connect();
  const service = await server.getPrimaryService(FITNESS_MACHINE_SERVICE_UUID);
  const characteristic = await service.getCharacteristic(INDOOR_BIKE_DATA_UUID);

  characteristic.addEventListener(
    "characteristicvaluechanged",
    async (event) => {
      const dataView = (event.target as any).value as DataView;
      console.log("dataView: ", dataView);

      const resistanceLevel = dataView.getInt16(6, true);
      console.log("resistanceLevel: ", resistanceLevel);
    }
  );

  characteristic.startNotifications();
};


以下是查看 @Michael Kotzjan 的回复后的结果

我查看了 @Michael Kotzjan 提供的链接,经过几次尝试,我能够通过运行下面的代码来获取标志

// GATT_Specification_Supplement_v8.pdf
// 3.124.1 Flags field: The bits of this field are defined below.
for (let i = 0; i < 16; i++) {
  console.log("flags[" + i + "] = " + !!((flags >>> i) & 1));
}

console.log 如下所示:

// flags[0] = false
// flags[1] = false
// flags[2] = true (Instantaneous Cadence present)
// flags[3] = false
// flags[4] = false
// flags[5] = true (Resistance Level present)
// flags[6] = true (Instantaneous Power present)
// flags[7] = false
// flags[8] = false
// flags[9] = false
// flags[10] = false
// flags[11] = false
// flags[12] = false
// ...

上面的true标志值似乎告诉我存在瞬时踏频存在阻力水平存在瞬时功率 可用。

我的问题是获取这些字段的值并将该值与“nRF Connect for Mobile”中的数据进行匹配。

我在没有任何理解的情况下盲目猜测数字,并能够使用下面的代码将输出数字与“nRF Connect for Mobile”匹配

    characteristic.addEventListener(
      "characteristicvaluechanged",
      async (event) => {
        const dataView = (event.target as any).value as DataView;

        const instantaneousCadence = dataView.getUint16(3, true) / 512;
        const resistanceLevel = dataView.getUint8(6);
        const instantaneousPower = dataView.getInt16(8, true);

        console.log(
          [instantaneousCadence, resistanceLevel, instantaneousPower].join("|")
        );
      }
    );

即使我得到了想要的号码,我仍然想知道为什么它有效?

例如,对于节奏:dataView.getUint16(3, true)/512 为什么字节偏移量为:3 而我需要除以 512?获得转速/分钟?

电阻级别和功率的字节偏移量为 6 和 8,我不确定在哪里以及如何获取字节偏移量?

最佳答案

您可以在 Assigned Numbers Document 中搜索您的服务由蓝牙 SIG 提供。您的 INDOOR_BIKE_DATA_UUID 是一个标准 UUID,其 16 位表示形式为 0x2ad2。分配的编号文档将此 UUID 显示为室内自行车数据,它是 Fitness Machine Service 的一部分。服务规范包含有关室内自行车数据的部分:

The Indoor Bike Data characteristic is used to send training-related data to the Client from an indoor bike (Server). Included in the characteristic value is a Flags field (for showing the presence of optional fields), and depending upon the contents of the Flags field, it may include one or more optional fields as defined on the Bluetooth SIG Assigned Numbers webpage.

这意味着您需要读出标志字段来找出您的自行车上存在哪些数据字段并相应地处理它们。有关数据字段的类型和长度的所有信息都可以在文档中找到。

关于javascript - 如何使用网络蓝牙解析 "Indoor Bike Data",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75937738/

相关文章:

javascript - 如何使用 webpack 访问 Typescript 文件中的 JS 全局变量

Android BLE 命令发送不工作

ios - iOS 6 上的低功耗蓝牙; CBPeripheralManagerDelegate

javascript - TypeError : r. getClientRects 不是函数

javascript - 如何在 UWP 安装的应用程序中添加一些源代码?

javascript - 点击链接时会发生什么

javascript - 类型错误 : Attempted to wrap getRepository which is already wrapped

angular - 如何将可观察值传递给@Input() Angular 4

typescript - Webpack + React + typescript

ios - BLE Bond 表 iOS