PyObjC ExternalAccessory Framework

12.1 · active · verified Tue Apr 14

PyObjC is a bridge between Python and the Objective-C runtime, allowing Python scripts to interact with macOS frameworks. The `pyobjc-framework-externalaccessory` package provides Python wrappers for the `ExternalAccessory` framework on macOS, enabling interaction with MFi (Made for iOS) accessories. The current version is 12.1, with releases tightly coupled to macOS SDK updates and Python version support cycles.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to access the shared `EAAccessoryManager` and list currently connected MFi accessories. While this code runs, full interaction with `ExternalAccessory` (e.g., establishing communication sessions, receiving notifications) often requires an active Cocoa event loop and proper application lifecycle management, which is typically handled by `PyObjCTools.AppHelper` in more complex PyObjC applications.

import ExternalAccessory
import objc

def list_accessories():
    manager = ExternalAccessory.EAAccessoryManager.sharedAccessoryManager()
    accessories = manager.connectedAccessories()

    if accessories:
        print(f"Found {len(accessories)} connected accessories:")
        for acc in accessories:
            print(f"  - Name: {acc.name()}")
            print(f"    Model Number: {acc.modelNumber()}")
            print(f"    Manufacturer: {acc.manufacturer()}")
            print(f"    Serial Number: {acc.serialNumber()}")
            print(f"    Firmware Revision: {acc.firmwareRevision()}")
            print(f"    Hardware Revision: {acc.hardwareRevision()}")
            print(f"    Protocol Strings: {acc.protocolStrings()}")
    else:
        print("No MFi accessories connected.")

# Note: EAAccessoryManager typically requires an active runloop or
# an application context to correctly detect accessories and receive notifications.
# For simple scripting, you might need to run a console event loop.
# import PyObjCTools.AppHelper
# PyObjCTools.AppHelper.runConsoleEventLoop(installInterrupt=True, mode=objc.NSDefaultRunLoopMode, untilIdle=True)

if __name__ == '__main__':
    # This part runs the list_accessories function immediately.
    # In a full Cocoa app, this would be part of a delegate or controller.
    # For a simple script, it will just check at startup.
    list_accessories()

view raw JSON →