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"/>
'Android' 카테고리의 다른 글
[Android] logcat chatty 로그가 제대로 출력되지 않을 때 expire 1 line (0) | 2021.09.07 |
---|---|
Android adb command list (0) | 2021.09.07 |
Android Annotation Summary (0) | 2021.09.07 |
List up Android version, SDK version, API level (0) | 2021.09.03 |
Android 버전, SDK 버전, API 수준 정리 (0) | 2020.07.27 |
Android adb 명령어 정리 (0) | 2020.05.19 |
Android Annotation 정리 (0) | 2020.04.23 |
WifiNetworkSuggestion 구현 예제 (0) | 2020.04.20 |