register BroadcastReceiver in onResume () and override it. don't forget to undo it in onPause ()
private class WiFiDirectBroadcastReceiver extends android.content.BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
then try calling openPeers () first
mWifiP2pManager.discoverPeers(Channel mChannel, ActionListener mActionListener);
if findPeers () finds peers, the WIFI_P2P_PEERS_CHANGED_ACTION action will be triggered.
we can call requestPeers () in WIFI_P2P_PEERS_CHANGED_ACTION in BroadcastReceiver
mWifiP2pManager.requestPeers(Channel mChannel, WifiP2pManager.PeerListListener);
so our BroadcastReceiver now looks like this
private class WiFiDirectBroadcastReceiver extends android.content.BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
to implement WifiP2pManager.PeerListListener you need to override onPeersAvailable (WifiP2pDeviceList nodes)
in onPeersAvailable (), the wifiP2pDeviceList parameter means the peers you discovered
we need a UI object so that we can choose which device to connect, so I use spinner here.
also you can use listView or something else.
private List<WifiP2pDevice> mPeers = new ArrayList<WifiP2pDevice>(); spinnerAdapter = new WiFiPeerListAdapter(this, R.layout.row_devices, mPeers); ... @Override public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) { mPeers.clear(); mPeers.addAll(wifiP2pDeviceList.getDeviceList()); spinnerAdapter.notifyDataSetChanged(); }
Finally, we can connect to the device
WifiP2pDevice device = spinnerAdapter.getItem((int) mSpinner.getSelectedItemId()); WifiP2pConfig config = new WifiP2pConfig(); config.deviceAddress = device.deviceAddress; config.wps.setup = WpsInfo.PBC; mWifiP2pManager.connect(mChannel, config, mActionListener);
after connecting two devices, the BroadcastReceiver WIFI_P2P_CONNECTION_CHANGED_ACTION action will be activated. so we can do something here.
our BroadcastReceiver now looks like
private class WiFiDirectBroadcastReceiver extends android.content.BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
btw, the action in the WIFI_P2P_CONNECTION_CHANGED_ACTION log will get something like this
NetworkInfo: type: WIFI_P2P[], state: UNKNOWN/IDLE, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, simId: 0
now we need to implement WifiP2pManager.ConnectionInfoListener and override its abstract onConnectionInfoAvailable method (WifiP2pInfo information) for requestConnectionInfo ()
private WifiP2pInfo p2pInfo; @Override public void onConnectionInfoAvailable(final WifiP2pInfo info) { p2pInfo = info; mWifiP2pManager.requestGroupInfo(mChannel, WifiP2pManager.GroupInfoListener); }
again we need to implement WifiP2pManager.GroupInfoListener and override onGroupInfoAvailable (WifiP2pGroup group)
@Override public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) { String log; if(wifiP2pGroup.isGroupOwner()) { log = "I am GO"; }else{ log = "I am not GO"; } Log.d(TAG, log); }
now we almost got every information about these two devices
enjoy it