我需要在Delphi中的Android设备上以编程方式启用和禁用wi-fi。
答案 0 :(得分:3)
此示例可以解决此问题:
procedure TForm1.btWiFiEnableClick(Sender: TObject);
var
WiFIServiceNative: JWifiManager;
begin
// https://developer.android.com/reference/android/net/wifi/WifiManager.html#getWifiState()
// https://developer.android.com/reference/android/net/wifi/WifiManager.html#WIFI_STATE_DISABLED
WiFIServiceNative := TJWifiManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.WIFI_SERVICE));
if Assigned(WiFIServiceNative) then begin
Memo1.Lines.Add('Has Wifi manager');
Memo1.Lines.Add('Wifi state is ' + IntToStr(WiFIServiceNative.getWifiState));
// if WiFIServiceNative.getWifiState = TJWifiManager.JavaClass.WIFI_STATE_DISABLED then
if WiFIServiceNative.getWifiState < 2 then // WIFI_STATE_DISABLING or WIFI_STATE_DISABLED
begin
Memo1.Lines.Add('Try to Wi-Fi on:');
if WiFIServiceNative.setWifiEnabled(true) then
Memo1.Lines.Add(' - ' + IntToStr(TJWifiManager.JavaClass.WIFI_STATE_ENABLED) + ' OK')
else
Memo1.Lines.Add(' - FAIL');
end else begin
Memo1.Lines.Add('Try to Wi-Fi off:');
if WiFIServiceNative.setWifiEnabled(false) then
Memo1.Lines.Add(' - ' + IntToStr(TJWifiManager.JavaClass.WIFI_STATE_DISABLED) + ' OK')
else
Memo1.Lines.Add(' - FAIL');
end;
end;
end;
需要使用以下权限:
android:name="android.permission.ACCESS_WIFI_STATE"
android:name="android.permission.WRITE_SETTINGS"