jasongin / noble-uwp

Noble (Node.js Bluetooth LE) with Windows 10 UWP bindings
MIT License
83 stars 45 forks source link

subscribe does not send notifications #12

Closed vohtaski closed 7 years ago

vohtaski commented 7 years ago

Thank your for this module!

Everything works for me on windows, I can discover and connect the device and send commands to the device. However, when I try to subscribe for notification characteristic, I don't get any data.

This code works with noble on Mac, but the callback in on('data') is not called on Windows. Any hints what I might be doing wrong? Thank you in advance!

let readChar = device.characteristics[readCharacteristicUUID];
readChar.on('data', (data, isNotification) => {
  console.log('data');
  console.log(data);
});
readChar.subscribe((err) => {
  if (err) console.log(err);
  console.log('Notification started');
});
vohtaski commented 7 years ago

@jasongin do notifications work for you on 15063? After debugging, I see the listener below is never called on my windows machine:

listener = ((source, e) => {
    debug('notification: %s %s %s', deviceUuid, serviceUuid, characteristicUuid);
    let data = rt.toBuffer(e.characteristicValue);
    this.emit('read', deviceUuid, serviceUuid, characteristicUuid, data, true);
}).bind(this);

characteristic.addListener('valueChanged', listener);
this._listenerMap[listenerKey] = listener;
rt.keepAlive(true);
this.emit('notify', deviceUuid, serviceUuid, characteristicUuid, notify);

https://github.com/jasongin/noble-uwp/blob/develop/lib/bindings.js#L296

jasongin commented 7 years ago

@vohtaski I don't see anything obviously wrong there. Notifications were working for me on 15063. I've only tested with a TI SensorTag, and not a wide variety of BT devices.

vohtaski commented 7 years ago

Strange. Is it possible to see the config for SensorTag? My device sends many notifications for testing, maybe windows is blocking it ...

farfromrefug commented 7 years ago

@jasongin I am now facing the same issue. I also debugged the code and the listener is never called. How does characteristic.addListener('valueChanged', listener); works? How actually send the valueChanged event?

Edit: i saw how it worked through to doc. In my case i should mention that i am developping a uart device. noble-uwp correctly connects to the device and also successfuly notifies my device which receives the message and tries to send one back.

Looking here it might be related to the characteristic object being released?

farfromrefug commented 7 years ago

ACctually it seems to be a bug in 15063 with console apps https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/bluetooth-bluetoothledevicefromidasync-does-not-complete-on-10015063?forum=wdk#ef927009-676c-47bb-8201-8a80d2323a7f

There is a way to resolve it by (comment by Ilis on May 9th) but i don't see how to do this with NodeRT :s

vohtaski commented 7 years ago

@farfromrefug I very dirtily patched the generated _nodert_generated.cpp file with instructions in your link and it's working for me now.

Still, I guest the best is to wait for proper windows fix... Will send a link with patched file soon

vohtaski commented 7 years ago

Here is what works for me: https://github.com/vohtaski/noble-uwp/commit/108df4471e3eaedb0a8e4a785f2ca867a9c195fb

farfromrefug commented 7 years ago

@vohtaski great to know it s working. However i don't think it s a good idea to path the generated nodeRT file, or we will need to patch it everytime we update the NodeRT files... Could be run that code in the binding.js using NodeRT?

vohtaski commented 7 years ago

@farfromrefug there is anyways one nodeRT file that is already patched :) Anyways, I guess it's a temporary fix, hopefully before the windows bug is fixed

jasongin commented 7 years ago

Hmm, maybe when I saw it working was using the 15063 SDK but a different build of Windows. I'm glad you guys were able to figure it out!

Yes, I already had to patch the generated code in one place to workaround a NodeRT bug. It hasn't been too much of a problem so far. Whenever I regenerate the code, I check the diff and manually revert the patched section of code, as well as some .gyp file changes.

farfromrefug commented 7 years ago

@vohtaski @jasongin it works perfectly. I already made my first release with it! THANKS

jasongin commented 7 years ago

I merged @vohtaski's fix here: https://github.com/jasongin/noble-uwp/commit/0b30cd02b40ce1ce67c9b6a40fc30645587f25a1