Android实例-获取安卓手机WIFI信息(XE8+小米2)

时间:2021-11-04 00:59:53

Android实例-获取安卓手机WIFI信息(XE8+小米2)

 

结果:

1.必须打开Access wifi state权限,不打开权限会出图二的错误。

 

相关资料:

http://blog.csdn.net/lyf_lyf/article/category/173576

 

实例代码:

  1 unit Unit1;
2
3 interface
4
5 uses
6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.ScrollBox,
8 FMX.Memo, FMX.StdCtrls, FMX.Controls.Presentation,
9 Androidapi.JNI.GraphicsContentViewText,//需要引入
10 Androidapi.JNIBridge,//需要引入
11 Androidapi.JNI.Telephony,//需要引入
12 Androidapi.JNI.JavaTypes,//需要引入
13 FMX.Helpers.Android,//需要引入
14 Androidapi.JNI.Net,//需要引入
15 Androidapi.Helpers;//需要引入
16
17 type
18 TForm1 = class(TForm)
19 Label1: TLabel;
20 Button1: TButton;
21 Memo1: TMemo;
22 procedure Button1Click(Sender: TObject);
23 private
24 { Private declarations }
25 public
26 { Public declarations }
27 end;
28
29 var
30 Form1: TForm1;
31
32 implementation
33
34 {$R *.fmx}
35 {$R *.NmXhdpiPh.fmx ANDROID}
36
37 //ip地址整数转字符串
38 function int2Ip(intIP : Int64) : string;
39 var
40 n : int64;
41 ip4, ip3, ip2, ip1: string;
42 begin
43 Result := '';
44 n := intIP shr 24;
45 intIP := intIP xor (n shl 24);
46 ip4 := IntToStr(n);
47
48 n := intIP shr 16;
49 intIP := intIP xor (n shl 16);
50 ip3 := IntToStr(n);
51
52 n := intIP shr 8;
53 intIP := intIP xor (n shl 8);
54 ip2 := IntToStr(n);
55
56 n := intIP;
57 ip1 := IntToStr(n);
58
59 Result := ip1 + '.' + ip2 + '.' + ip3 + '.' + ip4;
60 end;
61
62 //ip地址字符串转整数(没测过)
63 function ip2Int(const strIP : string): Int64;
64 var
65 lst : TStringList;
66 i : integer;
67 begin
68 result := 0;
69 lst := TStringList.Create;
70 try
71 lst.Delimiter := '.';
72 lst.DelimitedText := strIP;
73 for i := 0 to lst.Count - 1 do
74 result := result + StrToInt64(lst[i]) shl (24 - i * 8);
75 finally
76 lst.Free;
77 end;
78 end;
79
80 procedure TForm1.Button1Click(Sender: TObject);
81 var
82 Service: JObject;
83 WifiManager: JWifiManager;
84 ConnectionInfo: JWifiInfo;
85 ScanResults: JList;
86 ScanResult: JScanResult;
87 I: Integer;
88 iIP: Int64;
89 begin
90 Memo1.Lines.Clear;
91 Service := SharedActivity.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
92 WifiManager := TJWifiManager.Wrap((Service as ILocalObject).GetObjectID);
93 if not WifiManager.isWifiEnabled then
94 Memo1.Lines.Add('WiFi禁用')
95 else
96 begin
97 ConnectionInfo := WifiManager.getConnectionInfo;
98 Memo1.Lines.Add('连接信息');
99 Memo1.Lines.Add(' SSID: ' + JStringToString(ConnectionInfo.getSSID));
100 Memo1.Lines.Add(' BSSID: ' + JStringToString(ConnectionInfo.getBSSID));
101 Memo1.Lines.Add(' IPV4: ' + int2Ip(ConnectionInfo.getIpAddress));
102 Memo1.Lines.Add(' MAC address: ' + JStringToString(ConnectionInfo.getMacAddress));
103 ScanResults := WifiManager.getScanResults;
104 for I := 0 to ScanResults.size - 1 do
105 begin
106 Memo1.Lines.Add('');
107 Memo1.Lines.Add('检测到的接入点 ' + IntToStr(I));
108 ScanResult := TJScanResult.Wrap((ScanResults.get(I) as ILocalObject).GetObjectID);
109 Memo1.Lines.Add(' SSID: ' + JStringToString(ScanResult.SSID));
110 Memo1.Lines.Add(' BSSID: ' + JStringToString(ScanResult.BSSID));
111 Memo1.Lines.Add(' Capabilities: ' + JStringToString(ScanResult.capabilities));
112 Memo1.Lines.Add(' Frequency: ' + IntToStr(ScanResult.frequency) + 'MHz');
113 Memo1.Lines.Add(' Signal level: ' + IntToStr(ScanResult.level) + 'dBm');
114 end
115 end;
116 end;
117
118 end.