我正在尝试使用Nexus 4连接到WiFi Direct网络。
我有以下代码:
public class MainActivity extends ActionBarActivity {
WifiP2pManager mWifiP2pManager;
Channel mChannel;
WiFiDirectBroadcastReceiver mReceiver;
IntentFilter mIntentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
mWifiP2pManager = (WifiP2pManager)getSystemService(WIFI_P2P_SERVICE);
mChannel = mWifiP2pManager.initialize(this, getMainLooper(), null);
((ImageButton)findViewById(R.id.imageButton_power)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("p2p", "discoverPeers() called");
mWifiP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d("p2p", "discoverPeers() Success");
}
@Override
public void onFailure(int reason) {
Log.d("p2p", "discoverPeers() Failure: " + reason);
}
});
}
});
}
@Override
protected void onResume() {
super.onResume();
mReceiver = new WiFiDirectBroadcastReceiver(mWifiP2pManager, mChannel, this);
registerReceiver(mReceiver, mIntentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
}
和BroadcastReceiver:
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();
Log.d("p2p", "Action: " + action);
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)) {
Log.d("p2p", "Requesting for peers");
if (mManager != null) {
mManager.requestPeers(mChannel, peerListListener);
}
} 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
}
}
private WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peers) {
Log.d("p2p", "deviceCount = " + peers.getDeviceList().size());
}
};
}
当我点击按钮时,discoverPeers()
会返回成功。但是,我从未获得IntentFilter中指定的任何广播,当我开始/恢复活动时,我会收到一些广播:
02-26 16:19:08.684 7656-7656 / com.example.app D / p2p:行动: android.net.wifi.p2p.STATE_CHANGED 02-26 16:19:08.684
7656-7656 / com.example.app D / p2p:行动: android.net.wifi.p2p.CONNECTION_STATE_CHANGE 02-26 16:19:08.684
7656-7656 / com.example.app D / p2p:行动: android.net.wifi.p2p.THIS_DEVICE_CHANGED
但是没有一个是我在IntentFilter上注册的那个,由于某种原因我得到了那些3,尽管它们没有添加到过滤器中。
修改 似乎我对字符串的名称感到困惑,并且它们是实际值,我确实收到了一些我添加到IntentFilter的操作。
答案 0 :(得分:1)
然后你实际上得到了正确的结果,尽管它们并不像你预期的那样。当您进行p2p状态更改时,您应该看到“android.net.wifi.p2p.CONNECTION_STATE_CHANGE”值。其他过滤器也是如此。查看此页面:http://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.html#WIFI_P2P_CONNECTION_CHANGED_ACTION
答案 1 :(得分:0)
不确定您是否仍然关注此事,但Google已在WifiP2p参考中添加了一条说明,可以解释此问题。 (我遇到的一个问题) 在此处找到:http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html
// Code for when the discovery initiation is successful goes here.
// No services have actually been discovered yet, so this method
// can often be left blank. Code for peer discovery goes in the
// onReceive method, detailed below.
从这个注释看来,如果discoverPeers()成功,你将获得onSuccess()指示但是你只会收到'WIFI_P2P_PEERS_CHANGED_ACTION'广播,表明如果有可用的对等点,可用对等列表已经改变检测。我用两个运行wifiP2p应用程序的智能手机测试了这个。
我希望这有帮助,如果至少你可能会在google引用中找到答案。