Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.alternadom.wifiiot;

/**
* Unified Wi-Fi connect result codes; values must match {@code lib/wifi_connect_codes.dart} and
* iOS {@code WifiConnectCodes.swift}. Empty string means success.
*/
public final class WifiConnectCodes {
private WifiConnectCodes() {}

public static final String OK = "";

public static final String INVALID_SSID = "invalid_ssid";

public static final String INVALID_BSSID = "invalid_bssid";
public static final String WEP_NOT_SUPPORTED = "wep_not_supported";
public static final String UNKNOWN = "unknown";

public static final String NETWORK_CONFIGURATION_FAILED = "network_configuration_failed";
public static final String DISCONNECT_FAILED = "disconnect_failed";
public static final String ENABLE_NETWORK_FAILED = "enable_network_failed";
public static final String CONNECTION_TIMEOUT = "connection_timeout";
public static final String CONNECTION_UNAVAILABLE = "connection_unavailable";

public static final String NETWORK_SUGGESTION_INTERNAL = "network_suggestion_internal";
public static final String NETWORK_SUGGESTION_APP_DISALLOWED = "network_suggestion_app_disallowed";
public static final String NETWORK_SUGGESTION_DUPLICATE = "network_suggestion_duplicate";
public static final String NETWORK_SUGGESTION_EXCEEDS_MAX_PER_APP =
"network_suggestion_exceeds_max_per_app";
public static final String NETWORK_SUGGESTION_REMOVE_INVALID = "network_suggestion_remove_invalid";
public static final String NETWORK_SUGGESTION_ADD_NOT_ALLOWED = "network_suggestion_add_not_allowed";
public static final String NETWORK_SUGGESTION_INVALID = "network_suggestion_invalid";
public static final String NETWORK_SUGGESTION_RESTRICTED_BY_ADMIN =
"network_suggestion_restricted_by_admin";
public static final String NETWORK_SUGGESTION_FAILED = "network_suggestion_failed";
}
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,10 @@ private void registerWifiNetwork(final MethodCall poCall, final Result poResult)
suggestedNet.setBssid(macAddress);
}

if (security != null && security.toUpperCase().equals("WPA")) {
if (security != null && (security.toUpperCase().equals("WPA") || security.toUpperCase().equals("WPA2"))) {
suggestedNet.setWpa2Passphrase(password);
} else if (security != null && security.toUpperCase().equals("WPA3")) {
suggestedNet.setWpa3Passphrase(password);
} else if (security != null && security.toUpperCase().equals("WEP")) {
// WEP is not supported
poResult.error(
Expand Down Expand Up @@ -1038,7 +1040,9 @@ public void run() {
private static String getSecurityType(ScanResult scanResult) {
String capabilities = scanResult.capabilities;

if (capabilities.contains("WPA")
if (capabilities.contains("SAE") || capabilities.contains("WPA3")) {
return "WPA3";
} else if (capabilities.contains("WPA")
|| capabilities.contains("WPA2")
|| capabilities.contains("WPA/WPA2 PSK")) {
return "WPA";
Expand Down Expand Up @@ -1252,6 +1256,37 @@ private static String longToIP(int longIp) {
return sb.toString();
}

private static String networkSuggestionStatusToCode(int status) {
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
return WifiConnectCodes.OK;
}
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_INTERNAL) {
return WifiConnectCodes.NETWORK_SUGGESTION_INTERNAL;
}
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_APP_DISALLOWED) {
return WifiConnectCodes.NETWORK_SUGGESTION_APP_DISALLOWED;
}
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE) {
return WifiConnectCodes.NETWORK_SUGGESTION_DUPLICATE;
}
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_EXCEEDS_MAX_PER_APP) {
return WifiConnectCodes.NETWORK_SUGGESTION_EXCEEDS_MAX_PER_APP;
}
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_REMOVE_INVALID) {
return WifiConnectCodes.NETWORK_SUGGESTION_REMOVE_INVALID;
}
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED) {
return WifiConnectCodes.NETWORK_SUGGESTION_ADD_NOT_ALLOWED;
}
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID) {
return WifiConnectCodes.NETWORK_SUGGESTION_INVALID;
}
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_RESTRICTED_BY_ADMIN) {
return WifiConnectCodes.NETWORK_SUGGESTION_RESTRICTED_BY_ADMIN;
}
return WifiConnectCodes.NETWORK_SUGGESTION_FAILED;
}

