urish / web-bluetooth-polyfill

Windows 10 Web Bluetooth Polyfill
76 stars 15 forks source link

readValue does not fire a `characteristicvaluechanged` event #33

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hi, I'm trying to use this polyfill for the first time. Nice work btw!

writeValue is working fine but readValue is not. I have a protocol analyzer and can see that the expected attribute protocol messages are exchanged with my peripheral and the expected characteristic value is being returned. But I don't get a callback to the registered function.

The same web page works fine from Chrome on Android and on Mac OS.

During service discovery I do this:

if (characteristic.uuid == MODEL_NUMBER_STRING) {
    model_number_string = characteristic;
    has_model_name_string = true;
    model_number_string.addEventListener('characteristicvaluechanged',
        onModelNumberStringChanged);
}

The callback function looks like this:

function onModelNumberStringChanged(event) {
    console.log("onModelNumberStringChanged");
    var value = event.target.value;
    var data = new Uint8Array(value.buffer);
    model_number_string = new TextDecoder("utf-8").decode(data);
    console.log(model_number_string);
    document.getElementById("model_number").innerHTML = model_number_string;
}

And I request the read of the characteristic with:

model_number_string.readValue();

Any ideas?

urish commented 6 years ago

Hi,

Thank you!

For the notifications, did you remember to call the startNotifications() method?

Then, for reading, can you please debug the background page of the extension and show the logs that you get there? Also, check if you get any output on the devtools console of your web page.

ghost commented 6 years ago

Hi Urish, I'm not trying to use notifications. I'm just reading a characteristic value.

The debug logs show that the read result is being received. I'm just not getting a callback to the handler function onModelNumberStringChanged.

Sent native message: {device: "BluetoothLE#BluetoothLE7c:7a:91:db:f3:43-b8:27:eb:c5:e9:31", service: "{0000180a-0000-1000-8000-00805f9b34fb}", characteristic: "{00002a24-0000-1000-8000-00805f9b34fb}", cmd: "read", _id: 130}
background.js:24 Received native message: 

By the way, I'm using notifications with a different characteristic and that is working fine. It's just the use of characteristic.readValue which is not working.

urish commented 6 years ago

I see, currently the event listener is only invoked if you use notifications. I will check what the spec says about this - currently, the way it works with reading would be to wait for the promise to resolve:

model_number_string.readValue().then(value => {
    // do something with value
});
urish commented 6 years ago

I see that according to the spec, readValue() should indeed fire a characteristicvaluechanged event. I will add it

ghost commented 6 years ago

Great, thank you. I'm preparing a series of tutorials for bluetooth.com. I'll mention your polyfill :-)

urish commented 6 years ago

Nice, and nice to meet you by the way! Are you also on Twitter?

ghost commented 6 years ago

Likewise :-)

@bluetooth_mdw

Very interested in seeing Web Bluetooth adopted across all the browsers!

urish commented 6 years ago

awesome!

Then perhaps you can chime in this discussion: https://twitter.com/Espruino/status/923555356835504128

urish commented 6 years ago

Can you check if the fix does the trick for you?

ghost commented 6 years ago

a) Works a treat - thanks for the rapid service!

b) I changed to the resolved promise pattern anyway since this is clearly more in keeping with the normal use of the API.

Marvellous!

urish commented 6 years ago

You are welcome :)

urish commented 6 years ago

not related - do you know if there is some ongoing process for a standard UART BLE service?

ghost commented 6 years ago

I don't think there is. It's a common request though.

urish commented 6 years ago

Okay, thanks!