我正在通过 Wifi Direct 连接两个 Android 设备。我在第一台设备上使用 Wifip2pManager.createGroup 创建了一个组。
现在,在第二台设备上我调用 Wifip2pManager.connect 方法。但即使第一个设备拒绝连接,连接方法也会成功,因为它只检查是否成功初始化。如何检查其他设备是否接受连接?
该类需要实现 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
}
Google 提供了一个非常好的演示应用程序,它使用 WiFi Direct 在两个设备之间发送图像。检查其实现并尝试在其基础上进行构建。关联:http://developer.android.com/guide/topics/connectivity/wifip2p.html
希望这可以帮助。如果您有任何疑问,请告诉我。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)