使用Delphi XE5连接到特定的Wifi网络进行Android应用程序开发

使用Delphi XE5连接到特定的Wifi网络进行Android应用程序开发,android,delphi,networking,wifi,delphi-xe5,Android,Delphi,Networking,Wifi,Delphi Xe5,我真正需要的是知道是否有一种方法可以使用Delphi XE5从我的Android应用程序连接到特定的Wifi网络 为了得到答案,我做了一些研究,但没有找到如何做的线索 最接近的问题在这里,但不是同一个问题,也没有得到回答。我的问题与链接中的问题的区别在于,链接中的问题指的是通用Wifi配置,但我的问题更具体。事实上,我想知道链接中提到的任何程序或功能是否可以解决我的问题 问题是:在开发Android应用程序时,如何使用Delphi XE5中的库、类或方法连接到Wifi网络 我还没有编写自己的代码

我真正需要的是知道是否有一种方法可以使用Delphi XE5从我的Android应用程序连接到特定的Wifi网络

为了得到答案,我做了一些研究,但没有找到如何做的线索

最接近的问题在这里,但不是同一个问题,也没有得到回答。我的问题与链接中的问题的区别在于,链接中的问题指的是通用Wifi配置,但我的问题更具体。事实上,我想知道链接中提到的任何程序或功能是否可以解决我的问题

问题是:在开发Android应用程序时,如何使用Delphi XE5中的库、类或方法连接到Wifi网络

我还没有编写自己的代码,因为到目前为止,我还没有从我发现的东西开始思考


我是否缺少一个解决此问题的好方法?

您需要使用JNI调用本机Java Android SDK函数来连接到您的网络

演示如何从Delphi中创建JNI

演示如何通过编程方式从Java端连接到Wifi SSID

基本上,您需要创建一个连接到网络的Java函数:

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;
        }           
    }
}
之后,通过调用JNI从Delphi端调用此函数,请参见上面的链接:

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;

希望我能帮忙

您需要使用JNI调用本机Java Android SDK函数来连接到您的网络

演示如何从Delphi中创建JNI

演示如何通过编程方式从Java端连接到Wifi SSID

基本上,您需要创建一个连接到网络的Java函数:

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;
        }           
    }
}
之后,通过调用JNI从Delphi端调用此函数,请参见上面的链接:

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;

希望我能帮忙

请澄清您的具体问题或添加其他详细信息,以突出显示您所需的内容。正如目前所写的,很难准确地说出你在问什么。请参见帮助澄清此问题的页面。我已经编辑了,现在我想我需要的内容已经很清楚了。谢谢Dalija。好的,现在我已经再次编辑了。如果你认为问题已经很清楚了,请告诉我。嗨,劳雷亚诺,你先看了安卓SDK吗?您可以使用wifimanager类,因为它提供了enableNetwork方法,您可以指示它是否将禁用其他网络并连接到您需要的网络id。断开,等等。看看,非常感谢你,迭戈。我假设这意味着使用JNI将Java类实现到Delphi Xe5中。请澄清您的具体问题或添加其他详细信息,以突出显示您所需要的内容。正如目前所写的,很难准确地说出你在问什么。请参见帮助澄清此问题的页面。我已经编辑了,现在我想我需要的内容已经很清楚了。谢谢Dalija。好的,现在我已经再次编辑了。如果你认为问题已经很清楚了,请告诉我。嗨,劳雷亚诺,你先看了安卓SDK吗?您可以使用wifimanager类,因为它提供了enableNetwork方法,您可以指示它是否将禁用其他网络并连接到您需要的网络id。断开,等等。看看,非常感谢你,迭戈。我假设这意味着使用JNI将Java类实现到Delphi XE5Great中,这个答案不仅对特定问题有很大帮助,而且对使用JNI的一般目的也有很大帮助。非常感谢。很好,这个答案不仅对具体问题有很大帮助,而且对与JNI合作的一般目的也有很大帮助。非常感谢你。