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

Slowness in subsequent connections (managed) #28

Open
StargazerNC opened this issue Sep 8, 2014 · 2 comments
Open

Slowness in subsequent connections (managed) #28

StargazerNC opened this issue Sep 8, 2014 · 2 comments

Comments

@StargazerNC
Copy link

Hi!

Well, I've been using this in one project and now I'm having (customer pointed this out for us) issues with slowness in the comunication phone <-> bluetooth device.

When it first connects, no problem, it takes about 1 sec most cases to connect. When I disconnect and get back, it also does it quickly. But if I go again and connect it can take up to 15 sec to connect again... Same code, some functions. The only difference is that it is not the first connection. My workflow can be synthesized like the listing below. I use pirometro.connectBluetooth() to start it all and connectBluetooth.disconnectBluetooth() to disconnect.

I'm a bit clueless on this one.

pirometro: {
   readTemperature: "",
   desvio: 0,
   stopMonitor: null,

  connectBluetooth: function () {
    // See If there is an address stored
    if (app.configuracao.MacAddPirometro === null || app.configuracao.MacAddPirometro === "") {
        console.log("Não existe pirometro configurado.");
        return;
    }

    var isAvailable = false;

    // Is there plugin active?
    if (window.bluetooth !== undefined) {
        console.log('It\'s alive. Cordova / Bluetooth plugin ok.');

        window.bluetooth.isEnabled(
            function (isOk) { // sucess callback
                if (isOk) {
                    isAvailable = isOk;
                }
                else {
                    isAvailable = false;
                    console.log("Bluetooth not available. Maybe it's not active on the device?");
                }
            },
            function () { // error callback
                isAvailable = false;
                console.log("Something went wrong. Couldn't check bluetooth availability.");
            });

        // Starthe a managed connection and a "monitor"
        if (isAvailable) {
            monitor = function () {

                window.bluetooth.startConnectionManager(
                    function (msg) {
                        var reading = jDataView(msg);

                        // Asking for data
                        if (reading.getUint8(0) === 3 && reading.getUint8(1) === 1) {
                            console.log("Preparing to send command");
                            window.bluetooth.write(

                            // This is a brute force command. It always sends the same thing, due to the format it has to be.
                            // That is why it was altered to my needs on the JAVA side.
                                function () {
                                    console.log('Wrote command succesfully!');
                                },
                                function (err) {
                                    console.log('There was an error writing: ' + err.message);
                                },
                                null); 
                        }
                        // Getting data
                        else if (reading.getUint8(0) === 1 && reading.getUint8(1) === 1) {

                            var tempRaw = reading.getUint32(54, true);

                            // Act on the data
                            (...)
                        }
                    },
                    function () {
                        console.log('Connection lost!');
                    });

                return function () {
                    window.bluetooth.stopConnectionManager(
                        function () {
                            console.log('Monitoring stopped!');
                        },
                        function (err) {
                            console.log('There was an error stopping the connection: ' + err.message);
                        });
                }
            }

                window.bluetooth.getUuids(
                    function (pairedDevice) {

                        window.bluetooth.isConnected(
                            function (isConnected) { // success callback
                                console.log("Is this connected => " + isConnected);
                                if (isConnected) {
                                    return;
                                }
                                else {
                                    console.log("not connected. Must connect first.");
                                    window.bluetooth.connect(
                                                function () {
                                                    console.log("Ok. We're on! Connection successful!...");
                                                    stopMonitor = monitor();
                                                },
                                                function (err) {
                                                    console.log('There was an error connecting to a device: ' + err.message);
                                                },
                                                {
                                                    uuid: pairedDevice.uuids[0],
                                                    address: pairedDevice.address   // 'my-bluetooth-address'
                                                });
                                }
                            },
                            function (error) { // Error callback
                                console.log('Something went wrong checking connection state => ' + error.message);
                            });
                    },
                    function (err) {
                        console.log('There was an error retrieving device UUIDs => ' + err.message);
                    },
                    //'00:06:66:43:05:14'); ITS
                    app.configuracao.MacAddPirometro);
        }
    }
    else {
        console.log("Bluetooth plugin and/or Cordova not ready.");
    }
},
disconnectBluetooth: function () {
    if (app.configuracao.MacAddPirometro === null || app.configuracao.MacAddPirometro === "") {
        console.log("Não existe pirometro configurado.");
        return;
    }

    var isAvailable = false;

    if (window.bluetooth !== undefined) {

        window.bluetooth.isEnabled(
            function (isOk) { // sucess callback
                if (isOk) {
                    isAvailable = isOk;
                }
                else {
                    isAvailable = false;
                    console.log("Bluetooth not available. Maybe it's not active on the device?");
                }
            },
            function () { // error callback
                isAvailable = false;
                console.log("Something went wrong. Couldn't check bluetooth availability.");
            });

        if (isAvailable)
        {
            stopMonitor();
            window.bluetooth.disconnect(
                function () {
                    console.log('Disconnected sucessfully.');
                },
                disconnectError = function (error) {
                    console.log('Something went wrong disconnecting => ' + error.message);
                });
        }
    } else {
        console.log("Bluetooth plugin and/or Cordova not ready.");
        return;
    }
}

}

