我开发了WiFi直接应用程序,并使用此代码来区分groupOwner和其他设备。但是groupOwner总是随机制作的。我想确保每次建立连接时连接设备都像groupOwner一样。我的代码:
if (info.groupFormed && info.isGroupOwner) {
// GroupOwner
} else if (info.groupFormed) {
}
答案 0 :(得分:7)
如果您的目标是制作宣传服务的设备,也要宣传群组所有者,然后在WifiP2pManager
createGroup
onSuccess
回调中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);
}
});
}}
{{1}}
答案 1 :(得分:5)
您必须使用groupOwnerIntent
对象的属性WifiP2pConfig
,并将其传递给connect()调用。例如:
config.groupOwnerIntent = 15;
值范围在0-15之间,值越高,成为groupOwner的可能性越大。