How to enable location services for CoreWLAN pyobjc wrapper to get bssid?

275 Views Asked by At

I'm using pyobjc wrapper to scan for networks:

import objc
objc.loadBundle(
    "CoreWLAN",
    bundle_path="/System/Library/Frameworks/CoreWLAN.framework",
    module_globals=globals()
)
from CoreWLAN import CWNetwork, CWWiFiClient
client = CWWiFiClient.sharedWiFiClient()
iface = client.interfaceWithName_("en0")
networks, error = iface.scanForNetworksWithName_error_(
    None,
    None,
)
print(networks)

and get

{( <CWNetwork: 0x7ff7a64040d0> [ssid=FRITZ!Box 7520 HT, bssid=(null), security=WPA2 Personal, rssi=-84, channel=<CWChannel: 0x7ff79644b800> [channelNumber=116(5GHz), channelWidth={80MHz}], ibss=0], <CWNetwork: 0x7ff7a64447d0> [ssid=FRITZ$Box 7412, bssid=(null), security=WPA/WPA2 Personal, rssi=-52, channel=<CWChannel: 0x7ff7964054c0> [channelNumber=11(2GHz), channelWidth={20MHz}], ibss=0], ... )}

As you might see, bssid is null. Also, as pointed here, it's expected behaviour: https://developer.apple.com/forums/thread/119490?answerId=387785022#387785022

How do I enable location services for this code to be able to get bssid?

UPD: Found a solution! github.com/ronaldoussoren/pyobjc/issues/484

1

There are 1 best solutions below

1
On

This is the script I used to generate a location services permission request for Python:

import CoreLocation
from time import sleep

location_manager = CoreLocation.CLLocationManager.alloc().init()
location_manager.startUpdatingLocation()

max_wait = 60
# Get the current authorization status for Python
for i in range(1, max_wait):
    authorization_status = location_manager.authorizationStatus()
    if authorization_status == 3 or authorization_status == 4:
        print("Python has been authorized for location services")
        break
    if i == max_wait-1:
        exit("Unable to obtain authorization, exiting")
    sleep(1)

coord = location_manager.location().coordinate()
lat, lon = coord.latitude, coord.longitude
print("Your location is %f, %f" % (lat, lon))

In my testing with MacOS 13.2 if Python has location services permissions then you can see the BSSID. This seems to be a bug fix as this should have been the expected behavior since they started hiding the BSSID in MacOS 10.15.