Android 如何在WifiDirect中创建设备组所有者?
我开发了一个WiFi Direct应用程序,并使用此代码区分groupOwner和其他设备。但groupOwner总是随机产生的。我想确保每次建立连接时,连接设备都像groupOwner一样工作。我的代码:Android 如何在WifiDirect中创建设备组所有者?,android,wifi-direct,Android,Wifi Direct,我开发了一个WiFi Direct应用程序,并使用此代码区分groupOwner和其他设备。但groupOwner总是随机产生的。我想确保每次建立连接时,连接设备都像groupOwner一样工作。我的代码: if (info.groupFormed && info.isGroupOwner) { // GroupOwner } else if (info.groupFormed) { } 您必须使用WifiP2pConfig对象的属性g
if (info.groupFormed && info.isGroupOwner) {
// GroupOwner
} else if (info.groupFormed) {
}
您必须使用
WifiP2pConfig
对象的属性groupOwnerIntent
,将其传递给connect()调用。例如:
config.groupOwnerIntent = 15;
值的范围在0-15之间,值越高,成为组所有者的可能性越高。如果您的目标是让正在宣传服务的设备同时成为组所有者,则在
addLocalService
的onSuccess
回调中的WifiP2pManager
实例调用createGroup
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);
}
});
WifiP2pManager管理器=(WifiP2pManager)getSystemService(Context.WIFI\u P2P\u服务);
WifiP2pManager.Channel Channel=serverManager.initialize(this,getMainLooper(),null);
HashMap记录=新建HashMap();
WifiP2pServiceInfo serviceInfo=WifiP2pDnsSdServiceInfo.newInstance(“\u hello”、“\u world.\u tcp”,记录);
//删除旧组
manager.removeGroup(通道,空);
//为你的服务做广告
manager.addLocalService(频道、serviceInfo、新WifiP2pManager.ActionListener(){
@凌驾
公共失效失效失效(内部原因){
}
@凌驾
成功时的公共无效(){
//创建组,使此设备成为组的所有者
manager.createGroup(通道,空);
}
});