【问题标题】:How to programmatically enable wi-fi on android device in Delphi?如何在 Delphi 中以编程方式在 android 设备上启用 wi-fi?
【发布时间】:2016-06-02 08:56:07
【问题描述】:

我需要在我的 Android 设备上在 Delphi 中以编程方式启用和禁用 Wi-Fi。

【问题讨论】:

  • 为什么是 Delphi?到目前为止你有没有尝试过?
  • 我一直在用Delphi编程。

标签: android delphi firemonkey android-wifi


【解决方案1】:

这个示例可以解决这个问题:

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"

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 2015-10-14
    • 2012-01-06
    • 1970-01-01
    • 2011-07-11
    • 2012-02-07
    • 1970-01-01
    相关资源
    最近更新 更多