I have a M! Mac 13.1 running Python 3.10. I'm trying to write Python code that discovers any bluetooth devices near me. This is the code...
import objc
from Cocoa import NSObject
from IOBluetooth import IOBluetoothDeviceInquiry
@objc._protocols(['IOBluetoothDeviceInquiry'])
class BLEDeviceDiscovery(NSObject):
def init(self):
self = super(BLEDeviceDiscovery, self).init()
if self is None:
return None
self.discovery = IOBluetoothDeviceInquiry.new()
self.discovery.setDelegate_(self)
return self
def deviceInquiryDeviceFound_(self, sender, device):
print(f"Found a device: {device}")
def deviceInquiryStarted_(self, sender):
print("Starting discovery...")
def deviceInquiryUpdatingDeviceNamesStartedDevicesRemaining_(self, sender, devicesRemaining):
print(f"Updating names... {devicesRemaining} devices remaining.")
def deviceInquiryCompleteWithError_aborted_(self, sender, error, aborted):
if aborted:
print("Device inquiry was aborted.")
elif error:
print(f"Error during device inquiry: {error}")
else:
print("Device discovery completed.")
def start_discovery(self):
self.discovery.start()
def main():
ble_discovery = BLEDeviceDiscovery.alloc().init()
ble_discovery.start_discovery()
if __name__ == "__main__":
main()
Here is the result....
(react) johnd@Johns-MacBook-Pro MacPython % python3 BLE_discovery.py
Traceback (most recent call last):
File "/Volumes/T7/Programming/CrunchComm/MacPython/BLE_discovery.py", line 5, in <module>
@objc._protocols(['IOBluetoothDeviceInquiry'])
TypeError: 'module' object is not callable
What I have tried:
According to ChatGPT, I tried the following....
It seems that there is still a discrepancy in the method signature of the delegate function
deviceInquiryDeviceFound:deviceWithNames:RSSI:
. The method signature expected by the framework should match exactly.
Here's the corrected code for that method:
def deviceInquiryDeviceFound_deviceWithNames_RSSI_(self, sender, device, names, rssi):
print(f"Found a device: {device}")
print(f"Names: {names}")
print(f"RSSI: {rssi}")
None of this is working... ChatGPT is no help, it keeps giving me the same fixes, and I'm going back and forth many times.