Skip to content

Commit

Permalink
fix(ios): getSSID and getBSSID always returning null (#175)
Browse files Browse the repository at this point in the history
* Fix getSSID on iOS

* Apply getSSID fix to getBSSID
  • Loading branch information
DominikStarke authored Sep 19, 2021
1 parent f0f7813 commit 089b84b
Showing 1 changed file with 54 additions and 53 deletions.
107 changes: 54 additions & 53 deletions ios/Classes/SwiftWifiIotPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ public class SwiftWifiIotPlugin: NSObject, FlutterPlugin {
disconnect(result: result)
break;
case "getSSID":
result(getSSID())
getSSID { (sSSID) in
result(sSSID)
}
break;
case "getBSSID":
result(getBSSID())
getBSSID { (bSSID) in
result(bSSID)
}
break;
case "getCurrentSignalStrength":
getCurrentSignalStrength(result: result)
Expand Down Expand Up @@ -134,32 +138,28 @@ public class SwiftWifiIotPlugin: NSObject, FlutterPlugin {
configuration.joinOnce = bJoinOnce ?? false

NEHotspotConfigurationManager.shared.apply(configuration) { [weak self] (error) in
if (error != nil) {
if (error?.localizedDescription == "already associated.") {
if let this = self, let ssid = this.getSSID() {
print("Connected to " + ssid)
}
result(true)
} else {
print("Not Connected")
result(false)
}
guard let this = self else {
print("WiFi network not found")
result(false)
return
} else {
guard let this = self else {
print("WiFi network not found")
result(false)
return
}
if let ssid = this.getSSID() {
}
this.getSSID { (sSSID) -> () in
if (error != nil) {
if (error?.localizedDescription == "already associated.") {
print("Connected to '\(sSSID ?? "<Unknown Network>")'")
result(true)
} else {
print("Not Connected")
result(false)
}
} else if let ssid = sSSID {
print("Connected to " + ssid)
// ssid check is required because if wifi not found (could not connect) there seems to be no error given
result(ssid == sSSID)
} else {
print("WiFi network not found")
result(false)
}
return
}
}
} else {
Expand All @@ -185,13 +185,14 @@ public class SwiftWifiIotPlugin: NSObject, FlutterPlugin {
}
}

private func isEnabled(result: FlutterResult) {
private func isEnabled(result: @escaping FlutterResult) {
// For now..
let sSSID: String? = getSSID()
if (sSSID != nil) {
result(true)
} else {
result(nil)
getSSID { (sSSID) in
if (sSSID != nil) {
result(true)
} else {
result(nil)
}
}
}

Expand All @@ -206,69 +207,69 @@ public class SwiftWifiIotPlugin: NSObject, FlutterPlugin {
}
}

private func isConnected(result: FlutterResult) {
private func isConnected(result: @escaping FlutterResult) {
// For now..
let sSSID: String? = getSSID()
if (sSSID != nil) {
result(true)
} else {
result(false)
}
}

private func disconnect(result: FlutterResult) {
if #available(iOS 11.0, *) {
let sSSID: String? = getSSID()
getSSID { (sSSID) in
if (sSSID != nil) {
print("trying to disconnect from '\(sSSID!)'")
NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: sSSID ?? "")
result(true)
} else {
print("SSID is null")
result(false)
}
}
}

private func disconnect(result: @escaping FlutterResult) {
if #available(iOS 11.0, *) {
getSSID { (sSSID) in
if (sSSID != nil) {
print("trying to disconnect from '\(sSSID!)'")
NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: sSSID ?? "")
result(true)
} else {
print("SSID is null")
result(false)
}
}
} else {
print("Not disconnected")
result(nil)
}
}

private func getSSID() -> String? {
var ssid: String?
private func getSSID(result: @escaping (String?) -> ()) {
if #available(iOS 14.0, *) {
NEHotspotNetwork.fetchCurrent(completionHandler: { currentNetwork in
ssid = currentNetwork?.ssid
result(currentNetwork?.ssid);
})
} else {
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
break
result(interfaceInfo[kCNNetworkInfoKeySSID as String] as? String)
return
}
}
}
result(nil)
}
return ssid
}

private func getBSSID() -> String? {
var bssid: String?
private func getBSSID(result: @escaping (String?) -> ()) {
if #available(iOS 14.0, *) {
NEHotspotNetwork.fetchCurrent(completionHandler: { currentNetwork in
bssid = currentNetwork?.bssid
result(currentNetwork?.bssid);
})
} else {
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
bssid = interfaceInfo[kCNNetworkInfoKeyBSSID as String] as? String
break
result(interfaceInfo[kCNNetworkInfoKeyBSSID as String] as? String)
return
}
}
}
result(nil)
}
return bssid
}

private func getCurrentSignalStrength(result: FlutterResult) {
Expand Down

0 comments on commit 089b84b

Please sign in to comment.