gpt4 book ai didi

android - WifiP2pManager.requestPeers() 不断返回 onPeersAvailable() 中的旧节点

转载 作者:太空宇宙 更新时间:2023-11-03 11:04:21 26 4
gpt4 key购买 nike

于是我做了功课,找了类似的问题,居然在两年前找到了一个人有同样的问题... he didn't get any answer though.

问题如下:我正在做来自 android 的 WifiP2p 教程。我在 MainActivity 上使用此 FloatingActionButton 手动调用 discoverPeers()。然后在 WifiDirectBroadcastReceiver 类中广播和接收 WIFI_P2P_PEERS_CHANGED_ACTION。在那里,我调用了 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]

最佳答案

在 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);
}
}

displayPeers() 在 MainActivity.java 中

 public void displayPeers(WifiP2pDeviceList peerList){
wifiP2pArrayAdapter.clear();
for(WifiP2pDevice peer: peerList.getDeviceList()){
wifiP2pArrayAdapter.add(peer.deviceName + "\n" + peer.deviceAddress);
}
}

关于android - WifiP2pManager.requestPeers() 不断返回 onPeersAvailable() 中的旧节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35487193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com