检查android wifip2p连接是否成功? [英] Check whether android wifip2p connection was successful?

查看:179
本文介绍了检查android wifip2p连接是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Wifi Direct连接两个android设备.我在第一台设备上使用Wifip2pManager.createGroup创建了一个组.

I am connecting two android devices via Wifi Direct. I created a group using Wifip2pManager.createGroup on the first device.

现在,在第二台设备上,我调用Wifip2pManager.connect方法.但是,即使第一个设备由于仅检查成功的初始化而拒绝了连接,connect方法还是成功的.如何检查其他设备是否接受了连接?

Now, on the second device I call the Wifip2pManager.connect method. But the connect method is succesful, even if the first device declines the connection since it only checks for successful initialization. How do I check if the other device accepted the connection ?

推荐答案

该类需要实现ConnectionInfoListener.在 onConnectionInfoAvailable(最终WifiP2pInfo信息)函数中,您可以检查是否已建立成功的连接.类型为 WifiP2pInfo info 自变量包含有关连接的信息.它包含一个名为 groupFormed 的布尔值,该布尔值指示是否已成功形成p2p组.您还可以从中检索设备是否为groupOwner以及groupOwner的IP等.

The class needs to implement ConnectionInfoListener. And in the function onConnectionInfoAvailable(final WifiP2pInfo info) you can check if a successful connection has been established or not. The info argument of type WifiP2pInfo contains the information about the connection. It contains a boolean called groupFormed which indicates if a p2p group has been successfully formed. You can also retrieve from it if the device is groupOwner and the IP of the groupOwner, etc.

@Override
    public void onConnectionInfoAvailable(final WifiP2pInfo info) {

        // After the group negotiation, we check if this device 
        // is acting as group owner

        if (info.groupFormed && !info.isGroupOwner) {

            // Do whatever you want the group owner to do

        } else if (info.groupFormed) {
            // The device now act as the slave device, 
            // and the other connected device is group owner
    }

Google提供了一个非常好的演示应用程序,它使用WiFi Direct在2个设备之间发送图像.检查其实现并尝试在其之上进行构建.链接: http://developer.android.com/guide/topics/connectivity/wifip2p.html

Google provides a very good demo app that uses WiFi Direct to send an image between 2 devices. Check its implementation and try to build on top of it. Link: http://developer.android.com/guide/topics/connectivity/wifip2p.html

希望这会有所帮助.如果您有任何问题,请告诉我.

Hope this helps. Let me know if you have any questions.

这篇关于检查android wifip2p连接是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