Jump to content

OhRio

Members
  • Content Count

    3
  • Joined

  • Last visited

Posts posted by OhRio


  1. 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;


  2. 13 hours ago, Rollo62 said:

    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
     

×