我正在通过Wifi连接两个android设备。我在第一个设备上使用Wifip2pManager.createGroup创建了一个组。
现在,在第二个设备上,我调用了Wifip2pManager.connect方法。但是连接方法是成功的,即使第一个设备拒绝连接,因为它只检查初始化是否成功。如何检查其他设备是否接受了连接?
发布于 2015-04-04 15:06:17
该类需要实现ConnectionInfoListener。在函数onConnectionInfoAvailable(final WifiP2pInfo info)中,您可以检查是否建立了成功的连接。类型为info的WifiP2pInfo参数包含有关连接的信息。它包含一个名为groupFormed的布尔值,它指示一个p2p组是否已经成功地形成。如果设备是groupOwner和groupOwner的IP,您也可以从它检索,等等。
@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
}谷歌提供了一个非常好的演示应用程序,它使用WiFi直接在两个设备之间发送图像。检查它的实现并尝试在其之上构建。链接:http://developer.android.com/guide/topics/connectivity/wifip2p.html
希望这能有所帮助。如果你有任何问题,请告诉我。
https://stackoverflow.com/questions/29445560
复制相似问题