Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop Android WiFi hotspots

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to develop Android WiFi hotspots". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "how to develop Android WiFi hotspots" can help you solve the problem.

Create a hotspot

1. Create hotspots according to the parameters such as encryption type, password, whether hidden or not.

Static WifiConfiguration createWifiConfig (String SSID, @ WifiSecurityType int wifiCipherType, String password, boolean hiddenSSID) {WifiConfiguration wifiConfiguration = new WifiConfiguration (); wifiConfiguration.SSID = convertToQuotedString (SSID); whether wifiConfiguration.hiddenSSID=hiddenSSID;// hides hotspots true= hiding, if hiding requires other devices to manually add network switch (wifiCipherType) {case WifiSecurityType.SECURITY_NONE: wifiConfiguration.allowedKeyManagement.set (WifiConfiguration.KeyMgmt.NONE); break Case WifiSecurityType.SECURITY_WEP: wifiConfiguration.allowedKeyManagement.set (KeyMgmt.NONE); wifiConfiguration.allowedAuthAlgorithms.set (AuthAlgorithm.OPEN); wifiConfiguration.allowedAuthAlgorithms.set (AuthAlgorithm.SHARED); if (! TextUtils.isEmpty (password)) {int length = password.length (); / / WEP-40, WEP-104, and 256-bit WEP (WEP-232?) If ((length = = 10 | length = = 26 | | length = = 58) & & password.matches ("[0-9A-Fa-f] *") {wifiConfiguration.wepKeys [0] = password;} else {wifiConfiguration.wepKeys [0] ='+ password +'";} break Case WifiSecurityType.SECURITY_WPA_PSK: wifiConfiguration.allowedKeyManagement.set (KeyMgmt.WPA_PSK); if (! TextUtils.isEmpty (password)) {if (password.matches ("[0-9A-Fa-f] {64}")) {wifiConfiguration.preSharedKey = password;} else {wifiConfiguration.preSharedKey ='"+ password +'";} break Case WifiSecurityType.SECURITY_WPA_EAP: wifiConfiguration.allowedKeyManagement.set (KeyMgmt.WPA_EAP); wifiConfiguration.allowedKeyManagement.set (KeyMgmt.IEEE8021X); wifiConfiguration.enterpriseConfig = new WifiEnterpriseConfig (); int eapMethod = 0; int phase2Method = 0; wifiConfiguration.enterpriseConfig.setEapMethod (eapMethod); wifiConfiguration.enterpriseConfig.setPhase2Method (phase2Method); if (! TextUtils.isEmpty (password)) {wifiConfiguration.enterpriseConfig.setPassword (password) } break; default: break;} return wifiConfiguration;}

Then call the setWifiApEnabled method of WifiManager to set the wifiConfiguration, because it is hidden and needs to be reflected:

Try {Method method = mWifManager.getClass (). GetMethod ("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE); boolean enable = (Boolean) method.invoke (mWifManager, config, true); if (enable) {Log.d ("WiFi", "hotspot enabled");} else {Log.d ("WiFi", "failed to create hotspot") }} catch (Exception e) {e.printStackTrace ();}

Turn off hotspots

It is easy to turn off the hotspot, which is also done using the above method. The second parameter is false:

Public void closeWifiAp () {try {Method method = mWifiManager.getClass (). GetMethod ("setWifiApEnabled", WifiConfiguration.class, boolean.class); method.invoke (mWifiManager, null, false);} catch (Exception e) {e.printStackTrace ();}}

Monitor hotspot statu

The status of hot spots can be monitored by broadcast:

Public static final String WIFI_AP_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED"

However, this variable is hidden and can only register the broadcast directly with the value:

IntentFilter filter = new IntentFilter (); filter.addAction ("android.net.wifi.WIFI_AP_STATE_CHANGED")

Then get the state in the broadcast:

Int state = intent.getIntExtra ("wifi_state", 0)

Wifi hotspots have the following states:

# WIFI_AP_STATE_DISABLED # WIFI_AP_STATE_DISABLING#WIFI_AP_STATE_ENABLED#WIFI_AP_STATE_ENABLING#WIFI_AP_STATE_FAILED

Other API:

Get the current status of WiFI hotspots. The returned values are the above five states:

Public int getWifiApState ()

Determine if the WiFi hotspot is turned on:

Public boolean isWifiApEnabled ()

Get the WifiConfiguration of the current wifi hotspot:

That's all for public WifiConfiguration getWifiApConfiguration () about "how to develop Android WiFi hotspots". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report