How to get a device that can connect to wifi direct on android

97 Views Asked by At

I would like to retrieve information about devices capable of wifi direct connection to Android.

First, click Settings->Connections->wifi->wifi direct on your Android device and you will see devices that can be connected.

Now when I click the button in my app, the devices that can connect to the wifi dircet should be shown in the console as Log.d();.

However, contrary to my wishes, the number of devices is returned as 0, so information about the device cannot be retrieved.

public class MainActivity extends AppCompatActivity {

    private WifiP2pManager wifiP2pManager;
    private Channel channel;
    private BroadcastReceiver wifiDirectReceiver;
    private Button searchButton;

    private Handler handler = new Handler();
    private static final long SEARCH_INTERVAL = 30000;


    private Runnable searchRunnable = new Runnable() {
        @Override
        public void run() {
            discoverPeers();
            handler.postDelayed(this, SEARCH_INTERVAL);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        channel = wifiP2pManager.initialize(this, getMainLooper(), null);

        wifiDirectReceiver = new WiFiDirectReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        registerReceiver(wifiDirectReceiver, intentFilter);

        searchButton = findViewById(R.id.btn_print);
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
          
                Log.d("MyApp", "clicked button");
                startDiscovery();
            }
        });
    }


    private void startDiscovery() {
      
        handler.removeCallbacks(searchRunnable); 
        handler.post(searchRunnable); 
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(wifiDirectReceiver);
    }

    private void discoverPeers() {
        wifiP2pManager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                
            }

            @Override
            public void onFailure(int reasonCode) {
                
            }
        });
    }

    private class WiFiDirectReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
               
                wifiP2pManager.requestPeers(channel, new WifiP2pManager.PeerListListener() {
                    @Override
                    public void onPeersAvailable(WifiP2pDeviceList peers) {
                        Log.d("WiFiDirect", "peers : " + peers.getDeviceList().size());
                        for (WifiP2pDevice device : peers.getDeviceList()) {
                            
                            Log.d("WiFiDirect", "Device name: " + device.deviceName);
                            Log.d("WiFiDirect", "Device address: " + device.deviceAddress);
                           
                        }
                    }
                });
            }
        }
    }
}
0

There are 0 best solutions below