OhRio 0 Posted October 29, 2020 (edited) Hi My project's requirement is that I need to connect to any wifi network automatically from the phone app when the user selects the ssid from a list (with accompanying password if any). I came across this discussion on stackoverflow. I am no java expert so i wonder if anybody can help to get this working in Delphi ? Thank you very much Edited October 29, 2020 by OhRio Share this post Link to post
Rollo62 536 Posted October 30, 2020 Not directly an answer to your question, but maybe some input. https://www.delphipraxis.net/194859-android-delete-wifi.html https://stackoverflow.com/questions/2318310/how-to-call-wi-fi-settings-screen-from-my-application-using-android https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-network Share this post Link to post
OhRio 0 Posted October 31, 2020 13 hours ago, Rollo62 said: Not directly an answer to your question, but maybe some input. https://www.delphipraxis.net/194859-android-delete-wifi.html https://stackoverflow.com/questions/2318310/how-to-call-wi-fi-settings-screen-from-my-application-using-android https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-network Hi Rollo62, Thank you , the wifi settings page code is useful though i still cannot connect to a desired SSID using java code. I wonder if anybody has ever implemented this .... Regards Share this post Link to post
Dave Nottage 557 Posted October 31, 2020 3 hours ago, OhRio said: i still cannot connect to a desired SSID using java code How do you mean? Please show your code. Also bear in mind that showing the wifi settings as per the last link provided by Rollo62 does not allow you to select a particular config. The user must do that Share this post Link to post
OhRio 0 Posted October 31, 2020 1 hour ago, Dave Nottage said: How do you mean? Please show your code. Also bear in mind that showing the wifi settings as per the last link provided by Rollo62 does not allow you to select a particular config. The user must do that Hi Dave This is the code that im using. Although people in Stackoverflow have had this working for them i can't seem to get it to. Perhaps a mistake in the implementation, not sure. The stackoverflow link i referred to is <here > Regards ----------------------------------------------------------------------------------------------------------------------- var WifiManager: JWifiManager; WifiInfo: JWifiInfo; WifiManagerObj: JObject; procedure ConnectToSSID(aSSID,aPWD: string; Memo: TMemo); var count, i, j: Integer; ScanResult: JScanResult; conf,WinConf: JWifiConfiguration; s,ScanCaps: string; IPAddr: TIdBytes; networkSSID,networkPass: Jstring; wManager: JWifiManager; WObject,wConfObj: JObject; networkID: Integer; List: JList; isDisconnected, isEnabled, isReconnected : Boolean; begin WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE); WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID); WifiInfo := WifiManager.getConnectionInfo(); Conf := TJWifiConfiguration.JavaClass.init; networkSSID := StringToJString('"' + aSSID + '"'); networkPass := StringToJString('"' + aPwd + '"'); for i := 0 to WifiManager.getScanResults.size - 1 do begin ScanResult := TJScanResult.Wrap((WifiManager.getScanResults.get(i) as ILocalObject).GetObjectID); S := JStringToString(ScanResult.SSID); ScanCaps := JStringToString(Scanresult.capabilities); ScanCaps := UpperCase(ScanCaps); if (S = aSSID) then begin Memo.Lines.Add('Item clicked, SSID ' + S + ' Security : ' + JStringToString(scanResult.capabilities)); conf.SSID := networkSSID; //"\"" + networkSSID + "\""; // Please note the quotes. String should contain ssid in quotes conf.status := TJWifiConfiguration_Status.JavaClass.ENABLED; conf.priority := 40; if (ScanCaps.Contains('WEP')) then begin Memo.Lines.Add('Configuring WEP'); conf.allowedKeyManagement.&set(TJWifiConfiguration_KeyMgmt.JavaClass.NONE); conf.allowedProtocols.&set(TJWifiConfiguration_Protocol.JavaClass.RSN); conf.allowedProtocols.&set(TJWifiConfiguration_Protocol.JavaClass.WPA); conf.allowedAuthAlgorithms.&set(TJWifiConfiguration_AuthAlgorithm.JavaClass.OPEN); conf.allowedAuthAlgorithms.&set(TJWifiConfiguration_AuthAlgorithm.JavaClass.SHARED); conf.allowedPairwiseCiphers.&set(TJWifiConfiguration_PairwiseCipher.JavaClass.CCMP); conf.allowedPairwiseCiphers.&set(TJWifiConfiguration_PairwiseCipher.JavaClass.TKIP); conf.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.WEP40); conf.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.WEP104); { if password is in hex then //(networkPass.matches("^[0-9a-fA-F]+$")) { conf.wepKeys[0] = networkPass; else conf.wepKeys[0] = "\"".concat(networkPass).concat("\""); } conf.wepKeys[0] := networkPass; conf.wepTxKeyIndex := 0; end else if (ScanCaps.Contains('WPA')) then begin Memo.Lines.Add('Configuring WPA'); conf.allowedProtocols.&set(TJWifiConfiguration_Protocol.JavaClass.RSN); conf.allowedProtocols.&set(TJWifiConfiguration_Protocol.JavaClass.WPA); conf.allowedKeyManagement.&set(TJWifiConfiguration_KeyMgmt.JavaClass.WPA_PSK); conf.allowedPairwiseCiphers.&set(TJWifiConfiguration_PairwiseCipher.JavaClass.CCMP); conf.allowedPairwiseCiphers.&set(TJWifiConfiguration_PairwiseCipher.JavaClass.TKIP); conf.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.WEP40); conf.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.WEP104); conf.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.CCMP); conf.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.TKIP); conf.preSharedKey := networkPass ; end else begin Memo.Lines.Add('Configuring OPEN network'); conf.allowedKeyManagement.&set(TJWifiConfiguration_KeyMgmt.JavaClass.NONE); conf.allowedProtocols.&set(TJWifiConfiguration_Protocol.JavaClass.RSN); conf.allowedProtocols.&set(TJWifiConfiguration_Protocol.JavaClass.WPA); conf.allowedAuthAlgorithms.clear(); conf.allowedPairwiseCiphers.&set(TJWifiConfiguration_PairwiseCipher.JavaClass.CCMP); conf.allowedPairwiseCiphers.&set(TJWifiConfiguration_PairwiseCipher.JavaClass.TKIP); conf.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.WEP40); conf.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.WEP104); conf.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.CCMP); conf.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.TKIP); end; wObject := TAndroidHelper.Context.getApplicationContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE); WManager := TJWifiManager.Wrap((WObject as ILocalObject).GetObjectID); networkID := wManager.addNetwork(conf); Memo.Lines.Add('networkID ' + networkId.ToString); List := wManager.getConfiguredNetworks(); Memo.Lines.Add('SSID: ' + JStringToSTring(networkSSID)); for j := 0 to List.size -1 do begin WinConf := TJWifiConfiguration.Wrap((List.get(j) as ILocalObject).GetObjectID); //Will give an exhaustive list of all SSIDs the phone has ever encountered //Memo.Lines.Add(JStringToString(WinConf.SSID)); if (WinConf.SSID = networkSSID ) then begin isDisconnected := wManager.disconnect(); Memo.Lines.Add('isDisconnected : ' + isDisconnected.ToString()); isEnabled := wManager.enableNetwork(WinConf.networkId, true); //OR winconf.networkID Memo.Lines.Add('isEnabled : ' + isEnabled.ToString()); isReconnected := wManager.reconnect(); Memo.Lines.Add('isReconnected : ' + isReconnected.ToString()); Break; end; end; //Break; end; end; end; Share this post Link to post