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

can't start the connection manager after have been connected #22

Open
Brileantag opened this issue Jul 18, 2014 · 10 comments
Open

can't start the connection manager after have been connected #22

Brileantag opened this issue Jul 18, 2014 · 10 comments

Comments

@Brileantag
Copy link

I have installed the plugin on phonegap CLI 3.0.0, and tried listing bluetooth devices, pairing and connecting sucessfully. Then when i try to start a managed connection, i have an error with the message "Socket has no active connection", and the code is 0, i don't know why.
I am making tests with an LG phone with android 4.1.2 and an Alcatel Pixi 2 with android 4.2
I have the SDK API 19 for building apk.

Here is a code snippet

var bluetooth=window.bluetooth;
var uuid;

var address="XX:XX:XX:XX:XX:XX"; // my phone address

if(bluetooth){  // if the plugin is successfully integrated
        // if the device is not paired, pair it
        var ispaired=function(already){
            if(!already){
                var successOnpaired=function(){
                    alert("paired");
                };
                var errorOnpairing=function(error){
                    alert("not paired, error occured"+JSON.stringify(error));
                }
                bluetooth.pair(successOnpaired,errorOnpairing, address);
            }
        }

        var errorChecking=function(error){
            alert("Error occured while checking if it was paired"+JSON.stringify(error));
        }

// check if the device is already paired with the one's getting address address, and pair if not yet
        bluetooth.isPaired(ispaired,errorChecking, address);

        // callback for stopping the discorery, if any in progress
        var onDiscovering=function(istrue){
            if(istrue){
                 bluetooth.stopDiscovery(null, null);
            }

        }
        // check if there is a discovery process in progress, and stop it            
        bluetooth.isDiscovering(onDiscovering, null);

       // callback to execute if an uuid is found for the device paired with
        var onUuidsRetrieved=function(device){
            var sucessconnect=function(){
                alert("Connexion établie");
                var messageReceived=function(message){
                    alert("received! "+message);                        
                };

                var onConnectionLost=function(error){
                    alert("Connection lost "+JSON.stringify(error));
                    console.log("Connection lost "+JSON.stringify(error));
                };
                // start the manager, this always fail: the node of problem
                bluetooth.startConnectionManager(messageReceived, onConnectionLost);
            };

            var errorOnconnect=function(error){
                alert("Erreur lors de la tentative de connexion:  "+JSON.stringify(error));
            };

            uuid=device.uuids[0];

            bluetooth.connect( sucessconnect, errorOnconnect,{address: address,uuid: uuid});
            alert("adress="+address); // show the device address, confirmed to be in the expected format

        };

        var errorOnretreiveUuid=function(error){
            alert("Error while retreiving uuids "+JSON.stringify(error));
        };
        bluetooth.getUuids(onUuidsRetrieved, errorOnretreiveUuid, address);

    }
@tanelih
Copy link
Owner

tanelih commented Jul 22, 2014

Hi,

Sorry for the late reply. Is there a reason you are not letting the isPaired and isDiscovering function calls complete before moving on to calling getUuids? The getUuids -> connect -> startConnectionManager flow seems to be correct, but maybe calling the isPaired and isDiscovering functions like that has some side effects?

You might want to try connecting without calling the pair and discovery functions before moving to the connection sequence.

@kozak
Copy link
Contributor

kozak commented Jul 22, 2014

On some devices, the BluetoothSocket#isConnected method always returns "false". I had the same problem and it is also reported here (http://stackoverflow.com/questions/14792040/android-bluetoothsocket-isconnected-always-returns-false).

Because of this, the plugin wont work on some devices. In my local version I write to the socket without checking its isConnected state, and handle exceptions afterwards. When disconnecting I set the socket back to null.

@tanelih
Copy link
Owner

tanelih commented Jul 23, 2014

Is there a list of which devices are affected by BluetoothSocket#isConnected always returning false? That should probably be mentioned in the README file...

I'm currently in a situation where I do not have any equipment to test this plugin out on or develop reliably. I'll make a mention of this in the README in case someone is interested to fork and develop this plugin.

@Brileantag
Copy link
Author

Hi,
My problem doesn't concern the BluetoothSocket#IsConnected function, it returns true if really connected. Nevertheless, after have been connected successfully, the startConnectionManager function is called, that ends ith his error callback with the error message "Socket has no active connection".
I have used this flow for the isPaired and the isDiscovering functions because sometimes the devices could have been paired previously, or have been discovering before. Thanks, i will change the flow and see the result.

@kozak
Copy link
Contributor

kozak commented Jul 23, 2014

@h2altitude How did you check that the BluetoothSocket#isConnected returns true properly ? look at the code here:
https://github.com/tanelih/phonegap-bluetooth-plugin/blob/master/src/android/BluetoothWrapper.java#L588

When you are trying to start the connection manager, and the socket isConnected returns false, the error you are mentioning occurs.

@kozak
Copy link
Contributor

kozak commented Jul 23, 2014

@tanelih here is my fork, with the changes applied:
https://github.com/kozak/phonegap-bluetooth-plugin

Not sure if you want me to make a pull request. I didn't test it extensively.

I don't know of any list of the devices. It might be related to the fact that isConnected method was not present in lower API versions.

@tanelih
Copy link
Owner

tanelih commented Jul 23, 2014

@kozak Thanks for that! I took a quick look at it and didn't really see any weirdness with it. Make a pull request. 😃

I take it that in your fork it would mean that whenever the underlying _socket isn't null, BluetoothWrapper#isConnected would return true. I think this behaviour is acceptable especially if the BluetoothSocket#isConnected method would not always return true for active connections.

@Brileantag
Copy link
Author

@kozak i just tested the BluetoothSocket#isConnected function, and i wonder, as you said, it always returns false. Thank you for bringing that to my attention.
How do i process to integrate the changes you made, in the version i installed through the phonegap CLI?
Or should i wait till @tanelih makes the change?

@kozak
Copy link
Contributor

kozak commented Jul 23, 2014

@tanelih I've made the pull request. Exactly, when the socket is not null, we assume its connected. We null it on disconnect() and read() failure. Any write or read on disconnected socket will cause an error as normal.

@h2altitude best if you wait for tanelih, then remove your plugin and reinstall it. For a quick test you can do this:

cordova plugin rm com.phonegap.plugins.bluetooth; cordova plugin add https://github.com/kozak/phonegap-bluetooth-plugin

@tanelih
Copy link
Owner

tanelih commented Jul 23, 2014

Thanks for your contributions! The pull request is now merged.

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

3 participants