2017-07-21 15 views
0

[IRC会話の言い訳]Web Bluetoothが複数の `characteristicvaluechanged`イベントを送信する方法を教えてください。

ardunioとDHT11センサーとHM10 Bluetoothセンサーによる温度センサーの構築を試みています。 Web Bluetoothで温度値を取得しても、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}`) 
} 

答えて

0

あなたは決して通知を購読しません。例については、https://googlechrome.github.io/samples/web-bluetooth/notifications.htmlを参照してください。イベントリスナーを登録するだけでは不十分な場合は、characteristic.startNotifications()も呼び出す必要があります。 readValueが1つの結果を返していたため、startNotificationsに置き換えてください。

関連する問題