连接 WiFi p2p android, WIFI_P2P_CONNECTION_CHANGED_ACTION
Posted
技术标签:
【中文标题】连接 WiFi p2p android, WIFI_P2P_CONNECTION_CHANGED_ACTION【英文标题】:connection WiFi p2p android, WIFI_P2P_CONNECTION_CHANGED_ACTION 【发布时间】:2016-12-15 10:36:08 【问题描述】:我发现了对等点,但我无法连接到其中之一。我觉得我的 BroadcastReceiver 没有得到 WIFI_P2P_CONNECTION_CHANGED_ACTION 因为它不执行里面的内容。
连接():
public void connect(View v)
// Picking the first device found on the network.
WifiP2pDevice device = (WifiP2pDevice) peers.get(0);
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
config.wps.setup = WpsInfo.PBC;
config.groupOwnerIntent = 0;
mManager.connect(mChannel, config, new WifiP2pManager.ActionListener()
@Override
public void onSuccess()
// WiFiDirectBroadcastReceiver will notify us. Ignore for now.
Toast.makeText(MultiActivity.this, "Connect initiated" ,
Toast.LENGTH_SHORT).show();
@Override
public void onFailure(int reason)
Toast.makeText(MultiActivity.this, "Connect failed. Retry.",
Toast.LENGTH_SHORT).show();
);
onReceive():
else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action))
// Connection state changed! We should probably do something about
// that.
Toast.makeText(activity, "ca marche",
Toast.LENGTH_SHORT).show();
if (mManager == null)
return;
NetworkInfo networkInfo = (NetworkInfo) intent
.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected())
// We are connected with the other device, request connection
// info to find group owner IP
mManager.requestConnectionInfo(mChannel, connectionListener);
Toast "ca marche" 永远不会出现在屏幕上。 请帮帮我,谢谢。
【问题讨论】:
【参考方案1】:确保使用正确的 IntentFilter 注册您的广播接收器。
例如:
-
创建新的意图过滤器:
IntentFilter tmpFilter = new IntentFilter();
添加您感兴趣的操作:tmpFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
向广播接收器注册(最好使用 onResume()):registerReceiver(p2PBroadcastReceiver, tmpFilter)
你可以在这里找到详细的例子: https://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html
祝你好运。
【讨论】:
我们可以在清单中静态注册的接收器上使用 wifi p2p 事件吗?我已在清单中注册,但没有任何内容被识别。【参考方案2】:public void connect(WifiP2pConfig config)
manager.connect(channel, config, new WifiP2pManager.ActionListener()
@Override
public void onSuccess()
// WiFiDirectBroadcastReceiver will notify us. Ignore for now.
peerListener.connectionSuccess();
@Override
public void onFailure(int reason)
peerListener.connectionFailure();
Toast.makeText(activity, "Connect failed. Retry.", Toast.LENGTH_SHORT).show();
);
【讨论】:
以上是关于连接 WiFi p2p android, WIFI_P2P_CONNECTION_CHANGED_ACTION的主要内容,如果未能解决你的问题,请参考以下文章
通过Wifi直接(P2P)的android到android实时视频流[关闭]
Android WifiDisplay分析二:Wifi display连接过程
Android WiFi Direct android.net.wifi.p2p.PEERS_CHANGED 收到无休止