//获取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.