Connecting mobile Wifi using WifiManager using Delphi classes to Android by Ivan Revelli

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,439
Credits
574
Connecting mobile Wifi using WifiManager using Delphi classes to Android
September 29, 2017 - Ivan Revelli
[SHOWTOGROUPS=4,20]
The goal was to connect programmatically to a specific WiFi, hence the need to manage the Java WifiManager class completely.

In the three main methods below we will have:
  1. recognition of the IP Address assigned to the wifi port
  2. List of available Wifi (those that the device recognizes)
  3. List of WiFi configurations saved on the device
  4. Ability to connect to a specific Wifi by creating the configuration if the same does not exist
NOTE: Dont forget, you need defined the "Permissions" in your project Options, to WIFI info!

Код:
unit unit111;

interface

uses
  system.sysutils,
  system.types,
  system.uitypes,
  system.classes,
  system.variants,
  fmx.types,
  fmx.controls,
  fmx.forms,
  fmx.graphics,
  fmx.dialogs,
  fmx.controls.presentation,
  fmx.stdctrls,
  fmx.edit,
  fmx.scrollbox,
  fmx.memo;

type
  tform4 = class(tform)
    button1: tbutton;
    checkbox1: tcheckbox;
    edsid: tedit;
    edpwd: tedit;
    memo1: tmemo;
    button2: tbutton;
    button3: tbutton;
    button4: tbutton;
    button5: tbutton;
    label1: tlabel;
    procedure button1click(sender: tobject);
    procedure button2click(sender: tobject);
    procedure button4click(sender: tobject);
    procedure button3click(sender: tobject);
    procedure button5click(sender: tobject);
    procedure formcreate(sender: tobject);
  private
    procedure getipaddress;
  public
    { public declarations }
  end;

var
  form4: tform4;

implementation

{$R *.fmx}

{
  'SharedActivityContext' is deprecated, use 'TAndroidHelper.Context' in RAD Studio 10.3.x Rio
}

uses
  androidapi.helpers,
  androidapi.jni.javatypes,
  androidapi.jni.graphicscontentviewtext,
  //
  androidapi.jni.net,
  androidapi.jnibridge,
  idipaddress;

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 := tandroidhelper.context.getsystemservice(tjcontext.javaclass.wifi_service);
  wifimanager    := tjwifimanager.wrap((wifimanagerobj as ilocalobject).getobjectid);
  wifiinfo       := wifimanager.getconnectioninfo();
  done           := false;

  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 := tandroidhelper.context.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 := tandroidhelper.context.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 := tandroidhelper.context.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;

end.
[/SHOWTOGROUPS]
 
Последнее редактирование: