我在我的 android 应用程序中使用 Android Wifi P2P 技术在多个设备之间发送简单的数据传输。我正在阅读 Wifi p2p 的开发人员指南,但该实现似乎只是为使用 Wifi 直接服务的一项活动而构建的。
按照指南,我创建了这个 BroadcastReceiver 类来监听 Wifi Direct 事件:
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager manager;
private Channel channel;
private WifiActivity activity;
public WifiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, WifiActivity activity){
this.manager = manager;
this.channel = channel;
this.activity = 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
}else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){
// Call WifiP2pManager.requestPeers() to get a list of current peers
}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
}
}
}
但是,构造函数似乎只适用于一项活动。我是否必须为每个使用 Wifi Direct 的活动制作接收器?我如何将连接传递给不同的活动,而不必每次都建立连接(即查找对等点、连接到对等点等)?