July 29, 2024, 17:02
python import bluetooth def receivemessages(): # Create a Bluetooth socket and bind it to any address and a port serversock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) port = bluetooth.PORTANY serversock.bind(("", port)) server_sock.listen(1) # Get the port that the server socket is using port = server_sock.getsockname()[1] print(f"Waiting for connection on RFCOMM channel {port}") # Advertise this service bluetooth.advertise_service(server_sock, "SampleServer", service_id="00001101-0000-1000-8000-00805F9B34FB", service_classes=["00001101-0000-1000-8000-00805F9B34FB", bluetooth.SERIAL_PORT_CLASS], profiles=[bluetooth.SERIAL_PORT_PROFILE]) # Accept a connection client_sock, client_info = server_sock.accept() print(f"Accepted connection from {client_info}") try: while True: # Receive data from the client data = client_sock.recv(1024) if data: print(f"Received: {data}") if data.strip() == b'1': print("Hello World") except OSError: pass # Close the connections print("Disconnected") client_sock.close() server_sock.close() if __name == "__main": receive_messages()