Delphi XE 获取Wifi 信息

//获取Wifi信息单元

unit my.Android.Wifi;



interface


uses
  System.SysUtils,
  System.Classes,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNIBridge,
  Androidapi.JNI.Telephony,
  Androidapi.Jni.JavaTypes,
  Androidapi.JNI.Net,
  Androidapi.Helpers;


function GetWiFiManager(var WifiManager: JWiFiManager): Boolean;
function GetWiFiInfo(var WiFiInfo: JWifiInfo; var AState: Integer): Boolean;
function IntToIp(intIP : Integer) : string;


implementation


function IntToIp(intIP : Integer) : string;
var
  n : int64;
begin
  Result := '';
  n := intIP shr 24;
  intIP := intIP xor (n shl 24);
  Result := IntToStr(n);
  n := intIP shr 16;
  intIP := intIP xor (n shl 16);
  Result :=  IntToStr(n) + '.'+Result;
  n := intIP shr 8;
  intIP := intIP xor (n shl 8);
  Result :=  IntToStr(n) + '.'+Result;
  n := intIP;
  Result :=  IntToStr(n)+'.'+Result;
end;


function GetWiFiInfo(var WiFiInfo: JWifiInfo; var AState: Integer): Boolean;
var
  WifiManager: JWiFiManager;
begin
  Result := False;
  if GetWiFiManager(WifiManager) then
  begin
    AState:= WifiManager.getWifiState;
    WiFiInfo := WifiManager.getConnectionInfo;
    Result := true;
  end;
end;


function GetWiFiManager(var WifiManager: JWiFiManager): Boolean;
var
  Obj: JObject;
begin
  Result := False;
  Obj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);

  if Assigned(Obj) then
  // raise Exception.Create('Could not locate Wifi Service');
  begin
    WifiManager := TJWiFiManager.Wrap((Obj as ILocalObject).GetObjectID);
    if Assigned(WifiManager) then
    begin
      if not WifiManager.isWifiEnabled then
      begin
        if WifiManager.setWifiEnabled(True) then
          Result := True
        else
          Result := False;
      end else
        Result := True;
    end;
  end;
end;


end.


//主窗体单元

unit Unit1;


interface


uses
  System.SysUtils,
  System.Types,
  System.UITypes,
  System.Classes,
  System.Variants,
  FMX.ScrollBox,
  FMX.Memo,
  FMX.Controls,
  FMX.Controls.Presentation,
  FMX.StdCtrls,
  FMX.Types,
  FMX.Forms,
  FMX.Graphics,
  FMX.Dialogs,
  FMX.Layouts,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNIBridge,
  Androidapi.JNI.Telephony,
  Androidapi.Jni.JavaTypes,
  Androidapi.JNI.Net,
  Androidapi.Helpers;


type
  TForm3 = class(TForm)
    Memo1: TMemo;
    btnObtain: TButton;
    procedure btnObtainClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form3: TForm3;


implementation
  uses my.Android.Wifi;


{$R *.fmx}


procedure TForm3.btnObtainClick(Sender: TObject);
var
  WiFiInfo: JWiFiInfo;
  state:Integer;
begin
  Memo1.Lines.Clear;
  if GetWifiInfo(WiFiInfo, state) then
  begin
    Memo1.Lines.add('State:' + IntToStr(state));
    Memo1.Lines.add('BSSID:' + JStringToString(WiFiInfo.getBSSID));
    Memo1.Lines.add('IpAddress:' + IntToIp(WiFiInfo.getIpAddress));
    Memo1.Lines.add('LinkSpeed:' + IntTostr(WiFiInfo.getLinkSpeed));
    Memo1.Lines.add('MacAddress:' + JStringToString(WiFiInfo.getMacAddress));
    Memo1.Lines.add('NetworkId:' + IntTostr(WiFiInfo.getNetworkId));
    Memo1.Lines.add('Rssi:' + IntTostr(WiFiInfo.getRssi));
    Memo1.Lines.add('String:' + JStringToString(WiFiInfo.toString));
  end else begin
    Memo1.Lines.add('State:' + IntToStr(state));
    Memo1.Lines.add('获取 WIFI 信息失败');
  end;
end;


end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值