neatorobito/Salut

registerWithHost not working (socket IOException in BackgroundClientRegistrationJob.doOnBackground())

Opened this issue · 5 comments

Hi. First of all, thank you for your great work on this project!

I'm already trying for hours to connect two devices without success. After a lot of debugging and testing I've found out that "BackgroundClientRegistrationJob.doOnBackground()" throws an IOException because the socket cannot be connected (Line 40 in BackgroundClientRegistrationJob.java: "registrationSocket.connect(this.hostDeviceAddress);").

What works and doesn't work (written from the perspective of the client side):
The hostDeviceAddress is /192.168.49.1:37500 so that looks fine. I successfully get the connect request dialog on the host side (after calling registerWithHost() on the client side) which I accept, then onConnectionInfoAvailable gets called (client side) soon after and then the GroupInfoListener.onGroupInfoAvailable() method is executed. There in the last line the Salut.this.startRegistrationForClient is called. After that I get the exception in BackgroundClientRegistrationJob.doOnBackground() (line 40) although the devices successfully connected to each other (I can see that in the settings -> wireless -> wifi direct)...

Assumptions

  • Could it be that wifi direct needs a moment to "fully" establish the connection? If so then maybe a small execution delay of BackgroundClientRegistrationJob.doOnBackground() would make it work?
  • Or could it be that the client turns into the host because the WifiP2pConfig.groupOwnerIntent is set to -1 (default) - 0 would indicate the least inclination to be a group owner whereas -1 means that it is up to the system to define the owner (which is quite random).

I would highly appreciate any help/hints and thoughts.

Thank you and best regards

I got the same problem with the master branch, v0.5.0 is working.

I think this might be directly related to #19 and has to do with connecting use manually created groups. I'm going to do some testing.

If i'm calling network.createGroup it's working

Hi,
@markrjr Many thanks for your library !
@hunnohu I met the same problem today and solve it after having a deep look in Salut implementation.
The problem seems to come from the onConnectionInfoAvailable method. Here is the method modified with some logs :

@Override
    public void onConnectionInfoAvailable(final WifiP2pInfo info) {
        /* This method is automatically called when we connect to a device.
         * The group owner accepts connections using a server socket and then spawns a
         * client socket for every client. This is handled by the registration jobs.
         * This will automatically handle first time connections.*/

        manager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() {
            @Override
            public void onGroupInfoAvailable(WifiP2pGroup group) {

                if (isRunningAsHost && !registrationIsRunning) {
                    Log.d(Salut.TAG, "This is a Host without any registration in progress !");
                    Log.d(Salut.TAG, "Is the group already formed ? " + info.groupFormed);
                    Log.d(Salut.TAG, "Is the group already have any client ? " + !group.getClientList().isEmpty());
                    if (info.groupFormed && !group.getClientList().isEmpty()) {
                        Log.d(Salut.TAG, "Attempting registering as host !");
                        startHostRegistrationServer();
                    }
                } else if (!thisDevice.isRegistered && !info.isGroupOwner) {
                    if (serviceRequest == null) {
                        //This means that discoverNetworkServices was never called and we're still connected to an old host for some reason.
                        Log.e(Salut.TAG, "This device is still connected to an old host for some reason. A forced disconnect will be attempted.");
                        forceDisconnect();
                    }
                    Log.v(Salut.TAG, "Successfully connected to another device.");
                    startRegistrationForClient(new InetSocketAddress(info.groupOwnerAddress.getHostAddress(), SALUT_SERVER_PORT));
                }
            }
        });
    }

The problem is Salut is trying to connect a server which hasn't been initialized because startHostRegistrationServer(); is never called except if you create a group manually. Indeed there is only one way to make the variable isRunningAsHost true and it's in the createGroup(...) method.

Here is how you can start a service in the right way using salut (master branch version only !) :

network.createGroup(new SalutCallback() {
            @Override
            public void call() {
                Log.d(TAG, " Group created!");
            }
        }, new SalutCallback() {
            @Override
            public void call() {
                Log.e(TAG, " Group failed to be created!");
            }
        });
        network.startNetworkService(new SalutDeviceCallback() {
            @Override
            public void call(SalutDevice salutDevice) {
                Log.d(TAG, salutDevice.readableName + " has connected!");
            }
        });

Hope it will help you,
Jean-Baptiste

@JeanBaptisteWATENBERG
This still does not for me.
createGroup is always erroring out, with a reason=2, which should be - BUSY
And again, startHostRegistrationServer is never executed.