Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bluetooth module blocking the rest of the program in case of connection reset or host is down #464

Open
raziurtotha opened this issue Jan 3, 2023 · 0 comments

Comments

@raziurtotha
Copy link

raziurtotha commented Jan 3, 2023

System

  • Operating System: debian buster
  • hardware: A small board running on rockchip 3308
  • Python Version: 3.7.3
  • PyBluez Version 0.22

Issue

I am trying to send and receive data simultaneously using bluetooth between client and server sides. The scripts are separate and running on separate devices, on for client and one for server. Initially, the rest of my programs would get blocked in a loop if there was a connection reset or issue with the client or server connection. So I added a single thread in both codes. Now it seems to fix the issue but I am not sure whether it's the correct way to solve this problem.

I have the following codes for client and server sides.

client side:

import bluetooth
import threading

# Set the server's Bluetooth address and port number
bd_addr = "00:e0:4c:9a:f8:64"
port = 1


def client():
    while True:
        try:
            # Create a Bluetooth socket and connect to the server
            sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
            sock.connect((bd_addr, port))

            # Continuously receive data from the server and print it out
            while True:
                data = sock.recv(1024)
                print("Received from server:", data.decode())

                # Prompt the user to type something to send back to the server
                txt = input("Type something to send back to the server: ")
                sock.send(txt.encode())

        except Exception as e:
            # If an error occurs, print the error message and close the socket
            # print(e)
            sock.close()

# Start the receive_data and send_data functions in separate threads
threading.Thread(target=client).start()

server side:

import bluetooth
import threading

def server():
    while True:
        try:
            # Create a Bluetooth socket and bind it to the specified port
            server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
            port = 1
            server_sock.bind(("", port))
            server_sock.listen(1)

            # Accept an incoming connection from a client
            client_sock, address = server_sock.accept()
            print("Accepted connection from", address)

            # Continuously prompt the user to type something to send to the client
            while True:
                txt = input("Type something to send to the client: ")
                client_sock.send(txt.encode())

                # Receive data from the client and print it out
                data = client_sock.recv(1024)
                print("Received from client:", data.decode())

        except Exception as e:
            # If an error occurs, print the error message and close the sockets
            # print(e)
            client_sock.close()
            server_sock.close()

# Start the receive_data and send_data functions in separate threads
threading.Thread(target=server).start()

Is using threading this way, to prevent the rest of the program in each script getting stuck, in case of connection error on either side, correct?

Are there any other/better ways to fix the issue, such as callback function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant