使用网络蓝牙从蓝牙设备读取温度

read tempreture from bluetooth device with web bluetooth

我在使用网络蓝牙时遇到了一些问题API。问题是我有一个温湿度记录仪蓝牙设备,通过传感器记录温湿度数据。我想在 javascript 中使用网络蓝牙在我的网络应用程序中获取该数据。那么我该如何执行此任务?

我试过网络蓝牙,但我只能获取设备名称和电池电量,但无法获取温度和湿度数据。

您需要知道什么蓝牙GATT服务提供温湿度数据。 “nRF Connect”和浏览器内置的 about://bluetooth-internals 页面等应用程序可以帮助您探索设备提供的服务。

Web Bluetooth 要求您将要访问的服务的 UUID 传递给 navigator.bluetooth.requestDevice() 函数,因此您需要先确定要访问的 GATT 服务,然后才能在 [= =11=] 对象由 API.

提供

如果您 post 设备的制造商和型号信息作为问题的一部分,有人可能会指导您从制造商那里获得有关设备提供的 GATT 服务的文档,或者有人已经逆转设计了如何访问您正在寻找的数据。

如果蓝牙设备使用 GATT 环境传感服务(GATT 分配编号 0x181A),您应该能够读取并收到温度(0x2A6E)和湿度(0x2A6F) GATT 特性。

下面是一些应该*有效的代码。

const serviceUuid = 0x181A;

// Requesting Bluetooth Device assuming it advertises
// the GATT Environmental Sensing Service...
const device = await navigator.bluetooth.requestDevice({
    filters: [{services: [serviceUuid]}]});

// Connecting to GATT Server...
const server = await device.gatt.connect();

// Getting Environmental Sensing Service...
const service = await server.getPrimaryService(serviceUuid);

// Getting Humidity Characteristic...
const characteristic = await service.getCharacteristic(0x2A6F);

// Reading Humidity...
const value = await characteristic.readValue();
console.log(value);

https://googlechrome.github.io/samples/web-bluetooth/notifications.html?characteristic=humidity&service=environmental_sensing 中的示例也可以帮助您入门。