Adding an eventListener and then calling startNotification()

374 views
Skip to first unread message

kol...@kolban.com

unread,
Jun 18, 2017, 11:35:52 PM6/18/17
to web-bluetooth
I seem to find that if I have a characteristic that supports notifications ... if I call:

characteristic.addEventListener("characteristicvaluechanged", handlerFunc);
characteristic.startNotifications();

Then I do not receive callbacks.  However if I change the logic to:

characteristic.startNotifications().then(function(newCharacteristic) {
   newCharacteristic.addEventListener("characteristicvaluechanged", handlerFunc);
});

The I do start to receive notification callbacks.

I think I am confused by what appears to be two distinct characteristic objects.  The one that I invoke "startNotifications" upon and the one returned in the promise returned by the call to "startNotifications".

Can anyone help me understand the story here?

Neil

Giovanni Ortuño

unread,
Jun 18, 2017, 11:38:43 PM6/18/17
to kol...@kolban.com, web-bluetooth
Hi Neil,

Hmm this could be a bug in the API. What version of Chrome are you using?

Gio

--
You received this message because you are subscribed to the Google Groups "web-bluetooth" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web-bluetoot...@chromium.org.
To post to this group, send email to web-bl...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/web-bluetooth/13a25afd-e8b9-4212-92e4-47313c2d9336%40chromium.org.

Neil Kolban

unread,
Jun 18, 2017, 11:52:53 PM6/18/17
to web-bluetooth, kol...@kolban.com
Howdy,
Sorry, I should have posted that in the original post.

I am using 59.0.3071.104 (Official Build) (64-bit).

I am running my tests on Linux.

Giovanni Ortuño

unread,
Jun 19, 2017, 12:26:25 AM6/19/17
to Neil Kolban, web-bluetooth
Hi Neil,

I just tested on Version 59.0.3071.104 (Official Build) (64-bit) Linux with the following code and I was able to get notifications:

navigator.bluetooth.requestDevice({filters: [{services: ['health_thermometer']}]})
  .then(device => {
    return device.gatt.connect();
  })
  .then(server => {
    return server.getPrimaryService('health_thermometer');
  })
  .then(service => {
    return service.getCharacteristic('temperature_measurement');
  })
  .then(characteristic => {
     characteristic.addEventListener('characteristicvaluechanged',
      () => console.log('notification received'));
    characteristic.startNotifications();
  });

The characteristics returned by both functions should be the exact same object so there shouldn't be any differences between attaching the event listener before or after the promise.

If the problem seems to be on Chrome side feel free to open an issue at http://crbug.com/new. Please attach some logs so that we can take a closer look. Instructions here

Gio

--
You received this message because you are subscribed to the Google Groups "web-bluetooth" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web-bluetoot...@chromium.org.
To post to this group, send email to web-bl...@chromium.org.

Neil Kolban

unread,
Jun 19, 2017, 12:57:46 AM6/19/17
to web-bluetooth, kol...@kolban.com, ort...@google.com
Gio,
Thanks for the response.  I switched on diagnostic tracing and tried to recreate my issue and failed to recreate it.  I then went back to my original setup (no diagnostics) and it continued to work as it should.  I am at a loss to explain.  It is possible I had a "glitch" in my Chrome environment.  I had been reloading my HTML/JS but I hadn't been going as far a shutting down chrome.  It is possible (opinion) that I had confused something along the way and the act of shutting down chrome and restarting it resolved what ever was going on.  Either way, all is now working for me.  I'm sorry to have caused you to do some work on my behalf that turned out fruitless.

As a back story, I am an ESP32 specialist.  This is a relatively new MCU device from Espressif that is a 240MHz processor with built in BLE and WiFI, dual cores, 512K of RAM and 4MB of flash.  These are retailing at about the $8 price point.  There is much interest in them at present but the BLE support is still relatively "fresh".  The ESP32 community is hungry to exploit ways in which they can be used and I am undertaking some tests of using them in conjunction with Web Bluetooth.  When done, and all goes well, recipes will be written up and posted to the ESP32 community and You Tube.

So far my experiences with Web Bluetooth have been overwhelmingly positive.  I have all I need running happily between my Chrome on Linux and an an Android phone running the BLE simulator app.  Over the next period, my plan is to write a BLE peripheral application in C/C++ running on the ESP32 and have that be driven (and drive) the corresponding Chrome browser hosted single page app.
Reply all
Reply to author
Forward
0 new messages