PyObjC CoreBluetooth Framework

12.1 · active · verified Mon Apr 13

PyObjC provides Python bindings for Apple's macOS frameworks, allowing Python applications to interact with native macOS APIs. `pyobjc-framework-corebluetooth` specifically offers wrappers for the CoreBluetooth framework, enabling Python programs to communicate with Bluetooth Low Energy (BLE) devices. The current version is 12.1, and PyObjC typically releases new versions in sync with macOS SDK updates and Python version support changes.

Warnings

Install

Imports

Quickstart

This quickstart initializes a `CBCentralManager` and uses a Python class as its delegate. It demonstrates how to create a PyObjC-compatible delegate and receive the `centralManagerDidUpdateState_` callback, which is essential for any CoreBluetooth interaction. For full asynchronous operation and to handle ongoing events, a dedicated Cocoa event loop (e.g., using `PyObjCTools.AppHelper.runConsoleEventLoop()` from `pyobjc-tools`) is required.

import objc
from Foundation import NSObject, NSLog
from CoreBluetooth import CBCentralManager, CBManagerDelegate

# Define a delegate for CBCentralManager
# PyObjC automatically bridges Python classes inheriting from NSObject
# and implementing a protocol into Objective-C delegates.
class MyCentralManagerDelegate(NSObject, protocols=[CBManagerDelegate]):
    def init(self):
        self = objc.super(MyCentralManagerDelegate, self).init()
        if self is None:
            return None
        self.centralManager = None
        return self

    # This method is called when the central manager's state changes.
    # It's typically the first delegate method called after initialization.
    def centralManagerDidUpdateState_(self, central):
        NSLog("Central Manager did update state: %d", central.state())
        # The state is an enum (e.g., CBManagerStatePoweredOn (5), CBManagerStatePoweredOff (4)).
        # For better readability, you'd map the enum values to human-friendly strings.

def main():
    delegate = MyCentralManagerDelegate.alloc().init()
    # CBCentralManager holds a weak reference to its delegate.
    # Ensure the Python delegate object is not garbage collected for the duration
    # of the manager's lifecycle. Keeping `delegate` in a local scope like `main`
    # is sufficient for this quick example.

    # Initialize the CBCentralManager, passing our delegate.
    # 'None' for the queue uses the main dispatch queue.
    manager = CBCentralManager.alloc().initWithDelegate_queue_options_(
        delegate, None, None
    )

    NSLog("Initialized CBCentralManager. Waiting for state update...")
    # CoreBluetooth operations are asynchronous and report via delegate methods.
    # For a console application, you typically need to run a Cocoa event loop
    # (e.g., using `PyObjCTools.AppHelper.runConsoleEventLoop()` from `pyobjc-tools`)
    # to process callbacks. For this minimal example, we'll wait briefly to see
    # the initial state update.
    import time
    time.sleep(1) # Give the system a moment to call the delegate
    NSLog("Quickstart complete. For interactive BLE, use a proper event loop.")

if __name__ == "__main__":
    main()

view raw JSON →