I’m learning to use Beeware to create mobile apps that access Bluetooth devices that I created. I’m coding on a Mac M1 Airbook running Sonoma with VSCode.
So I gathered that although Beeware does not by itself have Bluetooth LE support, it can use the bundled Rubicon compatability layer to access Objective-C frameworks, especially the corebluetooth framework.
Now, the tutorial code in https://rubicon-objc.readthedocs.io/en/stable/tutorial/tutorial-1.html runs fine. Yes. I’ve installed xcode command line tools, pyobjc etc, so all that should be ok, but when I try to run this very simple code:
from rubicon.objc import ObjCClass, ObjCInstance
# Import the necessary frameworks
CoreBluetooth = ObjCClass("CoreBluetooth")
CBCentralManager = ObjCClass("CBCentralManager")
CBPeripheral = ObjCClass("CBPeripheral")
CBCharacteristic = ObjCClass("CBCharacteristic")
I get the following error:
(venv) joonas@Joonas-MacBook-Air beeware-rubicon-testing % /Users/joonas/Documents/Projects/beeware-rubicon-testin
g/venv/bin/python /Users/joonas/Documents/Projects/beeware-rubicon-testing/rubicon_test.py
Traceback (most recent call last):
File "/Users/joonas/Documents/Projects/beeware-rubicon-testing/rubicon_test.py", line 19, in <module>
CoreBluetooth = ObjCClass("CoreBluetooth")
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joonas/Documents/Projects/beeware-rubicon-testing/venv/lib/python3.12/site-packages/rubicon/objc/api.py", line 1372, in __new__
ptr, name = cls._new_from_name(name_or_ptr)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joonas/Documents/Projects/beeware-rubicon-testing/venv/lib/python3.12/site-packages/rubicon/objc/api.py", line 1205, in _new_from_name
raise NameError(f"ObjC Class {name} couldn't be found.")
NameError: ObjC Class b'CoreBluetooth' couldn't be found.
After running
otool -L /System/Library/Frameworks/CoreBluetooth.framework/CoreBluetooth
it really seems that I don’t have CoreBluetooth installed. Yes, the file is a symlink to Versions/Current/Resources/CoreBluetooth, but that file does not exist!
I’ve tried reinstalling xcode command-line tools to no avail. Is this somehow related to me not using xcode to code (since I prefer VSCode)? Can I somehow install the framework manually? Should I sacrifice a goat to Shub-Niggurath for this to work, or what sacrificial algorithms would you suggest? Thanks!
2
Answers
I don’t know the cause of this specific problem, but rather than coding all this yourself, it might be easier to use an existing Bluetooth LE library like Bleak.
The CoreBluetooth is actually a framework instead of the python package.
You could use
NSBundle
to load the framework first:This code first loads the CoreBluetooth framework using
NSBundle
, and then imports the classes from it.