Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Cross-Platform-Entwicklung (https://www.delphipraxis.net/91-cross-platform-entwicklung/)
-   -   Delphi Android - delete WIFI (https://www.delphipraxis.net/194859-android-delete-wifi.html)

danten 17. Jan 2018 07:25

Android - delete WIFI
 
Hello,
I need to delete all saved WIFI networks for business purposes and retrieve new ones from the list downloaded from the server.

Here the user would click on the WIFI network in the list and join only to her.

Thank you for your help.

Delphi-Quellcode:
procedure RemoveWifiList;
var
  WifiManagerObj: JObject;
  WifiManager: JWifiManager;
  ScanResult: JScanResult;
  i: Integer;
begin
  WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
  WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID);

  for i := 0 to WifiManager.getScanResults.size do
  begin
    ScanResult := TJScanResult.Wrap((WifiManager.getScanResults.get(i) as ILocalObject).GetObjectID);
    WifiManager.removeNetwork(i);
  end;

end;

procedure ConnectToSelectedWIFI;
var
  WifiManager: JWifiManager;
  aSSID, aPASS: string;
  WifiConf: JWifiConfiguration;
begin
  WifiManager := ??????
  WifiConf := ?????

  aSSID := '"'+edSSID.Text+'"';
  aPASS := '"'+edPASS.Text+'"';

  WifiConf.SSID := StringToJString(aSSID);
  WifiConf.preSharedKey := StringToJString(aPASS);
  WifiConf.hiddenSSID := False;
  WIFIManager.addNetwork(WifiConf);
  WIFIManager.enableNetwork(WifiConf.networkId,True);
  ????  Create connections and connect new ???
end;

danten 17. Jan 2018 21:48

AW: Android - delete WIFI
 
Nobody has help for me?
The procedure for removing the Wifi network works well on your mobile, but it does not work on your tablet.
Do you know why?
Thank you

Rollo62 18. Jan 2018 06:23

AW: Android - delete WIFI
 
Not sure either, but maybe you have to work with WiFiConnection.

Rollo

danten 18. Jan 2018 07:12

AW: Android - delete WIFI
 
Thank you for answer.
What is the programming language?
I can not C or C ++ and I do not understand the code.

Could not anyone put a code for Delphi?

I will be grateful.

Thank you

Rollo62 18. Jan 2018 08:10

AW: Android - delete WIFI
 
This is probably Java.
Just replace the brackets { } by begin end and you're very close to Delphi anyhow :stupid:
Should be OK to understand the basic procedures needed.

The basic steps are described there, you can try to find the similar classes in Delpi source.

Rollo

danten 18. Jan 2018 08:50

AW: Android - delete WIFI
 
I probably do not understand it and nobody will correct my code.

Delphi-Quellcode:
procedure TfrmApp.ConnectToWifi; // ERROR
var
  WifiManagerObj: JObject;
  WifiManager: JWifiManager;
  WifiConfiguration: JWifiConfiguration;
  i: integer;
  netID: integer;
  List: JList;
begin
  try
  aSSID := '"'+qDB.FieldByName('ssid').AsString+'"';
  aPass := '"'+qDB.FieldByname('pass').AsString+'"';

  WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
  WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID);

  WifiConfiguration.SSID := StringToJString(aSSID);
  WifiConfiguration.preSharedKey := StringToJString(aPass);

  List := WifiManager.getConfiguredNetworks;

  for i := 0 to List.size -1 do
  begin
    if WifiConfiguration.SSID = stringToJString(aSSID) then
    begin
      netID := WifiManager.addNetwork(WifiConfiguration);
      WifiManager.disconnect;
      WifiManager.enableNetwork(netID, True);
      WifiManager.reconnect;

      Break;
    end;
  end;
  except
    on E: exception do
    begin
      ShowMessage('Connect: '+E.Message);
    end;
  end;
end;

procedure TfrmApp.RemoveWifiList; // NOT WORK IN TABLET AND ANDROID 7
var
  WifiManagerObj: JObject;
  WifiManager,WifiManager2: JWifiManager;
  ScanResult: JScanResult;
  WifiConf: JWifiConfiguration;
  i,r: Integer;
  netID: integer;
begin
  try
  WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
  WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID);

  try
  for i := 0 to WifiManager.getScanResults.size -1 do
  begin
    ScanResult := TJScanResult.Wrap((WifiManager.getScanResults.get(i) as ILocalObject).GetObjectID);
    netID := WifiManager.getConnectionInfo.getNetworkId;
    WifiManager.removeNetwork(netID);
    WifiManager.saveConfiguration();
  end;
  finally
    ConnectToWifi;
  end;
  except
    on E: exception do
    begin
      ShowMessage('Remove: '+E.Message);
    end;
  end;
end;

Rollo62 18. Jan 2018 09:00

AW: Android - delete WIFI
 
Sorry, I have not time left and about to leave the office soon.
If you would have a complete test-project it would be easier to look at it.

Rollo

danten 18. Jan 2018 09:29

AW: Android - delete WIFI
 
Liste der Anhänge anzeigen (Anzahl: 1)
Thank you, I uploaded the project as an attachment.

