- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
于是我做了功课,找了类似的问题,居然在两年前找到了一个人有同样的问题... he didn't get any answer though.
问题如下:我正在做来自 android 的 WifiP2p 教程。我在 MainActivity 上使用此 FloatingActionButton
手动调用 discoverPeers()
。然后在 WifiDirectBroadcastReceiver 类中广播和接收 WIFI_P2P_PEERS_CHANGED_ACTION
。在那里,我调用了 requestPeers()
,如果有至少一个可发现的点,它又会调用 onPeersAvailable()
>(这很有道理。)
然而,问题是一旦它发现了一个点,它就永远不会离开列表。即使我关闭检测到的设备。让我用一个例子来说明:
设备角色的任何排列都会发生这种情况。所以问题确实出在代码中,因为它们在官方 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/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!