Search code examples
androidwifiwifi-directwifimanagerwifip2p

I can't connect to the access point made by WI-FI DIRECT via WIFI


I created an access point with android WI-FI P2P APIs and i want to connect to it with another phone using WIFI(not wifi direct) and the following code :

WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; 
conf.preSharedKey = "\""+ networkPass +"\"";
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
    if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
         wifiManager.disconnect();
         wifiManager.enableNetwork(i.networkId, true);
         wifiManager.reconnect();               

         break;
    }           
 }

and with the following permissions :

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

but it doesn't connect to that access point.
but sumsung wifi manager connects to it easily.
i tried many libraries to connect , but the result was same.
i thought that maybe because the access point does not have an internet connection i can't connect to it.
please help me i need to solve this issue for my application. tnks


Solution

  • i fixed the issue and i want to share the solution with you.
    i used WIFI DIRECT API to discover the access point and retrieve it's info like password to connect it but i realized that when you are using WIFI DIRECT API to discover services , you can't connect to an access point at the same time with the WIFI (i think it's bug).
    so you should stop the service discovery first and then connect to the access point.
    i used the following code :

    public void connectToAccessPoint(GroupAccessPoint groupAccessPoint) {
        stopServiceDiscovery(new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                connect(groupAccessPoint);
            }
    
            @Override
            public void onFailure(int reason) {
                connect(groupAccessPoint);
            }
        });
    }
    
    public void stopServiceDiscovery(@Nullable WifiP2pManager.ActionListener stopDiscoveryListener) {
        if (wifiP2pManager == null) return;
        wifiP2pManager.clearServiceRequests(channel, new DefaultActionListener(actionListener, Action.CANCEL_DISCOVERING));
        wifiP2pManager.setDnsSdResponseListeners(channel, null, null);
        wifiP2pManager.stopPeerDiscovery(channel, stopDiscoveryListener);
    }
    
    private void connect(GroupAccessPoint groupAccessPoint) {
        WifiUtils.withContext(activity.getApplicationContext())
                .connectWith(groupAccessPoint.ssid, groupAccessPoint.password)
                .setTimeout(12000)
                .onConnectionResult(new ConnectionSuccessListener() {
                    @Override
                    public void success() {
                        actionListener.onSuccess(Action.JOIN_GROUP);
                    }
    
                    @Override
                    public void failed(@NonNull ConnectionErrorCode errorCode) {
                        actionListener.onFailure(Action.JOIN_GROUP);
                    }
                }).start();
    }