Android Wi-Fi Direct:onPeersAvailable

Android Wi-Fi Direct:onPeersAvailable,第1张

概述我正在开发一个基于WiFi Direct for Android的简单应用程序,它必须连接两个设备.为此,我需要调用funciónonPeersAvailable(myPeerListListener),但我不知道如何. 我的应用程序有这两个元素: 1主要活动: package android.nacho.WifiDirect;import android.net.wifi.p2p.Wif 我正在开发一个基于WiFi Direct for Android的简单应用程序,它必须连接两个设备.为此,我需要调用funciónonPeersAvailable(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类broadcastReceiver:

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中.所有这些都发生在名为OnReceiv()的broadcastReceiver方法的第二个意图中,通过调用:

manager.requestPeers(channel,myPeerListListener);

现在我想 *** 纵那个列表.因此,根据API信息,它应该调用requestPeers,您可以在此处查看API:

http://developer.android.com/guide/topics/connectivity/wifip2p.html

*发现同行的部分

所以我试过的是在下面打电话给:

manager.onPeersAvailable(myPeerListListener);

但我得到这个错误:

对于wifip2pManager类型,未定义onPeersAvailable(wifip2pManager.PeerListListener)方法

谁能告诉我怎样才能将PeerListListener正确发送到主活动?

非常感谢您的宝贵时间

解决方法 您应该在PeerListListener中实现onPeersAvailable方法.

有关更多信息,请参阅http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html.

总结

以上是内存溢出为你收集整理的Android Wi-Fi Direct:onPeersAvailable全部内容,希望文章能够帮你解决Android Wi-Fi Direct:onPeersAvailable所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1138119.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存