Android WiFi Direct android.net.wifi.p2p.PEERS_CHANGED 收到无休止
Posted
技术标签:
【中文标题】Android WiFi Direct android.net.wifi.p2p.PEERS_CHANGED 收到无休止【英文标题】:Android WiFi Direct android.net.wifi.p2p.PEERS_CHANGED received endlessly 【发布时间】:2016-01-06 23:36:37 【问题描述】:社区, 这是我的第一个问题,如果我不小心不符合某些规范或类似规范,请告诉我。
我正在尝试编写一个与 WiFi 直接通信的 android 应用程序。 此时一切正常,但我的 BroadcastReceiver 一次又一次地收到 android.net.wifi.p2p.PEERS_CHANGED 操作。
public class WifiP2PBroadcastReceiver extends BroadcastReceiver
private final static String TAG = "WifiP2PBR";
private WifiP2pManager _manager;
private WifiP2pManager.Channel _channel;
private Activity _callbackActivity;
private static ArrayList<WifiBroadcastCallback> _arrayList_callbacks;
public WifiP2PBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel, Activity callback)
_manager = manager;
_channel = channel;
_callbackActivity = callback;
/**
* add a callback to the receiver
*
* @param callback
*/
public static void addWifiCallback(WifiBroadcastCallback callback)
if(!getCallbacksList().contains(callback))
getCallbacksList().add(callback);
public void registerReceiver()
Log.d(TAG,"registerReceiver called");
if(_callbackActivity != null)
_callbackActivity.registerReceiver(this, getIntentFilter());
public void unregisterReceiver()
if(_callbackActivity != null)
_callbackActivity.unregisterReceiver(this);
@Override
public final void onReceive(Context context, Intent intent)
Log.d(TAG, "action: "+intent.getAction());
switch(intent.getAction())
case WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION:
Log.d(TAG, "state changed action");
break;
case WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION:
Log.d(TAG, "peers changed action");
notifyPeerUpdate();
break;
case WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION:
Log.d(TAG, "wifi p2p connection changed action");
NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
Log.d(TAG,"network info available");
break;
case WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION:
Log.d(TAG, "wifi p2p this device changed action");
notifyDeviceUpdate();
WifiP2pDevice thisDevice = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
Log.d(TAG,"this device acquired");
break;
private void notifyPeerUpdate()
for(WifiBroadcastCallback c : getCallbacksList())
c.onPeersChanged();
private void notifyDeviceUpdate()
for(WifiBroadcastCallback c : getCallbacksList())
c.onDeviceChanged();
/**
* get array list of callbacks
*
* @return
*/
private static ArrayList<WifiBroadcastCallback> getCallbacksList()
if(_arrayList_callbacks == null)
_arrayList_callbacks = new ArrayList<WifiBroadcastCallback>();
return _arrayList_callbacks;
/**
* interface for callbacks
*/
public interface WifiBroadcastCallback
void onDeviceChanged();
void onPeersChanged();
private IntentFilter getIntentFilter()
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
return intentFilter;
这是我的 BroadcastReceiver 代码。其他一切都很好,我没有想法,它从无处开始。
【问题讨论】:
【参考方案1】:确实如此。基本上,你应该在那里做的是用你的 WifiP2pManager 实例调用 requestPeers。然后,您将调用来自 WifiP2pManager.PeerListListener 的 onPeersAvailable 并使用看到的对等点数组,然后您可以在那里检查更改是否是您想要报告的内容。
参考实现可以在我的github projects找到
【讨论】:
好的,所以可以肯定的是,每隔几秒就更改一次 PEERS 是正常的,我应该通过查看它是否为我改变了什么来处理它?编辑:感谢您的快速回答和参考!以上是关于Android WiFi Direct android.net.wifi.p2p.PEERS_CHANGED 收到无休止的主要内容,如果未能解决你的问题,请参考以下文章
Android:如何检查设备是不是通过 WiFi - Direct 连接到另一台设备?
WIFI_P2P_CONNECTION_CHANGED_ACTION, Wifi Direct, Android
Android 同时运行 Wifi Lan 和 Wifi Direct