SSID name

Just starting out? Need help? Post your questions and find answers here.
l1marik
User
User
Posts: 49
Joined: Tue Jun 30, 2020 6:22 am

SSID name

Post by l1marik »

Is any way how to get active connected WiFi SSID name (or IP)?

THX
infratec
Always Here
Always Here
Posts: 6866
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: SSID name

Post by infratec »

It is helpful if you tell us your OS

Or do you want a solution for all OSs :?:
l1marik
User
User
Posts: 49
Joined: Tue Jun 30, 2020 6:22 am

Re: SSID name

Post by l1marik »

Linux and Win.

THX
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: SSID name

Post by Shardik »

I have tested the following example successfully on these operating systems:
  • Linux Mint 19.3 'Tricia' x64 with Cinnamon and PB 6.00 x64
  • MacOS 11.7.4 'Big Sur' with PB 6.01 x64
  • MacOS 13.2.1 'Ventura' with PB 6.00 x64
  • Raspbian 11 'Bullseye' ARM 32-Bit with PB 6.00
  • Windows 10 Home x64 22H2 with PB 6.00 x86

Code: Select all

EnableExplicit

Define SSID.S

Procedure.S GetSSID()
  Protected SSID.S

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Define WiFiInterface.I
      Define WiFi.I
      
      WiFi = CocoaMessage(0, 0, "CWWiFiClient sharedWiFiClient")
      WiFiInterface = CocoaMessage(0, WiFi, "interface")
      SSID = PeekS(CocoaMessage(0, CocoaMessage(0, WiFiInterface, "ssid"),
        "UTF8String"), -1, #PB_UTF8)
    CompilerCase #PB_OS_Linux
      Define ConsoleCommand.I
      Define Result.S
      
      ConsoleCommand = RunProgram("iwgetid", "-r", "",
        #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide)
    CompilerCase #PB_OS_Windows
      Define ConsoleCommand.I
      Define Result.S
      
      ConsoleCommand = RunProgram("NetSh", "WLAN show profiles", "",
        #PB_Program_Open | #PB_Program_Read)
  CompilerEndSelect

  CompilerIf #PB_Compiler_OS <> #PB_OS_MacOS
    If ConsoleCommand = 0
      MessageRequester("Error",
        "Sorry, getting SSID failed!",
        #PB_MessageRequester_Error)
      ProcedureReturn ""
    Else
      While ProgramRunning(ConsoleCommand)
        If AvailableProgramOutput(ConsoleCommand)
          Result + ReadProgramString(ConsoleCommand)
        EndIf
      Wend
      
      CloseProgram(ConsoleCommand)
    EndIf
  CompilerEndIf

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      SSID = Result
    CompilerCase #PB_OS_Windows
      SSID = Trim(StringField(Result, 3, ":"))
  CompilerEndSelect

  ProcedureReturn SSID
EndProcedure

SSID = GetSSID() 

If SSID <> ""
  MessageRequester("Info", "SSID: " + SSID, #PB_MessageRequester_Info)
EndIf

Update: Flag #PB_Program_Hide added in RunProgram() as proposed by Lord. Thank you for your hint, Lord!
Last edited by Shardik on Wed Mar 22, 2023 8:50 pm, edited 1 time in total.
User avatar
Lord
Addict
Addict
Posts: 849
Joined: Tue May 26, 2009 2:11 pm

Re: SSID name

Post by Lord »

Please add for windows version #PB_Program_Hide as flag to RunProgram().
This will prevent the console window to flash up.
But it's just a cosmetic thing.
Image
Post Reply