我正在尝试将多个设备连接到我手动选择的组所有者
我希望对等方在找到他后手动连接到群组所有者
我有3个电话(没有模拟器),每个电话上都有一个“创建组”按钮
public void createWifiGroup(View view) {
mManager.createGroup(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
mManager.requestGroupInfo(mChannel,new MyGroupInfoListener(MainActivity.this));
}
@Override
public void onFailure(int reason) {
}
});
}
正如您所看到的,我执行requestGroupInfo并将其传递给在日志中打印以下行的侦听器:
groupFormed: true isGroupOwner: true groupOwnerAddress: /192.168.49.1
所以我认为它成功了
我还有一个MyPeerListener类,当接收到WIFI_P2P_PEERS_CHANGED_ACTION动作意图时,BroadCastReceiver会调用该类.
MyPeerListener将遍历对等体并在找到组所有者时进行连接
@Override
public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
this.wifiP2pDeviceList = wifiP2pDeviceList;
//Toast toast = Toast.makeText(receiver.mActivity, "I found some peers", Toast.LENGTH_LONG);
// toast.show();
Iterator deviceListIterator = wifiP2pDeviceList.getDeviceList().iterator();
boolean foundGroup = false;
while (deviceListIterator.hasNext()) {
final WifiP2pDevice device = deviceListIterator.next();
if (!foundGroup && device.isGroupOwner() && !MainActivity.connected) {
//MainActivity.mManager.removeGroup(MainActivity.mChannel, null);
foundGroup = true;
final WifiP2pConfig config = new WifiP2pConfig();
//config.wps.setup = WpsInfo.PBC;
config.groupOwnerIntent = 0;
config.deviceAddress = device.deviceAddress;
MainActivity.mManager.connect(MainActivity.mChannel, config, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
//success logic
Toast toast = Toast.makeText(receiver.mActivity, "connect success", Toast.LENGTH_SHORT);
toast.show();
MainActivity.connected = true;
MainActivity.mManager.requestGroupInfo(MainActivity.mChannel, new MyGroupInfoListener(receiver.mActivity));
MainActivity.mManager.requestConnectionInfo(MainActivity.mChannel, new MyConnectionInfoListener(receiver));
}
@Override
public void onFailure(int reason) {
//failure logic
//MainActivity.connected = false;
Toast toast = Toast.makeText(receiver.mActivity, "connect fail, reason: " + reason, Toast.LENGTH_LONG);
toast.show();
}
});
}
ListView list = (ListView) receiver.mActivity.findViewById(R.id.device_list_view);
list.setAdapter(new DeviceListViewAdapter(this, receiver.mActivity));
}
}
}
(我知道使用静态变量和整体糟糕的设计,但这是一个快速的原型学习)
现在我获得连接成功Toast但是连接后的requestConnectionInfo显示了这一行
groupFormed: false isGroupOwner: false groupOwnerAddress: null
对requestGroupInfo的调用还显示:
group is null
这种情况经常发生在大约80%的时间.有时它会显示默认组IP 192.168.49.1.但如果两个“奴隶”手机都有正确的群组信息,我会很幸运.
当我尝试将套接字连接到所有者而组信息为空时,它失败,目标主机无法访问.但是当组信息正确时,我可以正确连接和发送数据.
那为什么会这样呢?如何连接成功但该组为空?
解决方法:
我重新做了整个项目,发现只有一个错误
我在做
mManager.connect(...,new ActionListener(){
@Override
onSuccess(){
//Here I called requestConnectionInfo right after the pairing happens
mManager.requestConnectionInfo(myConnectionInfoListener);
Socket socket = new Socket();
// try to connect and send data....
}
@Override
onFailure(int reason){
}
});
所以我从connect()的onSuccess()回调中删除了requestConnectionInfo(),并在扩展BroadcastReceiver的MyWifiDirectBroadcastReceiver中删除了它
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Check to see if Wi-Fi is enabled and notify appropriate activity
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// Call WifiP2pManager.requestPeers() to get a list of current peers
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
//Here is where the request for connection info should happen
//As sometimes the pairing can happen but the devices
//might still be negotiating group owner for example
mManager.requestConnectionInfo(myConnectionInfoListener());
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
}
}
在此更改后,MyConnectionInfoListener永远不会返回null信息,并且我连接到侦听器提供的IP没有任何问题.
标签:android,sockets,wifi-direct,wifip2p
来源: https://codeday.me/bug/20190701/1352095.html