我正在尝试将多个设备连接到我手动选择的组所有者
我希望对等节点在找到群主后手动连接到他
我有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<WifiP2pDevice> 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));
}
}
}(我知道静态变量的使用和整体糟糕的设计,但这是一个需要学习的快速原型)
现在我得到了一个connect success Toast,但是连接之后的requestConnectionInfo显示了这一行
groupFormed: false isGroupOwner: false groupOwnerAddress: null调用requestGroupInfo还会显示:
group is null这种情况经常发生,大约有80%的情况发生。有时它会显示默认组IP 192.168.49.1。但如果两部“从属”手机都有正确的群组信息,那我就很幸运了。
当我尝试在组信息为空的情况下将套接字连接到所有者时,会失败并返回destination host unreachable。但是当群组信息正确时,我可以正确地连接和发送数据。
那么为什么会发生这种情况呢?怎么可能连接成功,但组为空呢?
发布于 2016-11-27 04:22:30
我重新做了整个项目,发现只有一个错误。
我在做
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在此更改后从不返回空信息,并且我连接到侦听器提供的IP时没有任何问题。
https://stackoverflow.com/questions/40679154
复制相似问题