我开发了一个 WiFi Direct 应用程序,我使用此代码来区分 groupOwner 和其他设备。但是 groupOwner 总是随机生成的。我想确保每次建立连接时连接设备都像 groupOwner 一样。我的代码:
if (info.groupFormed && info.isGroupOwner) {
// GroupOwner
} else if (info.groupFormed) {
}
最佳答案
如果您的目标是让正在广告服务的设备也是群组所有者,那么在您的 WifiP2pManager
实例上调用 onSuccess 中的
createGroup
addLocalService
WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
WifiP2pManager.Channel channel = serverManager.initialize(this, getMainLooper(), null);
HashMap<String, String> record = new HashMap<String, String>();
WifiP2pServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance("_hello", "_world._tcp", record);
//remove legacy group
manager.removeGroup(channel, null);
//advertise your service
manager.addLocalService(channel, serviceInfo, new WifiP2pManager.ActionListener() {
@Override
public void onFailure(int reason) {
}
@Override
public void onSuccess() {
//create group, making this device the owner of the group
manager.createGroup(channel, null);
}
});
关于android - 如何在 Wifi Direct 中创 build 备组所有者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19318291/