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

标签 android delphi networking wifi delphi-xe5

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

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

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

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

我还没有编写自己的代码,因为到目前为止我还没有想到我所找到的东西的起点。

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

最佳答案

您将需要使用 JNI 调用 native Java (Android SDK) 函数以连接到您的网络。

This tutorial向您展示如何从 Delphi 使用 JNI。

This SO question向您展示如何以编程方式从 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;

希望我有所帮助。

关于android - 使用 Delphi XE5 连接到特定的 Wifi 网络进行 Android 应用程序开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30132133/

相关文章:

c# - Xamarin Android OnBackPressed 未触发

delphi - 即使我已将记录定义设置为可供所有单位使用,为何仍会收到 "incompatible types"错误?

delphi - Delphi编译的可执行文件的内部结构

security - SYN 拒绝服务攻击

Android:http套接字超时 - 强制关闭错误

android - 不调用 AsyncTask 的 onPostExecute 方法

android - 如何使用数据库编写 Robolectric (2.3) 测试

object - 在 Delphi 上,如何引用仅使用一个变量创建的对象?

c - IP/UDP数据包头细节过滤

ubuntu - 在ubuntu中通过终端访问无线网络