eelcocramer / node-bluetooth-serial-port

Serial I/O over bluetooth for NodeJS
Other
495 stars 138 forks source link

Support multiple incoming connections to the same bluetooth channel #206

Open Happy0 opened 5 years ago

Happy0 commented 5 years ago

At the moment, it's possible to listen for 1 connection per channel. E.g:

var server = new(require('bluetooth-serial-port')).BluetoothSerialPortServer();
var server2 = new(require('bluetooth-serial-port')).BluetoothSerialPortServer();

var metadataServiceUUID = "b4721184-46dc-4314-b031-bf52c2b197f3";

server.listen(function (clientAddress) {
    console.log('Client: ' + clientAddress + ' connected!');

    server.write(new Buffer("hello!"});

}, function(error){
    console.error("Something wrong happened!:" + error);
}, {uuid: metadataServiceUUID, channel: 2} );

server.on('data', function(hm) {
    console.log("server1");
    console.log(hm);
})

server2.listen(function (clientAddress) {
    console.log("woo connection on another channel")
}, function(error){
    console.error("Something wrong happened!:" + error);
}, {uuid: "b0b2e90d-0cda-4bb0-8e4b-fb165cd17d48", channel: 3} );

server2.on('data', function(hm) {
    console.log("server2");
    console.log(hm);
})

However, I'd like for it to be possible to accept multiple incoming connections on the same channel and service UUID.

The use case is to support syncing up using the scuttlebutt protocol (https://www.scuttlebutt.nz) over bluetooth. This is something we recently achieved in the android app ( https://www.manyver.se/blog/bluetooth-sync / https://twitter.com/andrestaltz/status/1100783356072116224 ) and I'd like to bring the same functionality to the desktop applications too.

Is this something that youse would like to see in this library? Do any of the main maintainers have the time / motivation to work on it?

If nobody has the time or motivation (understandable - i know what it's like to be a maintainer in open source projects :)), I'm certainly up for trying to add it when I have time (although obviously it'll take me much more time and effort because I'm not super comfortable with C++ and writing node bindings for it.)

If I was to implement it, I'd change the listen callback function to include a client object which is an EventEmitter with events such as datajust like the current BluetoothSerialPort and BluetoothSerialPortServer objects.

server2.listen(function (clientAddress, client) {

    client.on('data', function(data) {
        ...
    })

    client.write(someBuffer...)

}, function(error){
    console.error("Something wrong happened!:" + error);
}, {uuid: "b0b2e90d-0cda-4bb0-8e4b-fb165cd17d48", channel: 3} );
})
eelcocramer commented 5 years ago

Hi, thanks for reaching out. I've heard of scuttlebot and think it is pretty cool so helping you out would be awesome. That said, I've done no work at all on the server implementation of the library. Most of this is the great work of @atilag so you could probably best ask him.

I may be able to point you in the right direction if you want to implement this yourself. You will find that most of the code and node bindings are not that complicated. Also most of the stuff is written in C wrapped in C++ method definitions, so no fancy C++ in here.

Please note that the server side implementation is only available for Linux where as the client is available on both Linux, Windows and macOS.