Android Wi-Fi Direct:onPeersAvailable
Posted
技术标签:
【中文标题】Android Wi-Fi Direct:onPeersAvailable【英文标题】:Android Wi-Fi Direct: onPeersAvailable 【发布时间】:2012-10-25 14:24:59 【问题描述】:我正在为 android 开发一个基于 WiFi Direct 的简单应用程序,它必须连接两个设备。为此,我需要调用函数 onPeersAvailable(myPeerListListener),但我不知道如何。
我的应用有这两个元素:
1-主要活动:
package android.nacho.WifiDirect;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class WifiDirect extends Activity
WifiP2pManager mManager;
Channel mChannel;
BroadcastReceiver mReceiver;
IntentFilter mIntentFilter;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi_direct);
//To register the BroadastReceiver
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null); //It was necessary to make a cast (Channel)
mReceiver = new WiFiBroadcastReceiver(mManager, mChannel, this); //, this);
//Layout
final Button btnScan = (Button)findViewById(R.id.btnScan);
final TextView TextDebug=(TextView)findViewById(R.id.TextDebug);
//To define the filter in the BroadcastReceiver
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);
final OnClickListener ScanListener=new OnClickListener() //Sacado de TEstPsycologico
public void onClick(View v)
TextDebug.setText("Se intentan buscar Peers");
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener()
public void onSuccess()
TextDebug.setText("Ha habido éxito buscando Peers");//DEBUG: Para ver si es posible encontrar Peers
public void onFailure(int reasonCode)
TextDebug.setText("Algo ha salido mal buscando Peers"); //DEBUG: Para ver si pasó algo raro busando Peers
);
onPeersAvailable(myPeerListListener);
;
btnScan.setOnClickListener(ScanListener);
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.activity_wifi_direct, menu);
return true;
//
@Override
protected void onResume()
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
// unregister the broadcast receiver
@Override
protected void onPause()
super.onPause();
unregisterReceiver(mReceiver);
2 类广播接收器:
package android.nacho.WifiDirect;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pManager;
import android.widget.Toast;
/**
* A BroadcastReceiver that notifies of important Wi-Fi p2p events.
*/
public class WiFiBroadcastReceiver extends BroadcastReceiver
private WifiP2pManager manager;
private Channel channel;
private WifiDirect activity;
private PeerListListener myPeerListListener;
//For toast, add also context
//private Context context;
public WiFiBroadcastReceiver(WifiP2pManager manager, Channel channel, WifiDirect activity)//, Context context)
super();
this.manager = manager;
this.channel = channel;
this.activity = activity;
// this.context= context;
@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)
//Toast.makeText(context, "Wi-Fi Direct is enable", Toast.LENGTH_LONG).show();
else
//Toast.makeText(context, "Wi-Fi Direct is not enable", Toast.LENGTH_LONG).show();
else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action))
// Call WifiP2pManager.requestPeers() to get a list of current peers
// 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 (manager != null)
manager.requestPeers(channel, myPeerListListener);
manager.onPeersAvailable(myPeerListListener);
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
到目前为止,我的代码应该能够检测到运行应用程序的设备周围的对等点,该 ID 应该存储在变量 myPeerListListener 中。所有这一切都发生在 BroadcastReceiver 方法的第二个意图 OnReceiv() 中,通过调用:
manager.requestPeers(channel, myPeerListListener);
现在我想操作该列表。因此,按照它应该调用 requestPeers 的 API 信息,您可以在此处查看 API:
http://developer.android.com/guide/topics/connectivity/wifip2p.html
*部分发现同行
所以我尝试在下面写一个调用:
manager.onPeersAvailable(myPeerListListener);
但我收到此错误:
未定义类型 WifiP2pManager 的 onPeersAvailable(WifiP2pManager.PeerListListener) 方法
谁能告诉我如何正确地将 PeerListListener 发送到主要活动?
非常感谢您的宝贵时间
【问题讨论】:
【参考方案1】:dtheo 已回答并指向关于此主题的不错的 Android 教程。我将仅对 OP 的代码进行更改,以展示如何实现所请求的功能。
根据Android官方指南here,wifip2p发现api应该这样使用:
在您的 MainActivity 类中:
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener()
public void onSuccess()
TextDebug.setText("Ha habido éxito buscando Peers");
public void onFailure(int reasonCode)
TextDebug.setText("Algo ha salido mal buscando Peers");
);
// ############# DELETE THE FOLLOWING LINE #############
onPeersAvailable(myPeerListListener); // <<<<<<< DELETE THIS LINE
在您的 BroadcastReceiver 类中,改为执行以下操作:
@Override
public void onReceive(Context context, Intent intent)
...
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action))
...
else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action))
if (manager != null)
manager.requestPeers(channel, new WifiP2pManager.PeerListListener()
@Override
public void onPeersAvailable(WifiP2pDeviceList peers)
Log.d(TAG,String.format("PeerListListener: %d peers available, updating device list", peers.getDeviceList().size()));
// DO WHATEVER YOU WANT HERE
// YOU CAN GET ACCESS TO ALL THE DEVICES YOU FOUND FROM peers OBJECT
);
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
【讨论】:
谢谢张。节省了我的时间。干杯。【参考方案2】:dtheo 已经回答了这个问题。
我想补充一点:对于 Wi-Fi Direct,两个设备需要同时扫描才能发现对方。还有一个扫描超时,这意味着一段时间后可能会失去可发现性。
【讨论】:
【参考方案3】:您应该在 PeerListListener 中实现 onPeersAvailable 方法。
请查看http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html了解更多信息。
【讨论】:
以上是关于Android Wi-Fi Direct:onPeersAvailable的主要内容,如果未能解决你的问题,请参考以下文章
32 - 如何克服 Android 中 Wi-Fi Direct 32 对凭据的限制?
32 - 如何克服 Android 中 Wi-Fi Direct 32 对凭据的限制?
Android Wi-Fi Direct P2P 连接多个设备
使用 android Wi-Fi Direct API 的问题:mChannel = mManager.initialize()