0

I have a python app that waits for messages to be send over ble. It works to recieve messages but while it's not recieving the app freezes and goes back to normal when it detects a new message. Anyone know what can cause this?

This is the start of bluetooth:

def startBT():
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

uuid = "a8d52687-55bb-4b1b-b8f5-a1b17cdb0e59"

advertise_service( server_sock, "SampleServer",
                service_id = uuid,
                service_classes = [ uuid, SERIAL_PORT_CLASS ],
                profiles = [ SERIAL_PORT_PROFILE ], 
#                   protocols = [ OBEX_UUID ] 
                    )
                
print("Waiting for connection on RFCOMM channel %d" % port)

global client_sock
client_sock, client_info = server_sock.accept()
global isConnected
isConnected = "true"
print("Accepted connection from ", client_info)
receiveMSG()
loadData()

This here is the recieving part of messages:

def receiveMSG():
if isConnected == "true":
    GLineEdit_692["state"] = "normal"
    print("it be working")
     ready = select.select([client_sock], [], [], 1)
    if ready[0]:
        data = client_sock.recv(1024)
        message = "%s" % data
        message = message.replace("b", "")
        message = message.replace("'", "")
        print(message)
        if("Stop" in message):
            GLineEdit_692.insert(tk.END, "Kart #" + message.replace("Stop", "") + " has been stopped.\n")
        elif("Res" in message):
            GLineEdit_692.insert(tk.END, "Kart #" + message.replace("Res", "") + " has been resumed.\n")
        elif("Pen" in message):
            messagelist = message.split()
            GLineEdit_692.insert(tk.END, "Kart #" + messagelist[0].replace("Pen", "") + " has been given a penalty of " + messagelist[1] + " seconds.\n")
        GLineEdit_692["state"] = "disabled"
    
    root.after(2000, receiveMSG)
mohammed_sajid
  • 233
  • 2
  • 18
  • Will `client_sock.recv(1024)` block if there isn't 1024 bytes of data available? You should edit your question to form a [mcve] so it's easier to assist you. Also `isConnected` should probably be a [boolean](https://peps.python.org/pep-0285/). – import random Aug 10 '22 at 13:32
  • I've changed isConnected to a boolean. Also I've found in my code for my android app that my messages have a byte size of 1024. I've tried to change these but end up with: list index out of range – mohammed_sajid Aug 10 '22 at 14:02
  • If you [profile](https://stackoverflow.com/questions/582336/how-do-i-profile-a-python-script) your script you can definitively determine where the code is _freezing_. What are your expectations? Is this a GUI application that is blocked and not responding to events so the desktop manager thinks the program has stopped responding? – import random Aug 11 '22 at 02:11
  • I managed to kinda fix the problem for now. See the changes I made in the code above. – mohammed_sajid Aug 12 '22 at 09:02

0 Answers0