Skip to content

Commit 06d79cf

Browse files
committed
working on macOS example with CoreLocation
1 parent 25b71b0 commit 06d79cf

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

macos_corelocation.py

+38-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,59 @@
11
# /usr/bin/env python3
22
"""
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+
311
pip install pyobjc
412
513
from https://forums.developer.apple.com/forums/thread/748161?answerId=782574022#782574022
614
"""
715

816
import objc
917
import CoreLocation
18+
from time import sleep
19+
20+
# Need authorization to get BSSID
1021

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()
1326

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
1440

1541
bundle_path = "/System/Library/Frameworks/CoreWLAN.framework"
1642

1743
objc.loadBundle("CoreWLAN", bundle_path=bundle_path, module_globals=globals())
1844

1945
# 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+
2152

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)
2455

25-
networks, error = iface.scanForNetworksWithSSID_error_(None, None)
56+
networks, error = iface.scanForNetworksWithName_includeHidden_error_(None, True, None)
2657

2758
for network in networks:
28-
print(network.ssid())
59+
print(f"{network.ssid()} {network.bssid()} {network.rssi()} channel {network.channel()}")

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ classifiers = ["Development Status :: 5 - Production/Stable",
1717
"Topic :: System :: Networking",
1818
"Topic :: Utilities"
1919
]
20-
requires-python = ">=3.10"
20+
requires-python = ">=3.9"
2121
dynamic = ["readme","version"]
2222
dependencies = ["requests", "pandas"]
2323

src/mozloc/__main__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
help="Mozilla location services URL--don't use this default test key",
2929
default="https://location.services.mozilla.com/v1/geolocate?key=test",
3030
)
31-
p.add_argument("-d", "--dump", help="print raw data to console without logging", action="store_true")
31+
p.add_argument(
32+
"-d", "--dump", help="print raw data to console without logging", action="store_true"
33+
)
3234
p.add_argument("-i", "--infile", help="use raw text saved from command line")
3335
args = p.parse_args()
3436

0 commit comments

Comments
 (0)