Android

WifiNetworkSuggestion Example

Darane 2021. 9. 2. 15:07

 

WifiNetworkSuggestion is an API that suggests Wi-Fi networks to users. Available for Android10(API level 29) or higher. You can add the information of the Wi-Fi network implemented by Application and recommend it to users who use the Application.

WifiManager.addNetworkSuggestions() API is for registering Wi-Fi networks to be proposed to users. When calling the API, it does not immediately generate a notification suggesting a Wi-Fi network.

The added Wi-Fi network is notified to users by notification when it is first discovered through Google Nearby(a platform for finding and communicating with peripheral devices).


1. Example
: Add 2 Wi-Fi Access Points(Networks)

WifiNetworkSuggestion suggestionOpen = new WifiNetworkSuggestion.Builder()
    .setSsid("freewifi") //SSID name
    .build(); 

WifiNetworkSuggestion suggestionWpa2 = new WifiNetworkSuggestion.Builder()
    .setSsid("iptime") //SSID name
    .setWpa2Passphrase("12345678") //password
    .build(); 

List<WifiNetworkSuggestion> networkSuggestions = new ArrayList<>(); 
networkSuggestions.add(suggestionOpen); networkSuggestions.add(suggestionWpa2);
 
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); 
wifiManager.addNetworkSuggestions(networkSuggestions); 

int status = wifiManager.addNetworkSuggestions(networkSuggestions)

 

반응형


Broadcast
: When the added Wi-Fi is connected, it receives broadcast.

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
        if (WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION.equals(intent.getAction())) { 
            //Processing after Wi-Fi connection. 
        }
    }
}; 

IntentFilter intentFilter = new IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION) 
getApplicationContext().registerReceiver(broadcastReceiver, intentFilter);


WifiManager.removeNetworkSuggestions()
: Delete added Wi-Fi

List<WifiNetworkSuggestion> networkSuggestions = new ArrayList<>(); 
networkSuggestions.add(suggestionOpen);
networkSuggestions.add(suggestionWpa2); 

int status = wifiManager.removeNetworkSuggestions(networkSuggestions);


Required permission

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

 

반응형