您需要使用 JNI 调用本机 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;
希望我能帮上忙。