Delphi Android WiFi Manager
L’obiettivo era quello di connettersi a livello programmatico ad una specifica WiFi da qui è sorta l’esigenza di gestire in modo completo la classe Java WifiManager.
Nei tre metodi principali riportati di seguito avremo:
- riconoscimento dell’IP Address assegnato alla porta wifi
- Elenco delle Wifi disponibili (quelle che il dispositivo riconosce)
- Elenco delle configurazioni WiFi salvate sul dispositivo
- Possibilità di connettersi ad una specifica Wifi creandone la configurazione se la stessa non esiste
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
function IntToInetAddr(ipInt : Integer) : String; var b : TBytes; Begin SetLength(b,4); b[3] := ((ipInt and $FF000000) shr 24); b[2] := ((ipInt and $00FF0000) shr 16); b[1] := ((ipInt and $0000FF00) shr 8); b[0] := (ipInt and $000000FF); result := b[0].ToString() + '.' + b[1].ToString() + '.' + b[2].ToString() + '.' + b[3].ToString(); End; (* https://stackoverflow.com/questions/36098871/how-to-search-and-connect-to-a-specific-wifi-network-in-android-programmatically http://blog.csdn.net/cmdasm/article/details/38357613 *) procedure TForm4.Button1Click(Sender: TObject); Var networkSSID : String; networkPass : String; WifiManagerObj: JObject; WifiManager: JWifiManager; AWifiConfiguration: JList; WifiInfo: JWifiInfo; WifiConfiguration,WinConf : JWifiConfiguration; networkId: Integer; Done: Boolean; iter: JIterator; begin // https://geekori.com/details.php?que_id=26676 WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE); WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID); WifiInfo := WifiManager.getConnectionInfo(); if CheckBox1.IsChecked then Begin networkSSID := '"' + edSid.Text + '"'; networkPass := '"' + edPwd.Text + '"'; End Else Begin networkSSID := edSid.Text; networkPass := edPwd.Text; End; WifiConfiguration := nil; networkId := -1; iter := WifiManager.getConfiguredNetworks().iterator; while iter.hasNext do Begin WinConf := TJWifiConfiguration.Wrap((iter.next as ILocalObject).GetObjectID); //Memo1.lines.add(WinConf.networkId.ToString + ' --> ' + JStringToString(WinConf.SSID)); if JStringToString(WinConf.SSID) = networkSSID then Begin WifiConfiguration := WinConf; networkId := WinConf.networkId; End; End; if WifiConfiguration = nil then Begin Memo1.lines.add('Network not found try to create configuration'); //WifiConfiguration := TJWifiConfiguration.Create; WifiConfiguration := TJWifiConfiguration.JavaClass.init; WifiConfiguration.SSID := StringToJString(networkSSID); WifiConfiguration.preSharedKey := StringToJString(networkPass); WifiConfiguration.hiddenSSID := true; WifiConfiguration.allowedAuthAlgorithms.&set(TJWifiConfiguration_AuthAlgorithm.JavaClass.OPEN); WifiConfiguration.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.TKIP); WifiConfiguration.allowedGroupCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.CCMP); WifiConfiguration.allowedKeyManagement.&set(TJWifiConfiguration_KeyMgmt.JavaClass.WPA_PSK); WifiConfiguration.allowedPairwiseCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.TKIP); WifiConfiguration.allowedPairwiseCiphers.&set(TJWifiConfiguration_GroupCipher.JavaClass.CCMP); WifiConfiguration.status := TJNsdManager.JavaClass.NSD_STATE_ENABLED; networkId := WifiManager.addNetwork(WifiConfiguration); End; if networkId = -1 then begin Done := False; Memo1.lines.add('ERROR: networkId = -1'); end Else Memo1.lines.add('networkId = ' + networkId.ToString()); if WifiManager.disconnect = False then begin Done := False; Memo1.lines.add('ERROR: not disconnected'); end; if done then Begin AWifiConfiguration := WifiManager.getConfiguredNetworks(); done := False; Memo1.lines.add(' Configured network size: ' + AWifiConfiguration.size.ToString()); iter := AWifiConfiguration.iterator; while iter.hasNext do Begin WinConf := TJWifiConfiguration.Wrap((iter.next as ILocalObject).GetObjectID); Memo1.lines.add(WinConf.networkId.ToString + ' --> ' + JStringToString(WinConf.SSID)); if WinConf.networkId = networkId then done := WifiManager.enableNetwork(networkId, true) Else WifiManager.disableNetwork(WinConf.networkId); End; // done := WifiManager.enableNetwork(networkId, true); WifiManager.reconnect; // done := WifiManager.enableNetwork(AWifiConfiguration.get(networkId).hashCode, true); End; if Done then Begin Memo1.lines.add('Connected'); End Else Begin Memo1.lines.add('Not Connected to selected network'); End; GetIpAddress; end; procedure TForm4.Button2Click(Sender: TObject); 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(); Memo1.Lines.Clear; Memo1.Lines.Add('Wifi Enabled: ' + WifiManager.isWifiEnabled.ToString); Memo1.Lines.Add('Wifi State: ' + WifiManager.getWifiState.ToString); Memo1.Lines.Add('Ping Supplicant: ' + WifiManager.pingSupplicant.ToString); 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)); GetIpAddress; end; procedure TForm4.Button3Click(Sender: TObject); var WifiManagerObj: JObject; WifiManager: JWifiManager; ScanResult: JScanResult; i: Integer; begin WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE); WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID); Memo1.Lines.Clear; for i := 0 to WifiManager.getScanResults.size - 1 do begin ScanResult := TJScanResult.Wrap((WifiManager.getScanResults.get(i) as ILocalObject).GetObjectID); memo1.Lines.Add( 'SSID: ' + JStringToString(ScanResult.SSID)); memo1.Lines.Add( 'frequency: ' + ScanResult.frequency.ToString + 'MHz' + ' level: ' + ScanResult.level.ToString + 'dBm)'); // ListBox1.AddObject(ListBoxItem); end; // ListBox1.EndUpdate; end; procedure TForm4.Button4Click(Sender: TObject); begin // networkSSID := '"WiTmm3"'; // networkPass := '"simona1987"'; edSid.Text := 'WiTmm3'; edPwd.Text := 'simona1987'; end; procedure TForm4.Button5Click(Sender: TObject); begin Memo1.Lines.Clear; end; procedure TForm4.GetIpAddress; var WifiManagerObj: JObject; WifiManager: JWifiManager; WifiInfo: JWifiInfo; begin label1.text := '127.0.0.1'; { TODO : Trovare il modo di leggere il corrente ipaddress del dispositivo } try WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE); WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID); WifiInfo := WifiManager.getConnectionInfo; label1.text := IntToInetAddr(WifiInfo.getIpAddress); if label1.text = '0.0.0.0' then label1.text := '127.0.0.1'; except on e: exception do begin label1.text := '127.0.0.1'; raise Exception.Create(e.Message); end; end; end; procedure TForm4.FormCreate(Sender: TObject); Begin GetIpAddress; End; |