-
Notifications
You must be signed in to change notification settings - Fork 864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Peripheral disconnects the device in connection #111
Comments
@vsnikkil, are you explicitly connecting to the peripheral before attempting discoverServices? noble.on('discover', function(p) {
p.connect(function(err) {
p.discoverSerivces(null, function(err, service) {
// gets stuck here
});
});
}); |
@tompropst thanks! @vsnikkil there is also a p.connect(function(err) {
// ...
p.once('disconnect', function() {
// do something
});
}); |
@sandeepmistry thank you, that works. What should I put inside the disconnect event function to stop execution of discoverServices() function? |
@vsnikkil, |
@tompropst in my case the discoverService doesn't call the callback function but is stuck somewhere inside the function |
@vsnikkil, are you checking the error status on the connect call? I'd be happy to try and help if you want to share your code. |
@tompropst, thanks! yes, I check the errors in p.connect: p.connect(err) {
if(err) console.log(err); // No errors ever occurred here
p.discoverServices(null, function(err, services) {
// Does not execute
// Gets stuck inside the discoverServices before callback is called
});
} The used BLE peripheral was faulty (physical problems) and it disconnected noble quite often. p.once('disconnect', ...) resolved my problem but I'm not sure if the program will consume memory because I guess node is still executing discoverServices function |
@vsnikkil, Try: p.connect(function(err) {
if(err) console.log(err);
else p.discoverServices(null, function(err, services) {
console.log("Discover complete");
});
}); |
@tompropst, thanks! You see, the device is disconnected during the discovery procedure and noble gets stuck inside the discoverService function and it doesn't call the callback function. This is the problem. This can be bypassed with disconnect event as @sandeepmistry suggested. |
@vsnikkil, I'm not an expert in the BLE protocol but I suspect that the MTU request just before the disconnection is part of the connection flow and not part of the service discovery. Do you know? MTU stands for "Maximum Transmission Units" which needs to be established between the two devices. Have you confirmed that the discoverServices call is being made? Or perhaps is the connection not established and hence the discoverServices isn't being called? If you're sure that it is being called, I'm afraid I don't know how to diagnose the underlying l2cap-ble module. |
@tompropst thanks again, exchanging Rx MTUs isn't very important here. Devices just exchange MTU sizes (my device has 23) and minimum will be used of these (23). The log just shows that there is a disconnection without any apparent reason and it got noble stuck. Yes, I have made very sure discoverServices is being called. |
@vsnikkil MTU exchange is very important, it looks like you device doesn't like it at all and disconnects. What are you trying to connect to? noble emits the Also, there is not much error handling in noble currently, so the Btw, did you get Wireshark working with BLE on Linux? |
@sandeepmistry, thanks! Yes I got WireShark work on linux. Just grabbed the newest version of it from git repository (BLE is quite new addition). I remember it had some dependencies that wasn't 100% straight forward to install. The sensor had some internal radio problems in it, which caused it to disconnect noble. Sometimes it worked perfectly. That log is just an example. Exchanging MTUs works sometimes too, the device will response with packet size of 23. Sometimes service discovery works but reading the characteristic won't and it gets stuck there. Thanks! |
@vsnikkil interesting, is it more reliable closer to your PC (or whatever your are using noble on)? Is there anything else I can help out on for this issue? |
@sandeepmistry, thanks again The device function was totally random. The device has been fixed and now it works very well. I am still curious if the discoverService function call eats memory when I use the disconnect event to prevent noble getting stuck. How could I stop the discoverServices function? |
@vsnikkil one way to fix this, is with the following code change in peripheral.js: Peripheral.prototype.discoverServices = function(uuids, callback) {
var servicesDiscoverCallback = function(services) {
this.removeListener('disconnect', disconnectCallback);
callback(null, services);
};
var disconnectCallback = function() {
this.removeListener('servicesDiscover', servicesDiscoverCallback);
callback(new Error('peripheral disconnected'));
}.bind(this);
if (callback) {
this.once('servicesDiscover', servicesDiscoverCallback);
this.once('disconnect', disconnectCallback);
}
this._noble.discoverServices(this.uuid, uuids);
}; This would have to be done for methods in peripheral.js, service.js, characteristic.js, and descriptor.js - as the peripheral could disconnect during any method calls. A bit messy ... I'll try to think of a better way. |
@sandeepmistry Did you find a better way..This is really an issue..I have a faulty device, which behaves randomly... |
I haven't looked at this for a while. It might be better if the |
I have that problem too. Sometimes it helps to unplug the Dongle and replug it again. Sometimes two exactly the same Dongle work differently: one works, one doesn't. I found out that the disconnect happen after connection but before mtu exchange. Unfortunately neither hcidump nor your hci-bluetooth-socket debug log helps me to investigate further. I saw somebody mentioning that authorization might be a problem. Could that be? Here's the interesting part:
Here's how a working dump looks:
Here is a hciconfig dump of two hci dongles - 1 works 1 doesn't.. as you can see they are similar.
|
Is there maybe a workaround such as a hardreset of bluez or something similar? I tried resetting the hci device, but that didn't work. |
I want to use noble in a prototype embedded device, and sometimes I got this issue. Restarting the ble or the device is something I want to avoid. Did someone manage to fix it. I saw the code snipped above but that is only for the peripheral. |
Please test: Relate-to: |
Relate-to: #923 (comment) |
It looks the maintenance of this project is in pause, but feel free to contribute to @abandonware fork until upstream is active again Feel free to comment: |
Hello,
I am connecting to a peripheral but it disconnects the noble in the connection (GATT server sends the disconnection I think). Noble doesn't notice this and gets stuck:
How to prevent noble from getting stuck?
Thanks, Vesa
The text was updated successfully, but these errors were encountered: