1

What I exactly need is to know if there is there is a way to connect to a specific Wifi Network from my Android App using Delphi XE5.

I have made some research in order to get an answer but haven´t found a clue on how to do it.

The closest question is here Create WifiConfiguration with Delphi XE5 for Android, but it is not the same question and it´s not answered though. The difference between my question and the one in the link, is that the one in the link refers to the general purpose Wifi Configuration, but my question is more specific. In fact, I would like to know if any of the procedures or functions mentioned on the question in the link can solve my question.

The question is: how can connect to a Wifi Network using a library, class or method within Delphi XE5 while developing an Android App.

I have not written my own code yet because there is no starting point that I have thought with what I have found until now.

Am I missing a good alternative on how to solve this problem?

Community
  • 1
  • 1
Laureano Bonilla
  • 335
  • 3
  • 18
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Dalija Prasnikar May 09 '15 at 12:56
  • I have edited and now I think it is clear what I need. Thank you Dalija. – Laureano Bonilla May 11 '15 at 14:19
  • Ok, now I already edited again. Let me know if you think the question is already clear please. – Laureano Bonilla May 11 '15 at 21:36
  • Hi Laureano, did you take a look at the android SDK first ? http://developer.android.com/reference/android/net/wifi/package-summary.html you can use the wifimanager class to work with as it provide the enableNetwork method that you indicates if it will disable the others networks and connect to the network id that you whant. Disconnect, et all. take a look at it. – Diego Garcia May 12 '15 at 13:51
  • Thank you very much Diego. I assume that this means using the JNI to implement the Java classes into Delphi XE5 – Laureano Bonilla May 12 '15 at 14:51

1 Answers1

4

You will need to use the JNI to call native Java (Android SDK) functions to connect to your network.

This tutorial shows you how to JNI from Delphi.

This SO question shows you how to programatically connect to a Wifi SSID from the Java side.

Basically, you need to create a Java function that connects to your network:

void connectToWifi()
{
    String networkSSID = "test";
    String networkPass = "pass";

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + networkSSID + "\"";

    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
    wifiManager.addNetwork(conf);

    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    for( WifiConfiguration i : list ) {
        if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
             wifiManager.disconnect();
             wifiManager.enableNetwork(i.networkId, true);
             wifiManager.reconnect();               

             break;
        }           
    }
}

After that, call this function from the Delphi side by invoking the JNI (see the link above):

try

  // Create the JVM (using a wrapper class)
JavaVM := TJavaVM.Create;

  // Set the options for the VM
Options[0].optionString := '-Djava.class.path=.';
VM_args.version := JNI_VERSION_1_2;
VM_args.options := @Options;
VM_args.nOptions := 1;

  // Load the VM
Errcode := JavaVM.LoadVM(VM_args);
if Errcode < 0 then
begin
  WriteLn(Format('Error loading JavaVM, error code = %d', [Errcode]));
  Exit;
end;

  // Create a Java environment from the JVM's Env (another wrapper class)
JNIEnv := TJNIEnv.Create(JavaVM.Env);

  // Find the class in the file system. This is why we added
  // the current directory to the Java classpath above.
Cls := JNIEnv.FindClass('YOUR_CLASS');
if Cls = nil then
begin
  WriteLn('Can''t find class: YOUR_CLASS');
  Exit;
end;

  // Find the static method 'connectToWifi' within the YOUR_CLASS class
Mid := JNIEnv.GetStaticMethodID(Cls, 'connectToWifi', '()V');
if Mid = nil then
begin
  WriteLn('Can''t find method: connectToWifi');
  Exit;
end;

  // Call the static method
JNIEnv.CallStaticVoidMethod(Cls, Mid, []);

except
  on E : Exception do
    WriteLn('Error: ' + E.Message);
end;

Hope I helped.

Community
  • 1
  • 1
Robin Eisenberg
  • 1,836
  • 18
  • 26
  • Great, this answer is not only a great help for the specific question but also for the general purpose of working with the JNI. Thank you very much. – Laureano Bonilla May 18 '15 at 14:13