Page 1 of 1

SSID name

Posted: Fri Mar 17, 2023 8:11 pm
by l1marik
Is any way how to get active connected WiFi SSID name (or IP)?

THX

Re: SSID name

Posted: Fri Mar 17, 2023 9:40 pm
by infratec
It is helpful if you tell us your OS

Or do you want a solution for all OSs :?:

Re: SSID name

Posted: Sat Mar 18, 2023 2:20 pm
by l1marik
Linux and Win.

THX

Re: SSID name

Posted: Sat Mar 18, 2023 11:48 pm
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!

Re: SSID name

Posted: Sun Mar 19, 2023 11:35 am
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.