Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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...

Python
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:
Python
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.
Posted
Updated 28-Aug-23 12:27pm
v3
Comments
Richard MacCutchan 25-Aug-23 4:33am    
"ChatGPT is no help"
Then don't use it, it is a machine, not a programming expert.
John Draper 2023 25-Aug-23 4:48am    
I read this document several times. I'm looking for example code on how to "Discover" BLE devices. I agree, chatGPT is brain dead, so is bard.google.com. Can you give me some references to example code? How much would you charge for a Zoom session with this 80 year old USAF disabled veteran? I would use VirtualBox and linux but the Mac M1 don't support it. Is there anyway you can help me?
John Draper 2023 25-Aug-23 4:57am    
I have other code examples I got from ChatGPT, but everyone has errors. If you want to look at them, I can send them along as well. I'm pretty inexperienced in coding, but have been pretty successful in soliciting good responses from ChatGPT and Bard. There appears to be some kind of "Gotcha" somewhere in this code... I did try other sources of Python libraries, none successful. With my disability, I don't have much of an opportunity to meet other programmers more experienced.
Dave Kreskowiak 25-Aug-23 10:08am    
ChatGPT cannot write code. Think of it as a toddler trying to please its parents, even if it gets answers wrong. The objective to is please, not be correct.

1 solution

 
Share this answer
 
Comments
John Draper 2023 1-Sep-23 20:10pm    
After checking round with help from ChatGPT - it refers to my M1 laptop does not yet support PyBluez module. Nobody has posted a correction or workaround for this, if anyone has, please let me know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900