|
1 | 1 | # /usr/bin/env python3
|
2 | 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 | +
|
3 | 11 | pip install pyobjc
|
4 | 12 |
|
5 | 13 | from https://forums.developer.apple.com/forums/thread/748161?answerId=782574022#782574022
|
6 | 14 | """
|
7 | 15 |
|
8 | 16 | import objc
|
9 | 17 | import CoreLocation
|
| 18 | +from time import sleep |
| 19 | + |
| 20 | +# Need authorization to get BSSID |
10 | 21 |
|
11 |
| -location_manager = CoreLocation.CLLocationManager.alloc().init() |
12 |
| -location_manager.requestWhenInUseAuthorization() |
| 22 | +mgr = CoreLocation.CLLocationManager.alloc().init() |
| 23 | +# mgr = CoreLocation.CLLocationManager.new() |
| 24 | +# mgr.requestAlwaysAuthorization() |
| 25 | +mgr.startUpdatingLocation() |
13 | 26 |
|
| 27 | +max_wait = 10 |
| 28 | +# Get the current authorization status for Python |
| 29 | +for i in range(1, max_wait): |
| 30 | + s = mgr.authorizationStatus() |
| 31 | + if s in {3, 4}: |
| 32 | + print("Python has been authorized for location services") |
| 33 | + break |
| 34 | + if i == max_wait - 1: |
| 35 | + raise SystemExit("Unable to obtain authorization, exiting") |
| 36 | + print(f"Waiting for authorization... do you see the Location Services popup window? {s}") |
| 37 | + sleep(0.5) |
| 38 | + |
| 39 | +# Get the current location |
14 | 40 |
|
15 | 41 | bundle_path = "/System/Library/Frameworks/CoreWLAN.framework"
|
16 | 42 |
|
17 | 43 | objc.loadBundle("CoreWLAN", bundle_path=bundle_path, module_globals=globals())
|
18 | 44 |
|
19 | 45 | # https://developer.apple.com/documentation/corewlan/cwinterface
|
20 |
| -iface = CoreLocation.CWInterface.interface() |
| 46 | +# iface = CWInterface.interface() # not recommended, low-level |
| 47 | +# https://developer.apple.com/documentation/corewlan/cwwificlient |
| 48 | +iface = CoreLocation.CWWiFiClient.sharedWiFiClient().interface() |
| 49 | + |
| 50 | +print(f"WiFi interface {iface.interfaceName()}") |
| 51 | + |
21 | 52 |
|
22 |
| -print(iface.interfaceName()) |
23 |
| -print(iface.ssid()) |
| 53 | +# need to run once to warmup -- otherwise all SSID are "(null)" |
| 54 | +iface.scanForNetworksWithName_includeHidden_error_(None, True, None) |
24 | 55 |
|
25 |
| -networks, error = iface.scanForNetworksWithSSID_error_(None, None) |
| 56 | +networks, error = iface.scanForNetworksWithName_includeHidden_error_(None, True, None) |
26 | 57 |
|
27 | 58 | for network in networks:
|
28 |
| - print(network.ssid()) |
| 59 | + print(f"{network.ssid()} {network.bssid()} {network.rssi()} channel {network.channel()}") |
0 commit comments