首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Wifip2p:为什么接入群主后群信息为空?

Android Wifip2p:为什么接入群主后群信息为空?
EN

Stack Overflow用户
提问于 2016-11-18 22:10:25
回答 1查看 1.5K关注 0票数 4

我正在尝试将多个设备连接到我手动选择的组所有者

我希望对等节点在找到群主后手动连接到他

我有3个电话(没有模拟器),在每个有一个“创建组”按钮与此点击处理程序

代码语言:javascript
复制
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) {

        }
    });

}

如您所见,我执行了requestGroupInfo操作,并向它传递了一个监听程序,该监听程序在日志中打印以下行:

代码语言:javascript
复制
groupFormed: true isGroupOwner: true groupOwnerAddress: /192.168.49.1

所以我想它成功了

我还有一个MyPeerListener类,当收到WIFI_P2P_PEERS_CHANGED_ACTION操作意图时,BroadCastReceiver会调用它。

MyPeerListener将遍历对等节点并在找到组所有者时进行连接

代码语言:javascript
复制
 @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显示了这一行

代码语言:javascript
复制
groupFormed: false isGroupOwner: false groupOwnerAddress: null

调用requestGroupInfo还会显示:

代码语言:javascript
复制
group is null

这种情况经常发生,大约有80%的情况发生。有时它会显示默认组IP 192.168.49.1。但如果两部“从属”手机都有正确的群组信息,那我就很幸运了。

当我尝试在组信息为空的情况下将套接字连接到所有者时,会失败并返回destination host unreachable。但是当群组信息正确时,我可以正确地连接和发送数据。

那么为什么会发生这种情况呢?怎么可能连接成功,但组为空呢?

EN

回答 1

Stack Overflow用户

发布于 2016-11-27 04:22:30

我重新做了整个项目,发现只有一个错误。

我在做

代码语言:javascript
复制
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){

}

});

因此,我从connect()onSuccess()回调中删除了requestConnectionInfo(),并将其放在扩展BroadcastReceiverMyWifiDirectBroadcastReceiver

代码语言:javascript
复制
@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's wifi state changing
    }
}

MyConnectionInfoListener在此更改后从不返回空信息,并且我连接到侦听器提供的IP时没有任何问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40679154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档