Delphi XE 获取Wifi 信息
2016-06-18 00:53
459 查看
//获取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.
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程序全屏显示无标题栏覆盖整个屏幕
- Delphi7 实现窗体全屏方法
- delphi创建excel写入数据并创建二维折线图
- Delphi TClientDataset查找定位功能
- Delphi类型转换
- Delphi代码标准文档
- Delphi开发文件枚举小工具
- Delphi之通过崩溃地址找出源代码的出错行
- Delphi 停靠技术的应用1(在一个窗体中停靠另一个窗体)
- Delphi 停靠技术的应用2(窗体之间的相互停靠,引入宿主窗体)
- Delphi 停靠技术的应用3(两个窗体停靠成PageControl样式, 分页停靠)
- Delphi的MDI编程中遇到的一个奇怪问题(值得研究的一个问题)
- 不用注册热键方式在Delphi中实现定义快捷键(又简单又巧妙,但要当前窗体处在激活状态)
- Delphi XE中使用dbExpress连接MySQL数据库疑难问题解决(对三层的例子配置有帮助)
- 关于Delphi中二维数组的声明和大小调整(对非基本类型数据,小心内存泄漏)
- Delphi中使用TXMLDocument控件应注意的问题
- Delphi中取整函数Round的Bug解决
- 让程序只运行一个实例(Delphi篇)(三种方法,其中使用全局原子的方法比较有意思)
- Delphi中三种方法获取Windows任务栏的高度
- Delphi截获webbrowser中关闭及右键消息处理一例