@@ -62,6 +62,7 @@ public class WifiIotPlugin
6262 private MethodChannel channel ;
6363 private EventChannel eventChannel ;
6464
65+ private Network joinedNetwork ;
6566 private WifiManager moWiFi ;
6667 private Context moContext ;
6768 private WifiApManager moWiFiAPManager ;
@@ -784,6 +785,27 @@ private void _loadWifiList(final Result poResult) {
784785 }
785786 }
786787
788+ private boolean selectNetwork (final Network network , final ConnectivityManager manager ) {
789+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
790+ return manager .bindProcessToNetwork (network );
791+ } else {
792+ return ConnectivityManager .setProcessDefaultNetwork (network );
793+ }
794+ }
795+
796+ private void onAvailableNetwork (
797+ final ConnectivityManager manager , final Network network , final Result poResult ) {
798+ final boolean result = selectNetwork (network , manager );
799+ final Handler handler = new Handler (Looper .getMainLooper ());
800+ handler .post (
801+ new Runnable () {
802+ @ Override
803+ public void run () {
804+ poResult .success (result );
805+ }
806+ });
807+ }
808+
787809 /// Method to force wifi usage if the user needs to send requests via wifi
788810 /// if it does not have internet connection. Useful for IoT applications, when
789811 /// the app needs to communicate and send requests to a device that have no
@@ -802,43 +824,30 @@ private void forceWifiUsage(final MethodCall poCall, final Result poResult) {
802824 boolean shouldReply = true ;
803825 if (Build .VERSION .SDK_INT > Build .VERSION_CODES .LOLLIPOP && manager != null ) {
804826 if (useWifi ) {
805- NetworkRequest .Builder builder ;
806- builder = new NetworkRequest .Builder ();
807- /// set the transport type do WIFI
808- builder .addTransportType (NetworkCapabilities .TRANSPORT_WIFI );
809- shouldReply = false ;
810- manager .requestNetwork (
811- builder .build (),
812- new ConnectivityManager .NetworkCallback () {
813- @ Override
814- public void onAvailable (Network network ) {
815- super .onAvailable (network );
816- boolean success = false ;
817- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
818- success = manager .bindProcessToNetwork (network );
819-
820- } else {
821- success = ConnectivityManager .setProcessDefaultNetwork (network );
822- }
823- manager .unregisterNetworkCallback (this );
824- final boolean result = success ;
825- final Handler handler = new Handler (Looper .getMainLooper ());
826- handler .post (
827- new Runnable () {
828- @ Override
829- public void run () {
830- poResult .success (result );
831- }
832- });
833- }
834- });
835-
836- } else {
837- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
838- success = manager .bindProcessToNetwork (null );
827+ // SDK-31 If not previously in a disconnected state, select the joinedNetwork to ensure
828+ // the correct network is used for communications, else fallback to network manager network.
829+ // https://developer.android.com/about/versions/12/behavior-changes-12#concurrent-connections
830+ if (joinedNetwork != null ) {
831+ success = selectNetwork (joinedNetwork , manager );
839832 } else {
840- success = ConnectivityManager .setProcessDefaultNetwork (null );
833+ NetworkRequest .Builder builder ;
834+ builder = new NetworkRequest .Builder ();
835+ /// set the transport type do WIFI
836+ builder .addTransportType (NetworkCapabilities .TRANSPORT_WIFI );
837+ shouldReply = false ;
838+ manager .requestNetwork (
839+ builder .build (),
840+ new ConnectivityManager .NetworkCallback () {
841+ @ Override
842+ public void onAvailable (Network network ) {
843+ super .onAvailable (network );
844+ manager .unregisterNetworkCallback (this );
845+ onAvailableNetwork (manager , network , poResult );
846+ }
847+ });
841848 }
849+ } else {
850+ success = selectNetwork (null , manager );
842851 }
843852 }
844853 if (shouldReply ) {
@@ -1134,6 +1143,7 @@ private void disconnect(Result poResult) {
11341143 connectivityManager .unregisterNetworkCallback (networkCallback );
11351144 networkCallback = null ;
11361145 disconnected = true ;
1146+ joinedNetwork = null ;
11371147 } else if (networkSuggestions != null ) {
11381148 final int networksRemoved = moWiFi .removeNetworkSuggestions (networkSuggestions );
11391149 disconnected = networksRemoved == WifiManager .STATUS_NETWORK_SUGGESTIONS_SUCCESS ;
@@ -1219,7 +1229,7 @@ private void removeWifiNetwork(MethodCall poCall, Result poResult) {
12191229 // remove network suggestion
12201230 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
12211231 List <WifiNetworkSuggestion > suggestions = moWiFi .getNetworkSuggestions ();
1222- List <WifiNetworkSuggestion > removeSuggestions = new ArrayList ();
1232+ List <WifiNetworkSuggestion > removeSuggestions = new ArrayList < WifiNetworkSuggestion > ();
12231233 for (int i = 0 , suggestionsSize = suggestions .size (); i < suggestionsSize ; i ++) {
12241234 WifiNetworkSuggestion suggestion = suggestions .get (i );
12251235 if (suggestion .getSsid ().startsWith (prefix_ssid )) {
@@ -1399,6 +1409,7 @@ public void run() {
13991409 public void onAvailable (@ NonNull Network network ) {
14001410 super .onAvailable (network );
14011411 if (!resultSent ) {
1412+ joinedNetwork = network ;
14021413 poResult .success (true );
14031414 resultSent = true ;
14041415 }
0 commit comments