stoprocent / noble

A Node.js BLE (Bluetooth Low Energy) central module
MIT License
11 stars 1 forks source link

Disconnect after 60 seconds #9

Closed marcelkottmann closed 1 month ago

marcelkottmann commented 1 month ago

As already commented in https://github.com/stoprocent/noble/issues/4 I experience a disconnect event 60 seconds after establishing a connection in the new implementation.

I debugged this problem and found a bug in node-bluetooth-hci-socket:

The L2_CONNECT_TIMEOUT in node-bluetooth-hci-socket/include/BluetoothStructs.h:36 must always be lower than the timeout in node-bluetooth-hci-socket/lib/native.js:20. Both are currently set to 60 seconds.

Fix: Reducing L2_CONNECT_TIMEOUT to 59 seconds fixes the problem.

Observations: Reducing both values to 30 secs lead to a disconnect after 30 seconds. Then reducing L2_CONNECT_TIMEOUT to 29 seconds then again fixes the problem.

stoprocent commented 1 month ago

I'm not sure yet if It makes sense because after a successful connection we setExpire(0) here https://github.com/stoprocent/node-bluetooth-hci-socket/blob/67869a78b018b31eea6985ea608300e5ec18a4e0/src/BluetoothHciSocket.cpp#L162

... and every 60 seconds from the native.js call Cleanup is called which then checks the expiration time of 60 seconds. After l2socket_ptr->setExpires(0); when cleanup checks if (now < it->second->getExpires()) then now will never be less. Before the change you propose, I need to debug myself because I might have made a mistake somewhere else.

stoprocent commented 1 month ago

I just tried that and I was not disconnected. I stayed in the connection for 5 min and no issues there. Can you share some more code you have been using

stoprocent commented 1 month ago

This should fix the issue. The problem wasn't with the time itself but with how connections were queued. It surfaced after I migrated the code to more modern APIs. Let me know if it works on your end. Eventually, I was able to reproduce and find the issue and fix it.

stoprocent commented 1 month ago

@marcelkottmann can you confirm ?

marcelkottmann commented 1 month ago

Hi @stoprocent , I can confirm that the disconnects disappear after upgrading to the new version. Thank you for maintaining this package!