LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 November 22 2016

vlatkozelka
Member

Wifi-Direct issue

Dear Lebanese programmers,

I am working on my graduation project

It's about Network Coding: A way to optimize broadcast . I mean to implement it on mobile phones, and for that I need to connect the phones without the need for a router. So I decided to use Wifi-Direct or Android WiFiP2P API

I hope this isn't against this site's policy but , I already posted the question on StackOverflow. But it never even had one comment . I even added a bounty to no avail.

Link to question

I will post the question here, but if you have an account on SO with privilege to upvote posts, please gimme some upvotes so it would attract some people to it, I really need this fixed !

I am trying to connect multiple devices to a group owner that I select manually

I want the peers to connect to the group owner manually once they find him

I have 3 phones (no emulators), on each there is a "Create group" button with this click handler

    public void createWifiGroup(View view) {

        mManager.createGroup(mChannel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {

                mManager.requestGroupInfo(mChannel,new MyGroupInfoListener(MainActivity.this));

            }

            @Override
            public void onFailure(int reason) {

            }
        });

    }

As you can see I do `requestGroupInfo` and pass it a listener that is printing the following line in logs:

    groupFormed: true isGroupOwner: true groupOwnerAddress: /192.168.49.1

So I suppose it succeeded

I also have a `MyPeerListener` class that the `BroadCastReceiver` calls when a `WIFI_P2P_PEERS_CHANGED_ACTION` action intent is received.

`MyPeerListener` will iterate through the peers and connect if it finds a group owner

     @Override
        public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
            this.wifiP2pDeviceList = wifiP2pDeviceList;
            //Toast toast = Toast.makeText(receiver.mActivity, "I found some peers", Toast.LENGTH_LONG);
            // toast.show();
            Iterator<WifiP2pDevice> deviceListIterator = wifiP2pDeviceList.getDeviceList().iterator();
            boolean foundGroup = false;
            while (deviceListIterator.hasNext()) {
                final WifiP2pDevice device = deviceListIterator.next();
                if (!foundGroup && device.isGroupOwner() && !MainActivity.connected) {
    
                    //MainActivity.mManager.removeGroup(MainActivity.mChannel, null);
    
                    foundGroup = true;
    
                    final WifiP2pConfig config = new WifiP2pConfig();
                    //config.wps.setup = WpsInfo.PBC;
                    config.groupOwnerIntent = 0;
                    config.deviceAddress = device.deviceAddress;
                    
                    MainActivity.mManager.connect(MainActivity.mChannel, config, new WifiP2pManager.ActionListener() {
    
                        @Override
                        public void onSuccess() {
                            //success logic
                            Toast toast = Toast.makeText(receiver.mActivity, "connect success", Toast.LENGTH_SHORT);
                            toast.show();
                            MainActivity.connected = true;
                            MainActivity.mManager.requestGroupInfo(MainActivity.mChannel, new MyGroupInfoListener(receiver.mActivity));
                            MainActivity.mManager.requestConnectionInfo(MainActivity.mChannel, new MyConnectionInfoListener(receiver));
                             
    
                        }
    
                        @Override
                        public void onFailure(int reason) {
                            //failure logic
                            //MainActivity.connected = false;
                            Toast toast = Toast.makeText(receiver.mActivity, "connect fail, reason: " + reason, Toast.LENGTH_LONG);
                            toast.show();
                        }
    
                    });
                }
                ListView list = (ListView) receiver.mActivity.findViewById(R.id.device_list_view);
                list.setAdapter(new DeviceListViewAdapter(this, receiver.mActivity));
            }
            
            }
    
    
        }

(I am aware of the use of static variables and overall bad design , but this is a fast prototype to learn)

Now I get a `connect success` `Toast` but the `requestConnectionInfo` right after the connection shows me this line

    groupFormed: false isGroupOwner: false groupOwnerAddress: null

A call to requestGroupInfo also shows:

    group is null

This happens very often about 80% of the times. Sometimes it shows the default group IP `192.168.49.1` . But I'll be lucky if both the "slave" phones have correct group info.

When I try to connect a socket to the owner while the group info is null it fails with `destination host unreachable`. But when the group info is correct I can connect and send data properly.

So why is this happening ? How is it even possible that the connection succeeds but the group is null ?

Offline

#2 February 16 2017

Frebaz
Member

Re: Wifi-Direct issue

Solved?

Offline

#3 February 23 2017

vlatkozelka
Member

Re: Wifi-Direct issue

Frebaz wrote:

Solved?

Yeah

Turns out Android docs forgot one tiny detail about the connection process. When the ConnectionInfoListener goes into onSuccess block it means that the connection was accepted by the other phone but the actual connection isn't there yet (idk how much sense it makes but that's how it is apparently)

So me trying to query connection info right inside onSuccess gives me null. It turns out I should do that in the WifiBroadcastReciever. Which is basically a Broadcast Receiver that receives intents about the status of the connection. When the WifiBroadcastReceiver receives an Intent.WIFI_STATUS_CHANGED (something like that) there it actually means the connection is ready to be used.

I know I should've thought of it, but it's just very misleading when you have an onSuccess that just doesn't do what you'd think it does 
Also very misleading of Android docs to say

onSuccess(){
...
//do what you want on connection success
// lol
}

Offline

Board footer