Android Wifip2p: why group information is null after connecting to group owner

I try to connect several devices to the group owner, which I manually select

I want peers to connect to the group owner manually when they find it

I have 3 phones (without emulators), each has 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 requestGroupInfoand pass it a listener that prints the following line in the logs:

groupFormed: true isGroupOwner: true groupOwnerAddress: /192.168.49.1

So, I suppose that succeeded

I also have a class MyPeerListenerthat calls BroadCastReceiverwhen an intent is accepted WIFI_P2P_PEERS_CHANGED_ACTION.

MyPeerListener will go through peers and connect if he finds the 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));
        }

        }


    }

( , )

connect success Toast, requestConnectionInfo

groupFormed: false isGroupOwner: false groupOwnerAddress: null

requestGroupInfo :

group is null

80% . IP 192.168.49.1. , "" .

, - null, destination host unreachable. , .

? , , null?

+4
1

mManager.connect(...,new ActionListener(){
@Override
onSuccess(){
    //Here I called requestConnectionInfo right after the pairing happens
    mManager.requestConnectionInfo(myConnectionInfoListener);
    Socket socket = new Socket();
    // try to connect and send data....

}

@Override
onFailure(int reason){

}

});

, requestConnectionInfo() onSuccess() connect() MyWifiDirectBroadcastReceiver, BroadcastReceiver

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
        // Check to see if Wi-Fi is enabled and notify appropriate activity
    } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
        // Call WifiP2pManager.requestPeers() to get a list of current peers
    } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
        //Here is where the request for connection info should happen
        //As sometimes the pairing can happen but the devices 
        //might still be negotiating group owner for example
        mManager.requestConnectionInfo(myConnectionInfoListener());
    } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
        // Respond to this device wifi state changing
    }
}

MyConnectionInfoListener , IP, .

+1

Source: https://habr.com/ru/post/1661210/


All Articles