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 实现拦截api的钩子(hook)

fastmm5

Delphi 把修改好的cookies重新赋值给webbrowser1 doc

Delphi获取开机时间代码

Delphi idhttp友好错误信息的捕获

Delphi 官方stylebook大全使用

Delphi dos编程

Delphi google text to speech api

Delphi windows 编程[19] - 改变菜单项的状态: getmenustate、enablemenuitem

Delphi 10 seattle中的画布上绘制字符

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



打赏

取消

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

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

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

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

评论

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