WifiP2pManager.requestPeers() 在 onPeersAvailable() 中不断返回旧对等点
Posted
技术标签:
【中文标题】WifiP2pManager.requestPeers() 在 onPeersAvailable() 中不断返回旧对等点【英文标题】:WifiP2pManager.requestPeers() keeps returning old peers in onPeersAvailable() 【发布时间】:2016-02-18 16:34:10 【问题描述】:于是我做了功课,找了类似的问题,居然发现一个人两年前也有同样的问题……he didn't get any answer though.
问题如下:我正在做android的WifiP2p教程。我在 MainActivity 上使用 FloatingActionButton
手动调用 discoverPeers()
。然后WIFI_P2P_PEERS_CHANGED_ACTION
在WifiDirectBroadcastReceiver类中广播和接收。在那里我调用requestPeers()
,而后者又调用onPeersAvailable()
仅如果有至少一个可发现的对等体(这很有意义。)
然而,问题是一旦它发现了一个对等点,它就永远不会从列表中消失。即使我关闭了检测到的设备。我举个例子来说明:
3 台设备(A、B 和 C),均支持 WiFi-Direct。 在设备 A 上启动应用,让设备 B 和 C 保持关闭状态。 尝试用 A 检测,没有检测到。没问题。 打开开启设备 B,进入 WiFi-Direct 设置,扫描。 尝试用A检测,检测B。没问题。 关闭关闭设备 B。 尝试用 A 检测,没有检测到。没问题。 打开开启设备 C,进入 WiFi-Direct 设置,扫描。 尝试用 A 检测,检测 C和 B。但设备 B 仍然关闭。为什么它仍然被检测到? (最糟糕的是,它会被检测到状态 3,对应于 AVAILABLE)设备角色的任何排列都会发生这种情况。所以问题确实出在代码中,因为它们都可以通过官方 WiFi-Direct 设置窗口正常运行和检测。
这是我的 MainActivity:
package com.gett.wifip2ptest;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.net.wifi.p2p.WifiP2pDevice;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements PeerListListener
private WifiP2pManager mManager;
private Channel mChannel;
private BroadcastReceiver mReceiver;
private IntentFilter mIntentFilter;
private List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
// If the discovery process succeeds and detects peers, the system broadcasts the
// WIFI_P2P_PEERS_CHANGED_ACTION intent.
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener()
@Override
public void onSuccess()
Toast.makeText(MainActivity.this, "Discovery Initiated",
Toast.LENGTH_SHORT).show();
@Override
public void onFailure(int reasonCode)
Toast.makeText(MainActivity.this, "Discovery Failed : " + reasonCode,
Toast.LENGTH_SHORT).show();
);
);
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
/* register the broadcast receiver with the intent values to be matched */
@Override
protected void onResume()
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
/* unregister the broadcast receiver */
@Override
protected void onPause()
super.onPause();
unregisterReceiver(mReceiver);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
/**
* This function is called by requestPeers upon receiving the peers list.
* @param peerList
*/
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList)
peers.clear();
System.out.println("Peers cleared : " + peers);
peers.addAll(peerList.getDeviceList());
System.out.println("Peers : " + peers);
if (peers.size() == 0)
System.out.println("No devices found");
return;
这里是 WiFiDirectBroadCastReceiver:
package com.gett.wifip2ptest;
import android.content.BroadcastReceiver;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.content.Context;
import android.content.Intent;
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver
private WifiP2pManager mManager;
private Channel mChannel;
private MainActivity mActivity;
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
MainActivity activity)
super();
this.mManager = manager;
this.mChannel = channel;
this.mActivity = activity;
@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
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED)
// Wifi P2P is enabled
System.out.println("Wifi Direct has been enabled.");
else
// Wi-Fi P2P is not enabled
System.out.println("Wifi Direct is NOT enabled.");
else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action))
// Call WifiP2pManager.requestPeers() to get a list of current peers
System.out.println("Peers changed.");
// request available peers from the wifi p2p manager. This is an
// asynchronous call and the calling activity is notified with a
// callback on PeerListListener.onPeersAvailable()
if (mManager != null)
mManager.requestPeers(mChannel, (PeerListListener) mActivity);
else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action))
// Respond to new connection or disconnections
else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action))
// Respond to this device's wifi state changing
以下是在其中一个关闭时检测到两个对等点的示例:
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: Peers : [Device: Galaxy J1-2
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: deviceAddress: 2a:27:bf:3a:2e:55
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: primary type: 10-0050F204-5
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: secondary type: null
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: wps: 392
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: grpcapab: 0
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: devcapab: 37
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: status: 3
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: wfdInfo: null
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: groupownerAddress: null
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: GOdeviceName: null
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: interfaceAddress:
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: SConnectInfo : null, Device: Test User (Galaxy J1)
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: deviceAddress: 2a:27:bf:3a:4a:8b
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: primary type: 10-0050F204-5
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: secondary type: null
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: wps: 392
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: grpcapab: 0
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: devcapab: 37
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: status: 3
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: wfdInfo: null
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: groupownerAddress: null
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: GOdeviceName: null
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: interfaceAddress:
02-18 16:31:56.406 20833-20833/com.gett.wifip2ptest I/System.out: SConnectInfo : null]
【问题讨论】:
您最好尝试一下,因为我发现 Android 的 WiFi Direct 的实际行为难以理解。现在,我很好奇如果你强制关闭你的应用程序并重新启动它会发生什么 - 它仍然会发现缺席的对等点吗?这暗示您可能持有一些可以尝试关闭的对象。至于解决您的问题 - 我发现 WifiP2pDeviceList 在测试多个设备时最有用。从列表中迭代对等点并查看 status == WifiP2pDevice.AVAILABLE 【参考方案1】:在 WIFI_P2P_PEERS_CHANGED_ACTION 中试试这个
else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action))
//Log.d("wifi", "WIFI PEERS CHANGED");
mPeers = new ArrayList<WifiP2pDevice>();
mConfigs = new ArrayList<WifiP2pConfig>();
if(mManager != null)
WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener()
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList)
mPeers.clear();
mPeers.addAll(peerList.getDeviceList());
mActivity.displayPeers(peerList);
//mPeers.addAll(peerList.getDeviceList());
for(int i=0; i < peerList.getDeviceList().size(); i++)
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = mPeers.get(i).deviceAddress;
mConfigs.add(config);
;
mManager.requestPeers(mChannel, peerListListener);
MainActivity.java 中的displayPeers()
public void displayPeers(WifiP2pDeviceList peerList)
wifiP2pArrayAdapter.clear();
for(WifiP2pDevice peer: peerList.getDeviceList())
wifiP2pArrayAdapter.add(peer.deviceName + "\n" + peer.deviceAddress);
【讨论】:
【参考方案2】:发生这种情况是因为已关闭 WiFi 直连的设备未向其不再可用的相邻设备发送任何广播。
我尝试对内置 WiFi Direct 屏幕进行相同操作,并获得了与您相同的结果。从内置屏幕发现一些 wifi p2p 设备,然后在发现的设备中关闭 wifi。在系统再次调用 discoverPeers() 之前它不会消失,系统当前设置(在 android 框架中)在 60 秒后调用。
所以禁用的设备会在 60 秒后消失。
这是正常行为,您可能无能为力。
(相反,只要打开 WiFi 直连,设备就会发送一个广播说它在附近可用。这就是为什么新设备会立即出现的原因)。
【讨论】:
【参考方案3】:我遇到了和你一样的问题,这是我的发现:
解释行为的好答案: https://***.com/a/25154034/3260008
在我的代码中,我也测试了该行为,与答案所说的相匹配,如果您调用 discoverPeers()
,则在 60 秒后,将刷新对等列表。对我的应用来说是可以接受的。
【讨论】:
以上是关于WifiP2pManager.requestPeers() 在 onPeersAvailable() 中不断返回旧对等点的主要内容,如果未能解决你的问题,请参考以下文章