Obtener info de la conexion wifi Android

Esta unit obtiene información de la conexión wifi y de los puntos de acceso disponibles desde vuestro móvil. Recordar que desde Project->Options->User Permissions hay que habilitarle el permiso ACCESS_WIFI_STATE


unit Unit2;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo,Androidapi.Helpers,
  Androidapi.JNI.JavaTypes, Androidapi.JNIBridge,
  Androidapi.JNI.GraphicsContentViewText, Androidapi.jni.net;

type
  TForm2 = class(TForm)
    Memo1: TMemo;
    ToolBar1: TToolBar;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm2.Button1Click(Sender: TObject);

var
  Service: JObject;
  WifiManager: JWifiManager;
  ConnectionInfo: JWifiInfo;
  ScanResults: JList;
  ScanResult: JScanResult;
  I: Integer;
begin

  Memo1.Visible := true;
  Memo1.Lines.Clear;

  Service := SharedActivity.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
    if not Assigned(Service) then
    raise Exception.Create('Could not locate Wifi Service');
  WifiManager := TJWifiManager.Wrap((Service as ILocalObject).GetObjectID);
  //tJwifimanager esta en la unit
  if not WifiManager.isWifiEnabled then
    Memo1.Lines.Add('Wifi is disabled')
  else
  begin
    ConnectionInfo := WifiManager.getConnectionInfo;
    Memo1.Lines.Add('Connection info');
    Memo1.Lines.Add('  SSID: ' + JStringToString(ConnectionInfo.getSSID));
    Memo1.Lines.Add('  BSSID: ' + JStringToString(ConnectionInfo.getBSSID));
    Memo1.Lines.Add('  MAC address: ' + JStringToString(ConnectionInfo.getMacAddress));
    ScanResults := WifiManager.getScanResults;
    for I := 0 to ScanResults.size - 1 do
    begin
      Memo1.Lines.Add('');
      Memo1.Lines.Add('Detected access point ' + IntToStr(I));
      ScanResult := TJScanResult.Wrap((ScanResults.get(I) as ILocalObject).GetObjectID);
      Memo1.Lines.Add('  SSID: ' + JStringToString(ScanResult.SSID));
      Memo1.Lines.Add('  BSSID: ' + JStringToString(ScanResult.BSSID));
      Memo1.Lines.Add('  Capabilities: ' + JStringToString(ScanResult.capabilities));
      Memo1.Lines.Add('  Frequency: ' + IntToStr(ScanResult.frequency) + 'MHz');
      Memo1.Lines.Add('  Signal level: ' + IntToStr(ScanResult.level) + 'dBm');
    end
  end
end;

end.



Libros:

No hay comentarios:

Publicar un comentario