我正在開發基於WiFi Direct的簡單應用程序,該應用程序必須連接兩個設備。爲此,我需要調用onPeersAvailable(myPeerListListener)的función,但我不知道如何。Android Wi-Fi Direct:onPeers Available
我的應用程序有這兩個元素:
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。所有這一切都在第二個發生打算叫OnReceiv()的廣播接收器的方法,通過調用:
manager.requestPeers(channel, myPeerListListener);
現在我想操縱該列表。所以下面應該調用requestPeers API的信息,你可以看到這裏的API:
http://developer.android.com/guide/topics/connectivity/wifip2p.html
*第情迷同行
所以,我曾嘗試是寫一個呼叫的下方, :
manager.onPeersAvailable(myPeerListListener);
,但我得到這個錯誤:
該方法onPeersAvailable(WifiP2pManager.PeerListListener)未定義的類型WifiP2pManager
任何人都可以告訴我怎麼能將PeerListListener正確地發送到主要活動?
非常感謝您的寶貴時間
Thx Zhang。爲我節省了時間。乾杯。 – greenspand