Could anyone please tell me how to get the Wi-Fi ID name that the user is currently on?

Could anyone please tell me how to get the Wi-Fi ID name that the user is currently on?
Using XE5 for Android.
#WiFi #Delphi #XE5 #Network

Comments

  1. Something like what you'd find in this unit, I'd imagine:

    unit Network;

    interface

    function IsConnected: Boolean;

    function IsWiFiConnected: Boolean;

    function IsMobileConnected: Boolean;

    function WiFiSSID: string;

    implementation

    uses
      System.SysUtils,
      Androidapi.JNIBridge,
      Androidapi.JNI.GraphicsContentViewText,
      Androidapi.JNI.JavaTypes,
      FMX.Helpers.Android, Misc;

    type
      JConnectivityManager = interface;
      JNetworkInfo = interface;
      JWifiManager = interface;
      JWifiInfo = interface;

      JNetworkInfoClass = interface(JObjectClass)
      ['{E92E86E8-0BDE-4D5F-B44E-3148BD63A14C}']
      end;

      [JavaSignature('android/net/NetworkInfo')]
      JNetworkInfo = interface(JObject)
      ['{6DF61A40-8D17-4E51-8EF2-32CDC81AC372}']
        {Methods}
        function isAvailable: Boolean; cdecl;
        function isConnected: Boolean; cdecl;
        function isConnectedOrConnecting: Boolean; cdecl;
      end;
      TJNetworkInfo = class(TJavaGenericImport) end;

      JConnectivityManagerClass = interface(JObjectClass)
      ['{E03A261F-59A4-4236-8CDF-0068FC6C5FA1}']
        {Property methods}
        function _GetTYPE_WIFI: Integer; cdecl;
        function _GetTYPE_WIMAX: Integer; cdecl;
        function _GetTYPE_MOBILE: Integer; cdecl;
        {Properties}
        property TYPE_WIFI: Integer read _GetTYPE_WIFI;
        property TYPE_WIMAX: Integer read _GetTYPE_WIMAX;
        property TYPE_MOBILE: Integer read _GetTYPE_MOBILE;
      end;

      [JavaSignature('android/net/ConnectivityManager')]
      JConnectivityManager = interface(JObject)
      ['{1C4C1873-65AE-4722-8EEF-36BBF423C9C5}']
        {Methods}
        function getActiveNetworkInfo: JNetworkInfo; cdecl;
        function getNetworkInfo(networkType: Integer): JNetworkInfo; cdecl;
      end;
      TJConnectivityManager = class(TJavaGenericImport) end;

      JWifiManagerClass = interface(JObjectClass)
      ['{609C773B-E42E-40F1-8C12-AB38DD38DBB1}']
      end;

      [JavaSignature('android/net/wifi/WifiManager')]
      JWifiManager = interface(JObject)
      ['{051511E1-6B00-4635-8BCA-5F1A4DD9A473}']
        {Methods}
        function getConnectionInfo: JWifiInfo; cdecl;
      end;
      TJWifiManager = class(TJavaGenericImport) end;

      JWifiInfoClass = interface(JObjectClass)
      ['{DBFE7F9D-5B77-49BC-AD59-54CE78227A88}']
      end;

      [JavaSignature('android/net/wifi/WifiInfo')]
      JWifiInfo = interface(JObject)
      ['{5120EF5E-9878-437F-8524-70CEEB07A99C}']
        {Methods}
        function getSSID: JString; cdecl;
      end;
      TJWifiInfo = class(TJavaGenericImport) end;

    function GetConnectivityManager: JConnectivityManager;
    var
      ConnectivityServiceNative: JObject;
    begin
      if not HasPermission('android.permission.ACCESS_NETWORK_STATE') then
        raise Exception.Create('You need the ACCESS_NETWORK_STATE permission');
      ConnectivityServiceNative := SharedActivityContext.getSystemService(TJContext.JavaClass.CONNECTIVITY_SERVICE);
      if not Assigned(ConnectivityServiceNative) then
        raise Exception.Create('Could not locate Connectivity Service');
      Result := TJConnectivityManager.Wrap(
        (ConnectivityServiceNative as ILocalObject).GetObjectID);
      if not Assigned(Result) then
        raise Exception.Create('Could not access Connectivity Manager');
    end;

    function IsConnected: Boolean;
    var
      ConnectivityManager: JConnectivityManager;
      ActiveNetwork: JNetworkInfo;
    begin
      ConnectivityManager := GetConnectivityManager;

    ReplyDelete
  2. ActiveNetwork := ConnectivityManager.getActiveNetworkInfo;
      Result := Assigned(ActiveNetwork) and ActiveNetwork.isConnected;
    end;

    function IsWiFiConnected: Boolean;
    var
      ConnectivityManager: JConnectivityManager;
      WiFiNetwork: JNetworkInfo;
    begin
      ConnectivityManager := GetConnectivityManager;
      WiFiNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_WIFI);
      Result := WiFiNetwork.isConnected;
    end;

    function IsMobileConnected: Boolean;
    var
      ConnectivityManager: JConnectivityManager;
      MobileNetwork: JNetworkInfo;
    begin
      ConnectivityManager := GetConnectivityManager;
      MobileNetwork := ConnectivityManager.getNetworkInfo(TJConnectivityManager.JavaClass.TYPE_MOBILE);
      Result := MobileNetwork.isConnected;
    end;

    function GetWifiManager: JWifiManager;
    var
      WifiServiceNative: JObject;
    begin
      if not HasPermission('android.permission.ACCESS_WIFI_STATE') then
        raise Exception.Create('You need the ACCESS_WIFI_STATE permission');
      WifiServiceNative := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
      if not Assigned(WifiServiceNative) then
        raise Exception.Create('Could not locate Wifi Service');
      Result := TJWifiManager.Wrap(
        (WifiServiceNative as ILocalObject).GetObjectID);
      if not Assigned(Result) then
        raise Exception.Create('Could not access Wifi Manager');
    end;

    function WiFiSSID: string;
    var
      WiFiManager: JWifiManager;
      ConnectionInfo: JWifiInfo;
    begin
      Result := '';
      if IsWiFiConnected then
      begin
        WifiManager := GetWifiManager;
        ConnectionInfo := WifiManager.getConnectionInfo;
        if Assigned(ConnectionInfo) then
          Result := JStringToString(ConnectionInfo.getSSID)
      end;
    end;

    end.

    Oh, for the code layout and syntax highlighting found on Stack Overflow...

    BTW, just strip out the HasPermission calls and make sure you have the permissions.

    ReplyDelete
  3. Hi Brian,

    Thanks very much for this code, however I'm still having a problem getting it to work. I added a memo and a button and the event.

    procedure TForm1.Button1Click(Sender: TObject);
    begin
       if IsConnected then
          Memo1.Lines.Add(WiFiSSID);
    end;

    But I get an exception ( Project GetWiFi.apk raised exception class Segment fault(11).)

    It doesn't seem to like ConnectivityManager.getActiveNetworkInfo or ConnectivityManager.getNetworkInfo but ConnectivityManager := GetConnectivityManager; works fine.

    I have to remove "Misc" from your uses section and the HasPermission calls but I guess that's not the problem.
    I have set the correct permissions (I've even tried setting all permissions)

    Below is the entire unit, hopefully you'll spot that I've done something silly.

    Many thanks
    Andy

    unit uwifi;

    interface

    uses
      System.SysUtils, System.Types, System.UITypes, System.Classes,
      System.Variants,
      FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
      FMX.Memo, FMX.StdCtrls;

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

    function IsConnected: Boolean;
    function IsWiFiConnected: Boolean;
    function IsMobileConnected: Boolean;
    function WiFiSSID: string;

    var
      Form1: TForm1;

    implementation

    {$R *.fmx}

    uses
      Androidapi.JNIBridge,
      Androidapi.JNI.GraphicsContentViewText,
      Androidapi.JNI.JavaTypes,
      FMX.Helpers.Android;

    type
      JConnectivityManager = interface;
      JNetworkInfo = interface;
      JWifiManager = interface;
      JWifiInfo = interface;

      JNetworkInfoClass = interface(JObjectClass)
        ['{E92E86E8-0BDE-4D5F-B44E-3148BD63A14C}']
      end;

      [JavaSignature('android/net/NetworkInfo')]
      JNetworkInfo = interface(JObject)
        ['{6DF61A40-8D17-4E51-8EF2-32CDC81AC372}']
        { Methods }
        function isAvailable: Boolean; cdecl;
        function IsConnected: Boolean; cdecl;
        function isConnectedOrConnecting: Boolean; cdecl;
      end;

      TJNetworkInfo = class(TJavaGenericImport)
      end;

      JConnectivityManagerClass = interface(JObjectClass)
        ['{E03A261F-59A4-4236-8CDF-0068FC6C5FA1}']
        { Property methods }
        function _GetTYPE_WIFI: Integer; cdecl;
        function _GetTYPE_WIMAX: Integer; cdecl;
        function _GetTYPE_MOBILE: Integer; cdecl;
        { Properties }
        property TYPE_WIFI: Integer read _GetTYPE_WIFI;
        property TYPE_WIMAX: Integer read _GetTYPE_WIMAX;
        property TYPE_MOBILE: Integer read _GetTYPE_MOBILE;
      end;

      [JavaSignature('android/net/ConnectivityManager')]
      JConnectivityManager = interface(JObject)
        ['{1C4C1873-65AE-4722-8EEF-36BBF423C9C5}']
        { Methods }
        function getActiveNetworkInfo: JNetworkInfo; cdecl;
        function getNetworkInfo(networkType: Integer): JNetworkInfo; cdecl;
      end;

      TJConnectivityManager = class(TJavaGenericImport    JConnectivityManager>)
      end;

      JWifiManagerClass = interface(JObjectClass)
        ['{609C773B-E42E-40F1-8C12-AB38DD38DBB1}']
      end;

      [JavaSignature('android/net/wifi/WifiManager')]
      JWifiManager = interface(JObject)
        ['{051511E1-6B00-4635-8BCA-5F1A4DD9A473}']
        { Methods }
        function getConnectionInfo: JWifiInfo; cdecl;
      end;

      TJWifiManager = class(TJavaGenericImport)
      end;

    ReplyDelete
  4. JWifiInfoClass = interface(JObjectClass)
        ['{DBFE7F9D-5B77-49BC-AD59-54CE78227A88}']
      end;

      [JavaSignature('android/net/wifi/WifiInfo')]
      JWifiInfo = interface(JObject)
        ['{5120EF5E-9878-437F-8524-70CEEB07A99C}']
        { Methods }
        function getSSID: JString; cdecl;
      end;

      TJWifiInfo = class(TJavaGenericImport)
      end;

    function GetConnectivityManager: JConnectivityManager;
    var
      ConnectivityServiceNative: JObject;
    begin
      ConnectivityServiceNative := SharedActivityContext.getSystemService
        (TJContext.JavaClass.CONNECTIVITY_SERVICE);
      if not Assigned(ConnectivityServiceNative) then
        raise Exception.Create('Could not locate Connectivity Service');
      Result := TJConnectivityManager.Wrap
        ((ConnectivityServiceNative as ILocalObject).GetObjectID);
      if not Assigned(Result) then
        raise Exception.Create('Could not access Connectivity Manager');
    end;

    function IsConnected: Boolean;
    var
      ConnectivityManager: JConnectivityManager;
      ActiveNetwork: JNetworkInfo;
    begin
      ConnectivityManager := GetConnectivityManager;
      ActiveNetwork := ConnectivityManager.getActiveNetworkInfo;
      Result := Assigned(ActiveNetwork) and ActiveNetwork.IsConnected;
    end;

    function IsWiFiConnected: Boolean;
    var
      ConnectivityManager: JConnectivityManager;
      WiFiNetwork: JNetworkInfo;
    begin
      ConnectivityManager := GetConnectivityManager;
      WiFiNetwork := ConnectivityManager.getNetworkInfo
        (TJConnectivityManager.JavaClass.TYPE_WIFI);
      Result := WiFiNetwork.IsConnected;
    end;

    function IsMobileConnected: Boolean;
    var
      ConnectivityManager: JConnectivityManager;
      MobileNetwork: JNetworkInfo;
    begin
      ConnectivityManager := GetConnectivityManager;
      MobileNetwork := ConnectivityManager.getNetworkInfo
        (TJConnectivityManager.JavaClass.TYPE_MOBILE);
      Result := MobileNetwork.IsConnected;
    end;

    function GetWifiManager: JWifiManager;
    var
      WifiServiceNative: JObject;
    begin
      WifiServiceNative := SharedActivityContext.getSystemService
        (TJContext.JavaClass.WIFI_SERVICE);
      if not Assigned(WifiServiceNative) then
        raise Exception.Create('Could not locate Wifi Service');
      Result := TJWifiManager.Wrap((WifiServiceNative as ILocalObject).GetObjectID);
      if not Assigned(Result) then
        raise Exception.Create('Could not access Wifi Manager');
    end;

    function WiFiSSID: string;
    var
      WiFiManager: JWifiManager;
      ConnectionInfo: JWifiInfo;
    begin
      Result := '';
      if IsWiFiConnected then
      begin
        WiFiManager := GetWifiManager;
        ConnectionInfo := WiFiManager.getConnectionInfo;
        if Assigned(ConnectionInfo) then
          Result := JStringToString(ConnectionInfo.getSSID)
      end;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
       if IsConnected then
          Memo1.Lines.Add(WiFiSSID);
    end;

    end.

    ReplyDelete
  5. I can't spot anything immediately, and my test app on my phone behaves as expected with my code. I'll try & remember to have a quick look later when done with the day job at the office here.
    It may signal a false assumption on my part. Anyone else care to check what happens when running the code?

    ReplyDelete
  6. Oh yeah, and this discussion is in the wrong forum :o)

    ReplyDelete
  7. Thanks Brian, I appreciate that. sorry, I'll use the correct forum next time :-)

    ReplyDelete
  8. Heh, yeah, slowly back and forth with my arms wrapped around my torso ;o)

    ReplyDelete
  9. Hi Brian, just a bit of extra info. I tried this on my S4 and my Nexus 7 and get the same exception in the same place.

    ReplyDelete
  10. Andy Kerr Annoying. I just typed in a reply and Internet Exploder (on machines at this client) lost it all.
    Anyway, I was gonna say that given the code appears to work nicely on my device, it may rely on some detective work from you. Perhaps do additional nil checking. Maybe use FMX.Types and add in liberal calls to Log.d('some progress message') so you can see the exact circumstances of the crash, and report back.
    As I say, my test app calls all of the routines in that unit and gets clean results, so I'm a bit puzzled right now.
    Also, if anyone else tries the code, we may get more ideas.

    ReplyDelete
  11. Hi Brian,
    I have no idea how to do what you're suggesting. I will look into doing it though. In the meantime I've zipped my project here: https://www.dropbox.com/s/zktp74butesfmaw/GetWiFi.zip and wondered if you could compile it on your system and then see if you get the same result. Possibly email me back the apk.

    I'd be grateful if others could also try it so please fee free. Please let me know if it works on your system but please don't all email your .apk's.
    Thanks
    Andy

    ReplyDelete
  12. LOL. My bad.

    Seems one of my imports wasn't right.

    In JNetworkInfo, change IsConnected to isConnected.

    Since Java is case sensitive and the Java bridge uses what you write to look up methods on the Java side, case is key in those imports.

    Clearly my testing leaves something to be desired ;o)

    ReplyDelete
  13. I take that back. Your bad. My import unit was correct :o) You somehow upper-cased an "i".

    Just checking I was paying attention, I'm sure.

    My testing was exemplary.

    ReplyDelete
  14. :-) Sorry, I don't know how I managed to do that. I'll fix it in the morning. Cheers you're a star.

    ReplyDelete
  15. Thanks Brian, that now works a treat...
    Just as an aside. I think I remember that you used to run (or had some involvement) with the Borland bulletin board back in the 90's :-) Remember bulletin boards, happy (2k4 or 14k4) days.. I think we may have briefly met too, maybe at one of the Borland / Inprise demos (Manchester or London) :-)

    ReplyDelete
  16. Yes, I used to cover CIX, until I was relieved of my job. I did used to get around once I was unleashed on the public :o)
    Glad to hear it's all up and running now.

    ReplyDelete
  17. CIX. That takes me back, I was system1@cix.

    ReplyDelete

Post a Comment