Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send message over bluetooth with Python 3

I am currently trying to send a message from Python 3 to a Arduino (with HC-06). I've managed to establish a connection but I can't seem to find the right way to send a message.

Here is the code I use to setup the connection (I'm using PyBluez for bluetooth):

import sys
import bluetooth

bd_addr = '[MAC-address for HC-06]'
port = 1
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr,port))

To send I'm trying:

sock.send("2")

Which throws the error: "TypeError: Expecting byte-buffer, got str".

Also tried:

sock.send(str('2'))

It also throws an error: "TypeError: Expecting byte-buffer, got str".

I've tried a bunch of other like "sock.send(2)", "sock.send(b'2')", bytearray, "sock.send(bytes(["2"]))". Basicly, whatever datatype I'm trying to send it demands another.

I've managed to send all my commands (only 1 and 2 at the moment) via a Bluetooth-terminal on my Android phone so there is no problem on the Arduino site.

There is a lot of guides out there with examples that I'm not able to replicate. Can anyone please tell me how I send basic commands? Am I even close to the answer?

EDIT: I've now tried "sock.send("2".encode())" and it also throws an error: "TypeError: data must be string, was class 'bytes'"

Using another ide gave me more specific information about the errors:

sock.send("2"): Warning (from warnings module): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyBluez-0.22-py3.6.egg/lightblue/_bluetoothsockets.py", line 737 Foundation.NSData.alloc().initWithBytes_length_(data, len(data)), UninitializedDeallocWarning: leaking an uninitialized object of type _NSPlaceholderData Traceback (most recent call last): File "", line 1, in sock.send("2") File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyBluez-0.22-py3.6.egg/bluetooth/osx.py", line 122, in send return self._sock.send(data) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyBluez-0.22-py3.6.egg/lightblue/_bluetoothsockets.py", line 524, in send result = self.__conn.write(writebuf[:sendbytecount]) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyBluez-0.22-py3.6.egg/lightblue/_bluetoothsockets.py", line 737, in write Foundation.NSData.alloc().initWithBytes_length_(data, len(data)), TypeError: Expecting byte-buffer, got str

sock.send("2".encode()): Traceback (most recent call last): File "", line 1, in sock.send("2".encode()) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyBluez-0.22-py3.6.egg/bluetooth/osx.py", line 122, in send return self._sock.send(data) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyBluez-0.22-py3.6.egg/lightblue/_bluetoothsockets.py", line 487, in send raise TypeError("data must be string, was %s" % type(data)) TypeError: data must be string, was class 'bytes'

I am starting to doubt the library. It's totally possible that I managed to do something wrong when installing PyBluez. I'm gonna see if it might work on my Raspberry Pi 3.

like image 630
Carl Bratt Avatar asked Jul 17 '26 06:07

Carl Bratt


1 Answers

Solution

When you send data over a socket you have to encode it or its gonna throw a byte buffer error. When you send data over the socket just use.

sock.send("2".encode())
like image 177
cunniemm Avatar answered Jul 18 '26 20:07

cunniemm