Developer Andrey Yefimov has a blog post up in Russian with an example of reading the WIFI state using the JNI on Android in Delphi XE6 Firemonkey. The example uses the following Android permissions: ACCESS_WIFI_STATE, CHANGE_WIFI_STATE, and
CHANGE_WIFI_MULTICAST_STATE. The wrapper makes the following information available: wifi enabled, wifi state, and ping supplicant. Plus the following additional information: BSSID, HiddenSSID, ip address, link speed, mac address, network ID, RSSI, SSID, and supplicant state. It also shows an example of listing the access point SSIDs which are in range of your Android device. The wrapper is a unit called Androidapi.JNI.Net.Wifi. Here is the sample code for reading the wifi information with the wrapper:
var
WifiManagerObj: JObject;
WifiManager: JWifiManager;
WifiInfo: JWifiInfo;
begin
WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID);
WifiInfo := WifiManager.getConnectionInfo();
Label1.Text := 'Wifi Enabled: ' + WifiManager.isWifiEnabled.ToString;
Label2.Text := 'Wifi State: ' + WifiManager.getWifiState.ToString;
Label3.Text := 'Ping Supplicant: ' + WifiManager.pingSupplicant.ToString;
Memo1.Lines.Clear;
Memo1.Lines.Add('BSSID: ' + JStringToString(WifiInfo.getBSSID));
Memo1.Lines.Add('HiddenSSID: ' + WifiInfo.getHiddenSSID.ToString);
Memo1.Lines.Add('IpAddress: ' + WifiInfo.getIpAddress.ToString);
Memo1.Lines.Add('LinkSpeed: ' + WifiInfo.getLinkSpeed.ToString + 'Mbps');
Memo1.Lines.Add('MacAddress: ' + JStringToString(WifiInfo.getMacAddress));
Memo1.Lines.Add('NetworkId: ' + WifiInfo.getNetworkId.ToString);
Memo1.Lines.Add('Rssi: ' + WifiInfo.getRssi.ToString + 'dBm');
Memo1.Lines.Add('SSID: ' + JStringToString(WifiInfo.getSSID));
Memo1.Lines.Add('SupplicantState: ' + JStringToString(WifiInfo.getSupplicantState.toString));
The original blog post is in Russian so use Google Chrome to translate. This code should also work in AppMethod.
Is there any way to scan available networks and list them with RSSI?
use
ip := WifiInfo.GetIPAddress;
Result := Format(‘%d.%d.%d.%d’, [ip and $FF, ip shr 8 and $FF, ip shr 16 and $FF, ip shr 24 and $FF]);
for get normal ip address
Thanks for format tips. I was “lost” without it.
muito bom! deu certo, obrigado!
Hello. Can you give example of Delphi XE8 code for use TrafficStats and getTotalTxBytes to monitor WIFI transfer? Thanks in advance. I’m totally Android newbie :/ Regards.
This code appears to work fine for Lollipop, but not Oreo. Any tips for getting this to work on Oreo?