Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): getSSID and getBSSID always returning null #175

Merged
merged 2 commits into from
Sep 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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