Android WiFi Direct android.net.wifi.p2p.PEERS_CHANGED 不断收到
Android WiFi Direct android.net.wifi.p2p.PEERS_CHANGED received endlessly
社区,
这是我的第一个问题,如果我不小心不符合某些规范或类似规范,请告诉我。
我正在尝试编写与 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 代码。其他一切正常,我没有想法,它从无到有。
确实是这样。基本上,您应该在那里做的是使用您的 WifiP2pManager 实例调用 requestPeers。然后你的 onPeersAvailable 来自 WifiP2pManager.PeerListListener 将被调用,并看到对等点数组,然后你可以在那里检查更改是否是你想要报告的内容。
可以从我的 github projects
中找到参考实现
社区, 这是我的第一个问题,如果我不小心不符合某些规范或类似规范,请告诉我。
我正在尝试编写与 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 代码。其他一切正常,我没有想法,它从无到有。
确实是这样。基本上,您应该在那里做的是使用您的 WifiP2pManager 实例调用 requestPeers。然后你的 onPeersAvailable 来自 WifiP2pManager.PeerListListener 将被调用,并看到对等点数组,然后你可以在那里检查更改是否是你想要报告的内容。
可以从我的 github projects
中找到参考实现