Skip to content
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

Question: How do I Unsubscribe, disconnect and then connect with a new BTdevice automatically? #17

Closed
manorius opened this issue Oct 6, 2013 · 1 comment

Comments

@manorius
Copy link

manorius commented Oct 6, 2013

I've been breaking my head to make this work and I keep on receiving the "Unable to connect to device" error when it reaches the connect stage of the code.
The only way it can work is if I disconnect and when successful, automatically unsubscribe and then manually invoke the bluetoothSerial.connect.
The code that is not working is bellow:

                function(){
                bluetoothSerial.unsubscribe(
                function (data) {
                console.log("UNSUBSCRIBED-> "+data);
                // CONNECT TO NEXT BTDEVICE
                app.connect()

                },
                app.showError
        );
                },     // stop listening to the port
                app.showError      // show the error if you fail
            );
showError: function(error) {
        console.log("ERROR RECEIVED -> "+ error);
    },
connect: function () {
            // attempt to connect:
            console.log("CONNECT");
            console.log("TRYING TO CONNECT WITH Mac> "+app.macAddress);
            bluetoothSerial.connect(
                app.macAddress,  // device to connect to
                app.openPort,    // start listening if you succeed
                app.showError    // show the error if you fail
        );
    }, 

Thanks!

@don
Copy link
Owner

don commented Oct 10, 2013

@manorius I think the correct way to do what you want is unsubscribe, disconnect, connect, subscribe. If the subscription is the same, you may be able to just disconnect from one, and connect to the second. (I haven't tested this.)

The way the code is structured, you can only be connected to on device at a time.

The subscribe and unsubcribe functions only affect how/when the javascript code is notified about the incoming data. Unsubscribing will not change the connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants