node-bluetooth-serial-port icon indicating copy to clipboard operation
node-bluetooth-serial-port copied to clipboard

Connection closes automatically

Open iamsush opened this issue 6 years ago • 7 comments

If the connection to a bluetooth device is opened again after closing it once. node-bluetooth-serial-port is not able to read the data and it closes the connection automatically.

Getting below error:

Bluetooth disconnected
Error: Error reading from connection

Bluetooth disconnected

iamsush avatar Mar 29 '19 09:03 iamsush

You should not reuse the connection object but re-create a new one when starting a new connection. Also see #90.

eelcocramer avatar Mar 29 '19 11:03 eelcocramer

By that you mean, for every connection you need to have a new connection object, i.e,

var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();

? But isn't this something troublesome, to create the object again and again? Might be possible some enhancements could be there. What do you say?

iamsush avatar Mar 29 '19 11:03 iamsush

It is troublesome :-) I'm happy to consider a PR that fixes this.

eelcocramer avatar Mar 29 '19 11:03 eelcocramer

I could do that, but I am not sure where to start with the code as I have little experience with native code development. On the top layer, I would like to contribute.

iamsush avatar Mar 29 '19 11:03 iamsush

This is the thing. It is a rather complex issue. Re-using the object was never a use case for me personally and other folks seemed to be happy enough with work-around.

eelcocramer avatar Mar 29 '19 11:03 eelcocramer

Actually, the application I am working on has a use case to switch between a serial port and a bluetooth port. So, for me I want to connect and disconnect bluetooth port multiple times. If there had been only one connection object to handle bluetooth port connection, it would have been way better than now for me.

Also, I guess, giving the functionality to connect and reconnect through the same connection object would be useful for a lot of people. (They would not have to worry about deleting the previous object and other memory leak issues.)

bluetooth-serial-port could come out as the universal bluetooth module to use, if working with node js and bluetooth.

(PS: I would still see If I can contribute to the branch, If I got to know how to deal with it. haha)

Thanks man!

iamsush avatar Mar 29 '19 11:03 iamsush

#90 and #162 may provide you with some pointers about the what the problem may be. Reading some of the comments I believe on Windows it is possible to re-use the object so I guess the Linux code is the best place to look for your issue. The implementation can be found in the file BluetoothSerialPort.cc.

eelcocramer avatar Mar 29 '19 19:03 eelcocramer