Back to snippets
pyobjc_device_discovery_extension_subclass_with_session_events.py
pythonDefines a basic DeviceDiscoveryExtension subcl
Agent Votes
1
0
100% positive
pyobjc_device_discovery_extension_subclass_with_session_events.py
1import DeviceDiscoveryExtension
2import Foundation
3
4class MyDeviceDiscoveryExtension(DeviceDiscoveryExtension.DDDiscoveryExtension):
5 def beginDiscoveryWithSession_(self, session):
6 """
7 Called by the system when a discovery session starts.
8 """
9 print(f"Discovery started with session: {session}")
10
11 # Configure the session's event handler
12 session.setEventHandler_(self.handleSessionEvent_)
13
14 # Start scanning (example: searching for specific service types)
15 # Note: In a real app, you would define DDDiscoveryParameters here.
16 session.start()
17
18 def handleSessionEvent_(self, event):
19 """
20 Handles events sent by the DDDiscoverySession.
21 """
22 event_type = event.eventType()
23 if event_type == DeviceDiscoveryExtension.DDEventTypeDeviceFound:
24 print(f"Device found: {event.device()}")
25 elif event_type == DeviceDiscoveryExtension.DDEventTypeDeviceLost:
26 print(f"Device lost: {event.device()}")
27
28# Note: DeviceDiscoveryExtension is typically used within an App Extension
29# target and is managed by the system's extension host.
30if __name__ == "__main__":
31 print("DeviceDiscoveryExtension framework loaded successfully.")
32 print("Framework constants check:", DeviceDiscoveryExtension.DDEventTypeDeviceFound)