将激光测距仪(博世 C,蓝牙)与 Chrome 连接

问题描述 投票:0回答:1

有没有办法将激光测距仪与Chrome蓝牙API连接。 GLM 系列等博世设备具有 andriod/ios SDK。我对此有点陌生,想知道是否有办法做到这一点,或者也许其他公司为其测量设备提供 javascript SDK

google-chrome bluetooth measurement
1个回答
0
投票

我知道这是一个老问题,但这是我在研究同一问题时最终遇到的问题,所以我想我会为所有仍然在这里没有答案的人回答这个问题。

此存储库中有一个完整的工作示例,以及现场演示该演示至少可以在 Chrome 和 Edge 中运行。

以下是演示项目中的一些有趣的内容:

要连接设备,请使用 navigator.bluetooth 并添加与您的设备匹配的过滤器。

const glmServiceUuid = "00005301-0000-0041-5253-534f46540000";
const characteristicUuid = "00004301-0000-0041-5253-534f46540000";

// filters used to filter out devices that are not Bosch PLR devices
const filters = [
  { namePrefix: "Bosch PLR" },
  { services: [glmServiceUuid] },
];

navigator.bluetooth
  .requestDevice({
    filters,
    optionalServices: [characteristicUuid],
  })

连接到设备GATT服务器并获取主要服务和特征

.then((device) => {
  console.log("Device connected: " + device.name);
  setDeviceInfo(device);
  return device.gatt.connect();
})
.then((server) => {
  bluetoothGattServer = server;
  console.log("Connected to GATT server");
  setInfoGattConnected(server.connected);

  // get primary service and then the characteristic
  return bluetoothGattServer
    .getPrimaryService(glmServiceUuid)
    .then((s) => {
      service = s;
      return s.getCharacteristic(characteristicUuid);
    });
})

启动通知(指示)并发送用于在设备上启用自动同步的代码,并设置事件侦听器以在设备发送数据时运行。

.then((c) => {
  console.log("Characteristic found");
  characteristic = c;

  // we need to enable indications to allow the device to send data without being polled
  // startNotifications() enables indications as well as notifications
  return c.startNotifications();
})
.then(() => {
  // "AutoSyncEnable" command, sets the device to send data when a measurement is
  // triggered on the device
  const data = new Uint8Array([0xc0, 0x55, 0x02, 0x01, 0x00, 0x1a]);
  return characteristic.writeValue(data);
})
.then(() => {
  // add listner for events (indications) triggered by device
  characteristic.addEventListener(
    "characteristicvaluechanged",
    indicationEvent
  );

然后您就可以根据Bosch GLM/PLR 蓝牙应用程序套件中的文档解析数据,演示代码存储库中也有一个示例。演示中的实际测量值是这样提取的:

// event.target.value is a DataView containing the characteristic's new value
let message = event.target.value;

// Create an ArrayBuffer with enough space to hold the measurement bytes
// Create a DataView from the ArrayBuffer
let measurementDataView = new DataView(new ArrayBuffer(4));

// Loop over the measurement bytes in the DataView
for (let i = 7; i < 11; i++) {
  // Get the byte as an unsigned 8-bit integer
  let byte = message.getUint8(i);
  measurementDataView.setUint8(i - 7, byte);
}
let measurement = measurementDataView.getFloat32(0, true);
© www.soinside.com 2019 - 2024. All rights reserved.