【问题标题】:read tempreture from bluetooth device with web bluetooth使用网络蓝牙从蓝牙设备读取温度
【发布时间】:2022-01-19 15:32:46
【问题描述】:

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

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

【问题讨论】:

    标签: javascript api bluetooth progressive-web-apps web-bluetooth


    【解决方案1】:

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

    这里有一些应该*工作的代码。

    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 的示例也可以帮助您入门。

    【讨论】:

      【解决方案2】:

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

      Web 蓝牙要求您将要访问的服务的 UUID 传递给 navigator.bluetooth.requestDevice() 函数,因此您需要确定要访问的 GATT 服务,然后才能在提供的 BluetoothRemoteGATTServer 对象上看到它们通过 API。

      如果您将设备的制造商和型号信息作为问题的一部分发布,那么某人可能会指导您查看制造商提供的有关设备提供的 GATT 服务的文档,或者已经对如何访问数据进行了逆向工程的人你正在寻找。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多