@tanelih
Copy link
Owner

tanelih commented Sep 16, 2014

Hi,

Sorry for the late reply! I haven't encountered the issue you are describing in my applications. I am however looking at the code you posted and noticed this:

var isAvailable = false;

if (window.bluetooth !== undefined) {

    window.bluetooth.isEnabled(
        function (isOk) { // sucess callback
            if (isOk) {
                isAvailable = isOk;
            }
            else {
                isAvailable = false;
                console.log("Bluetooth not available. Maybe it's not active on the device?");
            }
        },
        function () { // error callback
            isAvailable = false;
            console.log("Something went wrong. Couldn't check bluetooth availability.");
        });

    if (isAvailable)
    {
        // isAvailable is always going to be false
    }
}

The isAvailable variable seems to always resolve to false since you are calling the asynchronous window.bluetooth.isEnabled, which mutates isAvailable before directly referencing the isAvailable in the following lines.

You could try something like:

function onState(isAvailable) {
  // do whatever!
}

window.bluetooth.isEnabled(
  function(isEnabled) {
    onState(isEnabled);
  },
  function(err) {
    // log errors
   onState(false);
  });

Hope this helps,
tanelih

@StargazerNC
Copy link
Author

Thanks Taneli,

That may be the cause for the slowness? I mean, I haven't had problems
with that check that I know of, but you pointed to a valid fact that the
call is async and that could be causing me unsespected pain... I'll
adapt the code with your idea and see what happens and then I'll let you
know.

Thanks!

On 16/09/2014 06:44, Taneli Hartikainen wrote:

Hi,

Sorry for the late reply! I haven't encountered the issue you are
describing in my applications. I am however looking at the code you
posted and noticed this:

|var isAvailable = false;

if (window.bluetooth !== undefined) {

 window.bluetooth.isEnabled(
     function (isOk) { // sucess callback
         if (isOk) {
             isAvailable = isOk;
         }
         else {
             isAvailable = false;
             console.log("Bluetooth not available. Maybe it's not active on the device?");
         }
     },
     function () { // error callback
         isAvailable = false;
         console.log("Something went wrong. Couldn't check bluetooth availability.");
     });

 if (isAvailable)
 {
     // isAvailable is always going to be false
 }

}
|

The |isAvailable| variable seems to always resolve to |false| since
you are calling the asynchronous |window.bluetooth.isEnabled|, which
mutates |isAvailable| before directly referencing the |isAvailable| in
the following lines.

You could try something like:

|function onState(isAvailable) {
// do whatever!
}

window.bluetooth.isEnabled(
function(isEnabled) {
onState(isEnabled);
},
function(err) {
// log errors
onState(false);
});
|

Hope this helps,
tanelih


Reply to this email directly or view it on GitHub
#28 (comment).

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