/// Method to connect to WIFI Network
private void connectTo(
final Result poResult,
Expand All @@ -1265,13 +1300,13 @@ private void connectTo(
final Integer timeoutInSeconds) {
final Handler handler = new Handler(Looper.getMainLooper());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
final boolean connected =
final String connectResult =
connectToDeprecated(ssid, bssid, password, security, joinOnce, isHidden);
handler.post(
new Runnable() {
@Override
public void run() {
poResult.success(connected);
poResult.success(connectResult);
}
});
} else {
Expand All @@ -1281,8 +1316,7 @@ public void run() {
new Runnable() {
@Override
public void run() {
poResult.error(
"Error", "WEP is not supported for Android SDK " + Build.VERSION.SDK_INT, "");
poResult.success(WifiConnectCodes.WEP_NOT_SUPPORTED);
}
});
return;
Expand All @@ -1301,7 +1335,7 @@ public void run() {
new Runnable() {
@Override
public void run() {
poResult.error("Error", "Invalid BSSID representation", "");
poResult.success(WifiConnectCodes.INVALID_BSSID);
}
});
return;
Expand All @@ -1310,8 +1344,10 @@ public void run() {
}

// set password
if (security != null && security.toUpperCase().equals("WPA")) {
if (security != null && (security.toUpperCase().equals("WPA") || security.toUpperCase().equals("WPA2"))) {
builder.setWpa2Passphrase(password);
} else if (security != null && security.toUpperCase().equals("WPA3")) {
builder.setWpa3Passphrase(password);
}

// remove suggestions if already existing
Expand All @@ -1331,11 +1367,12 @@ public void run() {
final int status = moWiFi.addNetworkSuggestions(networkSuggestions);
Log.e(WifiIotPlugin.class.getSimpleName(), "status: " + status);

final String suggestionCode = networkSuggestionStatusToCode(status);
handler.post(
new Runnable() {
@Override
public void run() {
poResult.success(status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS);
poResult.success(suggestionCode);
}
});
} else {
Expand All @@ -1351,7 +1388,7 @@ public void run() {
new Runnable() {
@Override
public void run() {
poResult.error("Error", "Invalid BSSID representation", "");
poResult.success(WifiConnectCodes.INVALID_BSSID);
}
});
return;
Expand All @@ -1360,8 +1397,10 @@ public void run() {
}

// set security
if (security != null && security.toUpperCase().equals("WPA")) {
if (security != null && (security.toUpperCase().equals("WPA") || security.toUpperCase().equals("WPA2"))) {
builder.setWpa2Passphrase(password);
} else if (security != null && security.toUpperCase().equals("WPA3")) {
builder.setWpa3Passphrase(password);
}

final NetworkRequest networkRequest =
Expand All @@ -1385,7 +1424,7 @@ public void onAvailable(@NonNull Network network) {
super.onAvailable(network);
if (!resultSent) {
joinedNetwork = network;
poResult.success(true);
poResult.success(WifiConnectCodes.OK);
resultSent = true;
}
}
Expand All @@ -1397,7 +1436,7 @@ public void onUnavailable() {
connectivityManager.unregisterNetworkCallback(this);
}
if (!resultSent) {
poResult.success(false);
poResult.success(WifiConnectCodes.CONNECTION_UNAVAILABLE);
resultSent = true;
}
}
Expand Down Expand Up @@ -1464,7 +1503,7 @@ private android.net.wifi.WifiConfiguration generateConfiguration(
if (security != null) security = security.toUpperCase();
else security = "NONE";

if (security.toUpperCase().equals("WPA")) {
if (security.equals("WPA") || security.equals("WPA2")) {

/// appropriate ciper is need to set according to security type used,
/// ifcase of not added it will not be able to connect
Expand All @@ -1486,6 +1525,13 @@ private android.net.wifi.WifiConfiguration generateConfiguration(

conf.allowedProtocols.set(android.net.wifi.WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(android.net.wifi.WifiConfiguration.Protocol.WPA);
} else if (security.equals("WPA3")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
conf.preSharedKey = "\"" + password + "\"";
conf.allowedKeyManagement.set(android.net.wifi.WifiConfiguration.KeyMgmt.SAE);
conf.allowedProtocols.set(android.net.wifi.WifiConfiguration.Protocol.RSN);
conf.status = android.net.wifi.WifiConfiguration.Status.ENABLED;
}
} else if (security.equals("WEP")) {
conf.wepKeys[0] = "\"" + password + "\"";
conf.wepTxKeyIndex = 0;
Expand All @@ -1499,7 +1545,7 @@ private android.net.wifi.WifiConfiguration generateConfiguration(
}

@SuppressWarnings("deprecation")
private Boolean connectToDeprecated(
private String connectToDeprecated(
String ssid,
String bssid,
String password,
Expand All @@ -1513,7 +1559,7 @@ private Boolean connectToDeprecated(
int updateNetwork = registerWifiNetworkDeprecated(conf);

if (updateNetwork == -1) {
return false;
return WifiConnectCodes.NETWORK_CONFIGURATION_FAILED;
}

if (joinOnce != null && joinOnce.booleanValue()) {
Expand All @@ -1522,13 +1568,14 @@ private Boolean connectToDeprecated(

boolean disconnect = moWiFi.disconnect();
if (!disconnect) {
return false;
return WifiConnectCodes.DISCONNECT_FAILED;
}

boolean enabled = moWiFi.enableNetwork(updateNetwork, true);
if (!enabled) return false;
if (!enabled) {
return WifiConnectCodes.ENABLE_NETWORK_FAILED;
}

boolean connected = false;
for (int i = 0; i < 20; i++) {
WifiInfo currentNet = moWiFi.getConnectionInfo();
int networkId = currentNet.getNetworkId();
Expand All @@ -1537,16 +1584,18 @@ private Boolean connectToDeprecated(
// Wait for connection to reach state completed
// to discard false positives like auth error
if (networkId != -1 && netState == SupplicantState.COMPLETED) {
connected = networkId == updateNetwork;
break;
if (networkId == updateNetwork) {
return WifiConnectCodes.OK;
}
return WifiConnectCodes.CONNECTION_TIMEOUT;
}
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
break;
return WifiConnectCodes.CONNECTION_TIMEOUT;
}
}

return connected;
return WifiConnectCodes.CONNECTION_TIMEOUT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>9.0</string>
<string>12.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion packages/wifi_iot/example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
# platform :ios, '12.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
11 changes: 7 additions & 4 deletions packages/wifi_iot/example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -173,7 +173,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0910;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "The Chromium Authors";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -231,10 +231,12 @@
/* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand Down Expand Up @@ -263,6 +265,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -372,7 +375,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -419,7 +422,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0910"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -52,6 +52,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
enableGPUValidationMode = "1"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
2 changes: 1 addition & 1 deletion packages/wifi_iot/example/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit
import Flutter

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down
4 changes: 4 additions & 0 deletions packages/wifi_iot/example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
Loading
Loading