0

I am using PyBluez 0.22 to send data from ESP32 to Python via Bluetooth. So far I have been receiving the data but it is not in the form it. Here is my code

from bluetooth import *
import sys
def input_and_send():
    print("\nType something\n")
    while True:
        data = input()
        if len(data) == 0: break
        sock.send(data)
        sock.send("\n")    
def rx_and_echo():
    sock.send("\nsend anything\n")
    while True:
        data = sock.recv(buf_size)
        if data:
            print(data)
            sock.send(data)
addr = "08:3A:F2:52:1F:86"
service_matches = find_service( address = addr )
buf_size = 1024
if len(service_matches) == 0:
    print("couldn't find the SampleServer service =(")
    sys.exit(0)
for s in range(len(service_matches)):
    print("\nservice_matches: [" + str(s) + "]:")
    print(service_matches[s])  
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
port=1
print("connecting to \"%s\" on %s, port %s" % (name, host, port))
sock=BluetoothSocket(RFCOMM)
sock.connect((host, port))
print("connected")
# input_and_send()
rx_and_echo()
sock.close()
print("\n--- bye ---\n")

If I send three digit number, each digit gets printed in a separate line. For example, If I send "365", the output is

b'3'
b'65\r\n'

I have tried with changing the values of buffer size but it is not helpful. Please guide me how to make it right.

  • Have you connect to the ESP32 with something different and proved that it is being sent in one line? For example, use https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal. You can use `sudo btmon` to see the lower level Bluetooth commands. Also, this can be done with the standard Python Socket library. More info at: https://blog.kevindoran.co/bluetooth-programming-with-python-3/ – ukBaz Feb 21 '22 at 20:19
  • 2
    That's how serial works. You aren't guarantied you get the whole "message". You need to make sure you get the whole "message" yourself. – gre_gor Feb 21 '22 at 21:18
  • @gre_gor can you please guide me what to do in python code to get the full message. – Shahroz Ahmed Feb 22 '22 at 06:38
  • 1
    Read until you find the newline characters. – gre_gor Feb 22 '22 at 06:41

0 Answers0