我正在开发一个应用程序,它使用 wifi-direct 创建最多 4 个设备的组(1 个主机 + 3 个对等设备)。我从 developer.android.com 阅读了 wifi-direct 的手册,找到了这个精彩的答案:https://stackoverflow.com/a/31641302/3106249 - 而且,我还有几个问题不知道如何处理。
第一个问题一步一步:
在主机设备上注册本地服务并创建组。
Map<String, String> record = new HashMap<String, String>();
record.put(TXTRECORD_PROP_AVAILABLE, "visible");
record.put(Core.SESSION_KEY, Core.SESSION_KEY_VALUE);
record.put(Core.SERVICE_INSTANCE_KEY, SERVICE_INSTANCE);
localService = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_INSTANCE, Core.SERVICE_REG_TYPE, record);
manager.clearLocalServices(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "clearLocalServices success");
manager.addLocalService(channel, localService, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "addLocalService success");
manager.createGroup(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "createGroup success");
}
@Override
public void onFailure(int reason) {
Log.d(TAG, "createGroup fail: " + reason);
}
});
}
@Override
public void onFailure(int reason) {
Log.d(TAG, "addLocalService fail: " + reason);
}
});
}
@Override
public void onFailure(int reason) {
Log.d(TAG, "clearLocalServices fail: " + reason);
}
});
以 10 秒的间隔发现主机设备所需的速度。
manager.removeServiceRequest(channel, serviceRequest, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "discovering, removeServiceRequest success");
manager.stopPeerDiscovery(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "discovering, stopPeerDiscovery success");
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "discovering, discoverPeers success");
manager.addServiceRequest(channel, serviceRequest, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "discovering, addServiceRequest success");
manager.discoverServices(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
//Log.d(TAG, "discoverServices success");
}
@Override
public void onFailure(int reason) {
Log.d(TAG, "discoverServices fail: " + reason);
}
});
}
@Override
public void onFailure(int reason) {
Log.d(TAG, "addServiceRequest fail: " + reason);
}
});
}
@Override
public void onFailure(int reason) {
Log.d(TAG, "discoverPeers fail: " + reason);
}
});
}
@Override
public void onFailure(int reason) {
Log.d(TAG, "stopPeerDiscovery fail: " + reason);
}
});
}
@Override
public void onFailure(int reason) {
Log.d(TAG, "clearServiceRequests fail: " + reason);
}
});
发送连接邀请(通过调用 WiFiP2pManager#connect()
)
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
config.wps.setup = WpsInfo.PBC;
manager.connect(channel, config, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "manger.onSuccess with " + device.deviceName);
}
@Override
public void onFailure(int errorCode) {
Log.d(TAG, "Failed connecting to service " + errorCode);
}
});
WiFiP2pManager#connect()
时, 它在 ActionListenter 的 onFailure
中返回方法,错误代码为 2。然后,我调用 connect
5-10 秒后再次返回 onSuccess
方法。尽管如此,在连接的设备上没有出现连接提示对话框,这意味着我想没有收到连接邀请。
这两个问题是导致应用程序完全无法使用的主要问题。 谁能向我解释我做错了什么或如何处理这些问题?
UPD
为小型发现 session 附加日志。
主机日志 (nexus 7):http://pastebin.com/ycfqRE4m
对等日志 (nexus 10):http://pastebin.com/5kbp6e7A
最佳答案
这是我对你第二个问题的解决方案 我使用一个线程来发现并且不使用 Intent 接收器 第三个问题,在连接完成之前不要使用 stopDiscovery
ConnectHelper.manager.discoverPeers(ConnectHelper.channel, null);
new WiFiDirectScanner().start();
private class WiFiDirectScanner extends Thread {
@Override
public void run() {
while (context != null) {
try {
ConnectHelper.manager.requestPeers(ConnectHelper.channel, peerList -> {
Collection<WifiP2pDevice> refreshedPeers = peerList.getDeviceList();
if (!refreshedPeers.equals(peers)) {
peers.clear();
peers.addAll(refreshedPeers);
scanSuccessWD();
}
});
} catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (InterruptedException ignored) {
}
}
}
}
关于Android WiFi-Direct : discovering, 连接问题。详细讨论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36161706/