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.

相关阅读 >>

如何在Delphi中禁用关于“返回值...可能未定义”的警告?

Delphi 判断端口(port)是否被占用

Delphi开机启动项管理源码

Delphiwindows 下编译 exe 文件时把一个外部 txt 文件编译到 exe 里面

Delphi 如何在tmemo,tedit或trichedit中获得插入符的位置

Delphi winapi: getmodulefilename、getmodulehandle

Delphi 从twebbrowser webbrowser得到全部html源码

Delphi检测网络连接状态

Delphi 隐藏文件夹

Delphi 的tstringbuilder防止服务器内存碎片化

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...