Kastri icon indicating copy to clipboard operation
Kastri copied to clipboard

Android: Wifi-network not detected if mobile-network exists

Open westner opened this issue 3 years ago • 2 comments

Hello,

if my mobile phone is connected to a wifi and a cellular network then the GetConnectedNetworkInfo doesn't work and doesn't report the wifi-network.

In my opinion in DW.Connectivity.Android.pas the method class function TNetworkCallbackDelegate.GetConnectedNetworkInfo(const ASkipValidation: Boolean): JNetworkInfo;

should be changed to

class function TNetworkCallbackDelegate.GetConnectedNetworkInfo(const ASkipValidation: Boolean): JNetworkInfo;
var
  LAllNetworks: TJavaObjectArray<JNetwork>;
  LAllNetworkInfo: TJavaObjectArray<JNetworkInfo>;
  LInfo: JNetworkInfo;
  I: Integer;
begin
  Result := nil;
  if TJBuild_VERSION.JavaClass.SDK_INT >= 21 then
  begin
    LAllNetworks := ConnectivityManager.getAllNetworks;
    try
      for I := 0 to LAllNetworks.Length - 1 do
      begin
        LInfo := GetConnectedNetworkInfoFromNetwork(LAllNetworks[I], ASkipValidation);
        if LInfo <> nil then
        begin
          Result := LInfo;
          **if LInfo.getType = TJConnectivityManager.JavaClass.TYPE_WIFI then**
            Break;
        end;
      end;
    finally
      LAllNetworks.Sync;
    end;
  end

The bold line is added.

Any comment to this change?

Stefan

westner avatar Aug 18 '22 00:08 westner

You're right - if the device has both, then IsWifiInternetConnection will return False if the first network found is not Wifi. Both IsWifiInternetConnection and GetConnectedNetworkInfo will need to be changed to account for this.. thanks!

DelphiWorlds avatar Aug 18 '22 00:08 DelphiWorlds

I've pushed a change that should rectify the issue. I'm leaving the issue open until you can verify.

DelphiWorlds avatar Aug 18 '22 01:08 DelphiWorlds