Connect two devices via Wi-Fi Direct

I am trying to connect 2 Android devices via Wi-fi Direct. In my application, I hardcode the MAC address of another device and call the connect method. I assume Wi-Fi Direct is enabled on both devices. Here is the code I'm using:

com.abc package

import android.app.Activity; import android.content.Context; import android.content.IntentFilter; import android.net.wifi.WpsInfo; import android.net.wifi.p2p.WifiP2pConfig; import android.net.wifi.p2p.WifiP2pManager; import android.net.wifi.p2p.WifiP2pManager.Channel; import android.os.Bundle; import android.widget.Toast; public class WiFiDirectActivity extends Activity { /** Called when the activity is first created. */ protected WifiP2pManager manager; protected Channel channel; public WifiP2pConfig config ; protected final IntentFilter intentFilter = new IntentFilter(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); intentFilter.addAction (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); intentFilter .addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); channel = manager.initialize(this, this.getMainLooper(), null); config = new WifiP2pConfig(); config.deviceAddress = "78:d6:f0:ab:d9:da"; config.groupOwnerIntent = 0; config.wps.setup = WpsInfo.PBC; manager.connect(channel, config, new WifiP2pManager.ActionListener(){ @Override public void onSuccess() { Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG); } @Override public void onFailure(int reason) { Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG); } }); } 

}

but this is not a connection. What is wrong with my implementation?

+4
source share
2 answers

The similar code works for me, the main differences:

  • I get the address of the device that you called before the peers of discovery (if you do, you need to add the WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION group to the intent group)
  • I do not install config.groupOwnerIntent

 WifiP2pConfig config = new WifiP2pConfig(); config.deviceAddress = this.address; config.wps.setup = WpsInfo.PBC; 
+2
source

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)) { //TODO } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { //TODO } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { //TODO } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { //TODO } } } 

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)) { //TODO } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { mWifiP2pManager.requestPeers(mChannel , pl); } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { //TODO } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { //TODO } } } 

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)) { //TODO } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { mWifiP2pManager.requestPeers(mChannel , pl); } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); if (networkInfo != null) { Log.d(TAG,networkInfo.toString()); if (networkInfo.isConnected()) { mWifiP2pManager.requestConnectionInfo(mChannel, WifiP2pManager.ConnectionInfoListener); } } } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { //TODO } } } 

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

+1
source

Source: https://habr.com/ru/post/1414696/


All Articles