danten 19. Jan 2018 16:48

AW: Android - delete WIFI
 
Hello, I have tried all the options according to the help but still do not have a functional application.

You really do not know why it does not work.

I set all permissions in permission and it does not work either.

Rollo62 22. Jan 2018 17:07

AW: Android - delete WIFI
 
I found some time to check your demo.

Its hard to check, since I had to cleanup a lot.
I do not use UniDac, etc., so I have to separate some functions first,
and I tried to use with my fixed network ID here.

I assume your main goal is to connect with one network from the http list only, right ?

An my Nexus9 tablet I can move through the preconfigured network list, since I can see their SSID's (I removed saveConfiguration, to not destroy all my settings here).
But after removeNetwork it still connects to a removedSSID.

I'M not sure whether this behaviour is normal, but I lost a lot of time to unscramble the code, so I haven't test unter other settings.
The docs say this is depreceated anyway.
So all I can say now that I can walk through the networkID's, calling removeNewwork, but still my tablet keeps connected, and also will reconnect.


Delphi-Quellcode:
procedure TfrmApp.RemoveWifiList;
var
  WifiManagerObj: JObject;
  WifiManager: JWifiManager;
  ScanResult: JScanResult;
  List: JList;

  //WifiConf: JWifiConfiguration;
  i: Integer;
  netID: integer;
  LWifiConf: JWifiConfiguration;
  LSsid: String;
  LNetId: Integer;
begin
  try
  WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
  WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID);

  Memo1.Lines.Insert(0, 'RemoveWifiList:');

  List := WifiManager.getConfiguredNetworks;

  for i := 0 to List.size -1 do
  begin

    LWifiConf := TJWifiConfiguration.Wrap( List.get(i) );
    LSsid    := JStringToString( LWifiConf.SSID );
    LNetId   := LWifiConf.networkId;

    Memo1.Lines.Insert(0, '- SSID: ' + LSsid + ' ID(' + LNetId.ToString + ') REMOVED');

    WifiManager.removeNetwork( LNetId );
//    WifiManager.saveConfiguration(); // TEST this is maybe missing to store permanently


  end;
  except
    on E: exception do
    begin
      ShowMessage('Remove: '+E.Message);
    end;
  end;

...

end;

Rollo

danten 26. Jan 2018 19:35

AW: Android - delete WIFI
 
It does not work, it only shuts down and does not delete saved connection settings.

Delphi-Quellcode:
procedure TfrmApp.RemoveWifiList;
var
  WifiManagerObj: JObject;
  WifiManager: JWifiManager;
  List: JList;
  i: Integer;
  LWifiConf: JWifiConfiguration;
  LSsid: String;
  LNetId: Integer;
begin
  try
  WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
  WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID);

  List := WifiManager.getConfiguredNetworks;

  for i := 0 to List.size -1 do
  begin

    LWifiConf := TJWifiConfiguration.Wrap( List.get(i) );
    LSsid := JStringToString( LWifiConf.SSID );
    LNetId := LWifiConf.networkId;

    WifiManager.removeNetwork( LNetId );

    WifiManager.disableNetwork(LNetId);

    WifiManager.disconnect;
    WifiManager.saveConfiguration();

  end;

  except
    on E: exception do
    begin
      ShowMessage('Remove: '+E.Message);
    end;
  end;

end;

localhost 7. Mär 2019 14:27

AW: Android - delete WIFI
 
i have the same Problem. The Result from "removeNetwork" is False.

Developer.Andoird says: "Applications are not allowed to remove networks created by other applications." so my app can not delete everything?

mjustin 7. Mär 2019 14:43

AW: Android - delete WIFI
 
Zitat:

Zitat von localhost (Beitrag 1427216)
i have the same Problem. The Result from "removeNetwork" is False.

Developer.Andoird says: "Applications are not allowed to remove networks created by other applications." so my app can not delete everything?

Correct, see

- https://stackoverflow.com/questions/...rogramatically
- https://developer.android.com/about/...havior-network

Rollo62 7. Mär 2019 16:47

AW: Android - delete WIFI
 
So if we are not allowed to change the Wifi network,
maybe there is any alternative method, which asks the user to change the network ?

I think an app should be able to tell what "preferred" network it should use,
and Google probably put something like special permission dialog in ?

mjustin 7. Mär 2019 16:52

AW: Android - delete WIFI
 
Zitat:

Zitat von Rollo62 (Beitrag 1427254)
So if we are not allowed to change the Wifi network,
maybe there is any alternative method, which asks the user to change the network ?

I think an app should be able to tell what "preferred" network it should use,
and Google probably put something like special permission dialog in ?

The WiFi settings screen can be invoked from code, so the user may be notified about the preferred network first from your app, and then directed to select it there:

- https://stackoverflow.com/questions/...-using-android

- https://developer.android.com/refere...K_WIFI_NETWORK

Rollo62 7. Mär 2019 16:59

AW: Android - delete WIFI
 
Thanks for the info, would be at least the 2nd best option.
I hoped that I could give a parameter with this intent, but it cannot :(


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:42 Uhr.

Powered by vBulletin® Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2021 by Daniel R. Wolf