1

I try to get the characteristics everytime they change, the problem is, they change but the eventListener doesn't recognize it. So i only get the first value, after connecting to my BLE, but nothing happen after that. Is there something wrong in my code ?

Another Question, is there a way to update the characteristic every, i dont know 5 Seconds for example? And how would you do that, are there any code examples?(Maybe with setInterval() ? )

Thank you !

function test() {
  console.log('Requesting Bluetooth Device...');
  navigator.bluetooth.requestDevice({
    acceptAllDevices: true,
    optionalServices: ['af07ecb8-e525-f189-9043-0f9c532a02c7']
    })                     //c7022a53-9c0f-4390-89f1-25e5b8ec07af
  .then(device => {
    console.log('Gatt Server Verbindung');
    return device.gatt.connect();
  })
  .then(server => {
    console.log('Dose Service...');
    return server.getPrimaryService('af07ecb8-e525-f189-9043-0f9c532a02c7');
  })
  .then(service => {
    console.log('mgyh Characteristic...');
    return service.getCharacteristic('a99e0be6-f705-f59c-f248-230f7d55c3c1');
  })
  .then(characteristic => {
    // Set up event listener for when characteristic value changes.
    characteristic.addEventListener('characteristicvaluechanged',dosechanged);
    
    return characteristic.readValue();
  })
  .catch(error => {
    console.log('Das geht nicht: ' + error);
  });
}

function dosechanged(event) {
  
  let dose = event.target.value.getUint8(0)+event.target.value.getUint8(1)*10+event.target.value.getUint8(2)*100 + event.target.value.getUint8(3)*1000+ event.target.value.getUint8(4)*10000;
  console.log('Received ' + dose);

} 

1 Answers1

0
  1. You missed a characteristic.startNotifications() call to start receive notification. example
  2. setInterval would be fine to call readValue() every 5 seconds.
emptyhua
  • 6,634
  • 10
  • 11
  • Thank you for you answer. I just have a security problem with the `startNotification()` and i didnt know how to solve it. It doesnt allow Notifications, i dont know why, because my BLE allows it. Sorry for that stupid question but how would you add `readValue()`: Like `.then(characteristic => { // Set up event listener for when characteristic value changes. characteristic.addEventListener('characteristicvaluechanged',dosechanged); setInterval(characteristic.readValue(),5000); return characteristic.readValue(); })` ? – javagreenhorn Nov 02 '21 at 14:08
  • Yes, `let tm = setInterval(()=> characteristic.readValue(), 5000);` – emptyhua Nov 02 '21 at 14:10
  • When i do like you told me: `.then(characteristic => characteristic.startNotifications()) .then(characteristic => { characteristic.addEventListener('characteristicvaluechanged', dosechanged); console.log('Notifications have been started.'); })` I get from console: SecurityError: GATT operation not authorized. Do you have any advice for this error too ? :) – javagreenhorn Nov 02 '21 at 15:56
  • https://stackoverflow.com/questions/62356599/web-bluetooth-error-gatt-operation-not-authorized-occurs-on-windows-only – emptyhua Nov 02 '21 at 23:25