【问题标题】:How to get Web Bluetooth to send multiple `characteristicvaluechanged` events?如何让网络蓝牙发送多个“characteristicvaluechanged”事件?
【发布时间】:2017-07-22 05:50:30
【问题描述】:

[解释 IRC 对话]

尝试用 ardunio 和一个 DHT11 传感器和一个 HM10 蓝牙传感器构建一个温度传感器。当使用网络蓝牙获取温度值时,它似乎没有触发characteristicvaluechanged 事件。它只给出初始值。

document.querySelector('button')
    .addEventListener('click', connectBluetooth)


function connectBluetooth() {
    navigator.bluetooth
        .requestDevice({
            optionalServices: [ 0xffe0 ],
            acceptAllDevices: true
        })
        .then(device => device.gatt.connect())
        .then(server => server.getPrimaryService(0xffe0))
        .then(service => service.getCharacteristic(0xffe1))
        .then(characteristic => {
            characteristic.addEventListener('characteristicvaluechanged',
                                            handleValueChanged)

            return characteristic.readValue()
        })
        .catch(err => console.error(err))
}

function handleValueChanged(event) {
    console.log('Handling...')
    let value = event.target.value.getUint8(0)

    console.log(`The value is: ${value}`)
}

【问题讨论】:

    标签: web-bluetooth


    【解决方案1】:

    您从未订阅过通知。有关示例,请参阅https://googlechrome.github.io/samples/web-bluetooth/notifications.html。注册事件监听器是不够的,还必须调用characteristic.startNotifications()readValue 返回一个结果,所以只需将其替换为 startNotifications

    【讨论】:

      猜你喜欢
      • 2012-02-14
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      相关资源
      最近更新 更多