我正在开发一个应用程序,它使用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秒的间隔发现所需的peed。
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.然后,我会在5-10秒后再次调用connect
,它以onSuccess
方法返回。尽管如此,在连接的设备上没有出现连接提示对话框,这意味着我没有收到任何连接邀请。
这两个问题是使应用程序完全无法使用的主要问题。 任何人都可以向我解释我做错了什么或如何处理这些问题?
UPD
附加小发现会话的日志
主机日志(nexus 7):http://pastebin.com/ycfqRE4m
对等日志(nexus 10):http://pastebin.com/5kbp6e7A