Get Ssid When Wifi Is Connected

Get SSID when WIFI is connected

I listen for WifiManager.NETWORK_STATE_CHANGED_ACTION in a broadcast receiver

if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
NetworkInfo netInfo = intent.getParcelableExtra (WifiManager.EXTRA_NETWORK_INFO);
if (ConnectivityManager.TYPE_WIFI == netInfo.getType ()) {
...
}
}

I check for netInfo.isConnected(). Then I am able to use

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();

UPDATE

From android 8.0 onwards we wont be getting SSID of the connected network unless location services are enabled and your app has the permission to access it.

How to get WIFI SSID in Android 10.0?

It want be get.check original post

From android 8.0 onwards we wont be getting SSID of the connected
network unless GPS is turned on.

Get SSID when WIFI is connected

WifiManager mWifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
assert mWifiManager != null;
WifiInfo info = mWifiManager.getConnectionInfo();
return info.getSSID();

How to get SSID of currently connected Wifi Network in swift | iOS 14?

I have sorted out the way to get SSID of currently connected Wifi. Following are the pre-requisites to follow before writing the code.

-> You must have a paid developer account.

-> You must have a physical Device

-> You must enable Wifi-Entitlement by going to Target->Signing & Capabilities and adding Access WiFi Information or adding
<key>com.apple.developer.networking.wifi-info</key> <true/> directly to your entitlements file.

-> Allow location usage access from user

Then use this code in your class to get SSID.

import UIKit
import SystemConfiguration.CaptiveNetwork
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

var locationManager = CLLocationManager()
var currentNetworkInfos: Array<NetworkInfo>? {
get {
return SSID.fetchNetworkInfo()
}
}


let ssidLabel:UILabel = {

let lbl = UILabel()
lbl.translatesAutoresizingMaskIntoConstraints = false
return lbl

}()

let bssidLabel:UILabel = {

let lbl = UILabel()
lbl.translatesAutoresizingMaskIntoConstraints = false
return lbl

}()

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
view.addSubview(ssidLabel)
view.addSubview(bssidLabel)

NSLayoutConstraint.activate([

ssidLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
ssidLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),

bssidLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0),
bssidLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 20),
])

if #available(iOS 13.0, *) {
let status = CLLocationManager.authorizationStatus()
if status == .authorizedWhenInUse {
updateWiFi()
} else {
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
} else {
updateWiFi()
}
}

func updateWiFi() {
print("SSID: \(currentNetworkInfos?.first?.ssid ?? "")")

if let ssid = currentNetworkInfos?.first?.ssid {
ssidLabel.text = "SSID: \(ssid)"
}

if let bssid = currentNetworkInfos?.first?.bssid {
bssidLabel.text = "BSSID: \(bssid)"
}

}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
updateWiFi()
}
}

}

public class SSID {
class func fetchNetworkInfo() -> [NetworkInfo]? {
if let interfaces: NSArray = CNCopySupportedInterfaces() {
var networkInfos = [NetworkInfo]()
for interface in interfaces {
let interfaceName = interface as! String
var networkInfo = NetworkInfo(interface: interfaceName,
success: false,
ssid: nil,
bssid: nil)
if let dict = CNCopyCurrentNetworkInfo(interfaceName as CFString) as NSDictionary? {
networkInfo.success = true
networkInfo.ssid = dict[kCNNetworkInfoKeySSID as String] as? String
networkInfo.bssid = dict[kCNNetworkInfoKeyBSSID as String] as? String
}
networkInfos.append(networkInfo)
}
return networkInfos
}
return nil
}
}

struct NetworkInfo {
var interface: String
var success: Bool = false
var ssid: String?
var bssid: String?
}

How to get the wifi name(SSID) of the currently connected wifi in Flutter

It's just calling getWifiName(), available in the network_info_plus plugin. This method used to be available in the connectivity plugin, but it has been moved to this new plugin later.

In iOS, using this solution requires the steps described in this answer.

How to get current wifi connection name in android pie(9) devices?

This is related to permissions....since API level 27 you need either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission. You may also need CHANGE_WIFI_STATE for Android 9 (that's the case for wifi scan anyway as per google permisson model

then try this code

   ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
wifiInfo.getSSID();
String name = networkInfo.getExtraInfo();
String ssid = "\"" + wifiInfo.getSSID() + "\"";
}


Related Topics



Leave a reply



Submit