Skip to content
Advertisement

Communicating via Bluetooth serial with Python

How do you process and receive serial data via Bluetooth and Python?

I’m trying to make a simple Python server that access data via Bluetooth as explained here.

My server.py file, which sends a random number when sent the text “temp”, is:

JavaScript

When I first ran this, I was getting the error:

JavaScript

which Googled showed could be fixed by enabling compatibility mode and loading the serial profile by editing file /lib/systemd/system/bluetooth.service and changing line:

JavaScript

to:

JavaScript

and then running sudo sdptool add SP.

Now python server.py seems to run without error, and I can pair the machine running it to my Android phone. However, it seems it’s unable to receive any data.

I’ve tried using bluetooth terminal emulation apps like BlueTerm, BT Simple Terminal, and Arduino BT, but when I connect to the server enter text and press enter, there’s no response from server.py. It will initially report a “Accepted connection…” and having received and empty string, but it receives nothing after that.

Since there’s no explicit error being reported, I’m not sure how to diagnose the problem. How would I determine if the problem lies in my Python code? Or the bluetooth configuration on the server? Or my Android phone?

Advertisement

Answer

The correct code is:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement