|
| 1 | +# /usr/bin/env python3 |
| 2 | +""" |
| 3 | +MUST USE SYSTEM PYTHON /usr/bin/python3 |
| 4 | +DON'T USE sudo as LocationServices Python won't pop up. |
| 5 | +
|
| 6 | +TODO: even with all this, BSSID is still (null) / None |
| 7 | +
|
| 8 | +LocationServices Python app becomes available to enable in |
| 9 | +Settings > Privacy > Location Services |
| 10 | +
|
| 11 | +pip install pyobjc |
| 12 | +
|
| 13 | +from https://forums.developer.apple.com/forums/thread/748161?answerId=782574022#782574022 |
| 14 | +
|
| 15 | +Ref: https://docs.python.org/3/using/mac.html#gui-programming |
| 16 | +""" |
| 17 | + |
| 18 | +import CoreLocation |
| 19 | +import time |
| 20 | +import logging |
| 21 | +import pandas |
| 22 | + |
| 23 | +import objc |
| 24 | + |
| 25 | + |
| 26 | +def config_check() -> bool: |
| 27 | + """ |
| 28 | + Need authorization to get BSSID |
| 29 | + """ |
| 30 | + |
| 31 | + mgr = CoreLocation.CLLocationManager.alloc().init() |
| 32 | + # mgr = CoreLocation.CLLocationManager.new() |
| 33 | + # mgr.requestAlwaysAuthorization() |
| 34 | + mgr.startUpdatingLocation() |
| 35 | + |
| 36 | + max_wait = 10 |
| 37 | + # Get the current authorization status for Python |
| 38 | + # https://stackoverflow.com/a/75843844 |
| 39 | + for i in range(1, max_wait): |
| 40 | + s = mgr.authorizationStatus() |
| 41 | + if s in {3, 4}: |
| 42 | + print("Python has been authorized for location services") |
| 43 | + return True |
| 44 | + if i == max_wait - 1: |
| 45 | + logging.error("Unable to obtain authorization") |
| 46 | + return False |
| 47 | + print(f"Waiting for authorization... do you see the Location Services popup window? {s}") |
| 48 | + time.sleep(0.5) |
| 49 | + |
| 50 | + return False |
| 51 | + |
| 52 | + |
| 53 | +def get_signal(networks): |
| 54 | + # Get the current location |
| 55 | + |
| 56 | + dat: list[dict[str, str]] = [] |
| 57 | + |
| 58 | + for network in networks: |
| 59 | + # print(f"{network.ssid()} {network.bssid()} {network.rssi()} channel {network.channel()}") |
| 60 | + d = {"ssid": network.ssid(), "signalStrength": network.rssi()} |
| 61 | + if network.bssid() is not None: |
| 62 | + d["macAddress"] = network.bssid() |
| 63 | + dat.append(d) |
| 64 | + |
| 65 | + return pandas.DataFrame(dat) |
| 66 | + |
| 67 | + |
| 68 | +def scan_signal(): |
| 69 | + |
| 70 | + bundle_path = "/System/Library/Frameworks/CoreWLAN.framework" |
| 71 | + |
| 72 | + objc.loadBundle("CoreWLAN", bundle_path=bundle_path, module_globals=globals()) |
| 73 | + |
| 74 | + # https://developer.apple.com/documentation/corewlan/cwinterface |
| 75 | + # iface = CWInterface.interface() # not recommended, low-level |
| 76 | + # https://developer.apple.com/documentation/corewlan/cwwificlient |
| 77 | + iface = CoreLocation.CWWiFiClient.sharedWiFiClient().interface() |
| 78 | + |
| 79 | + logging.info(f"WiFi interface {iface.interfaceName()}") |
| 80 | + |
| 81 | + # need to run once to warmup -- otherwise all SSID are "(null)" |
| 82 | + iface.scanForNetworksWithName_includeHidden_error_(None, True, None) |
| 83 | + |
| 84 | + networks, error = iface.scanForNetworksWithName_includeHidden_error_(None, True, None) |
| 85 | + |
| 86 | + return networks |
0 commit comments