Skip to content

Commit

Permalink
fix(wifi_iot): Add support for SDK-31 concurrent networks addresses #273
Browse files Browse the repository at this point in the history
 (#309)

* Add support for Android SDK-31 concurrent connections https://developer.android.com/about/versions/12/behavior-changes-12#concurrent-connections

Inspiration taken from https://github.com/Martichou/WiFiFlutter commits: d51bf05 and b4c9aff.

* Fix WifiIotPlugin.java uses unchecked or unsafe operations.
  • Loading branch information
darhaywa authored Nov 5, 2022
1 parent 339d0de commit 397aaa3
Showing 1 changed file with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class WifiIotPlugin
private MethodChannel channel;
private EventChannel eventChannel;

private Network joinedNetwork;
private WifiManager moWiFi;
private Context moContext;
private WifiApManager moWiFiAPManager;
Expand Down Expand Up @@ -784,6 +785,27 @@ private void _loadWifiList(final Result poResult) {
}
}

private boolean selectNetwork(final Network network, final ConnectivityManager manager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return manager.bindProcessToNetwork(network);
} else {
return ConnectivityManager.setProcessDefaultNetwork(network);
}
}

private void onAvailableNetwork(
final ConnectivityManager manager, final Network network, final Result poResult) {
final boolean result = selectNetwork(network, manager);
final Handler handler = new Handler(Looper.getMainLooper());
handler.post(
new Runnable() {
@Override
public void run() {
poResult.success(result);
}
});
}

/// Method to force wifi usage if the user needs to send requests via wifi
/// if it does not have internet connection. Useful for IoT applications, when
/// the app needs to communicate and send requests to a device that have no
Expand All @@ -802,43 +824,30 @@ private void forceWifiUsage(final MethodCall poCall, final Result poResult) {
boolean shouldReply = true;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP && manager != null) {
if (useWifi) {
NetworkRequest.Builder builder;
builder = new NetworkRequest.Builder();
/// set the transport type do WIFI
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
shouldReply = false;
manager.requestNetwork(
builder.build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
boolean success = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
success = manager.bindProcessToNetwork(network);

} else {
success = ConnectivityManager.setProcessDefaultNetwork(network);
}
manager.unregisterNetworkCallback(this);
final boolean result = success;
final Handler handler = new Handler(Looper.getMainLooper());
handler.post(
new Runnable() {
@Override
public void run() {
poResult.success(result);
}
});
}
});

} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
success = manager.bindProcessToNetwork(null);
// SDK-31 If not previously in a disconnected state, select the joinedNetwork to ensure
// the correct network is used for communications, else fallback to network manager network.
// https://developer.android.com/about/versions/12/behavior-changes-12#concurrent-connections
if (joinedNetwork != null) {
success = selectNetwork(joinedNetwork, manager);
} else {
success = ConnectivityManager.setProcessDefaultNetwork(null);
NetworkRequest.Builder builder;
builder = new NetworkRequest.Builder();
/// set the transport type do WIFI
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
shouldReply = false;
manager.requestNetwork(
builder.build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
manager.unregisterNetworkCallback(this);
onAvailableNetwork(manager, network, poResult);
}
});
}
} else {
success = selectNetwork(null, manager);
}
}
if (shouldReply) {
Expand Down Expand Up @@ -1134,6 +1143,7 @@ private void disconnect(Result poResult) {
connectivityManager.unregisterNetworkCallback(networkCallback);
networkCallback = null;
disconnected = true;
joinedNetwork = null;
} else if (networkSuggestions != null) {
final int networksRemoved = moWiFi.removeNetworkSuggestions(networkSuggestions);
disconnected = networksRemoved == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS;
Expand Down Expand Up @@ -1219,7 +1229,7 @@ private void removeWifiNetwork(MethodCall poCall, Result poResult) {
// remove network suggestion
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
List<WifiNetworkSuggestion> suggestions = moWiFi.getNetworkSuggestions();
List<WifiNetworkSuggestion> removeSuggestions = new ArrayList();
List<WifiNetworkSuggestion> removeSuggestions = new ArrayList<WifiNetworkSuggestion>();
for (int i = 0, suggestionsSize = suggestions.size(); i < suggestionsSize; i++) {
WifiNetworkSuggestion suggestion = suggestions.get(i);
if (suggestion.getSsid().startsWith(prefix_ssid)) {
Expand Down Expand Up @@ -1399,6 +1409,7 @@ public void run() {
public void onAvailable(@NonNull Network network) {
super.onAvailable(network);
if (!resultSent) {
joinedNetwork = network;
poResult.success(true);
resultSent = true;
}
Expand Down

0 comments on commit 397aaa3

Please sign in to comment.