0

I would like to accomplish the following task: Interact via a python script with my Raspberry Pi and a Smartphone. For me it doesn't matter so much whether to use Classic Bluetooth or BLE but for user convenience I need some automation to establish the connection so eventually a PIN using Classic Bluetooth would be good enough.

How would you realize this project?

I have checked pybluez already: https://github.com/pybluez/pybluez

And I have some experience with opening the Bluetooth socket on the RaspberryPi and exchanging data between the RPI and the smartphone using the Adafruit Bluetooth App.

The next step for me would be doing this automated without scanning for devices on the RPI by hand, then selecting my MAC address and so on.

2

Here's the instruction for everyone who wants to make Bluetooth Serial communication to work without dealing with Python or other piggybacked programs running inside Raspberry Pi Model 3 B+ or 4 and Pi Zero easily.

  1. This configuration (below) works well with Android (old Android with BT 4.0 or later) and iPhone (8 or newer) with Bluetooth 4.0 or higher, using the Bluetooth Serial app for Android/IOS. Windows 10 notebook with Bluetooth and Bluetooth app will work well too.
  2. Treat RPi bluetooth serial device the same as other RPi serial ports, which would be useful when it comes to development (Python, C, etc.).
  3. With your Python/C/whatever program with serial routines, you can add username/password or token key or whatever for authentication for accessing RPi serial port (for others who are concerned about security).
  4. Make sure that your Raspberry Pi is freshly installed with latest O/S (as of March 2021) first before applying the configuration settings.
  5. This configuration took a long time to develop with hundreds of trials and errors to make it working correctly.

Known RPi serial ports:

Name              Device Path name
================  ==================
Bluetooth Serial  /dev/rfcomm0
TTL TX/RX         /dev/serial0  
    Note: You will need to disable TTL TX/RX serial console and enable it via raspi-config
USB0              /dev/ttyUSB0
USB1              /dev/ttyUSB1
USB2              /dev/ttyUSB2
USB3              /dev/ttyUSB3
Note: USB0-USB3 - using USB-serial adapter.

Bluetooth System Settings for Bluetooth Serial Communication

Important Note: The depreciated functions as listed here: hciattach hciconfig hcitool hcidump rfcomm sdptool ciptool gatttool as described from bluez.git may not happen. Reason: That link was posted 4 years ago, which I do not believe it will happen at all.

1. Install Minicomm

sudo apt-get install minicom -y

Minicom serial terminal program is useful for testing the serial communication between Bluetooth serial app installed the phone and RPi serial.

2. Enable SPP on Raspberry Pi

In order to use SPP, Bluetooth service needs to be added/restarted with ‘compatibility’ flag.

Open Bluetooth service configuration file.

sudo nano /etc/systemd/system/dbus-org.bluez.service

Look for a line starts with “ExecStart” and add compatibility flag ‘-C’ at the end of the existing line as shown here:

ExecStart=/usr/lib/bluetooth/bluetoothd -C

Add a new line below immediately after the “ExecStart” line (see above):

ExecStartPost=/usr/bin/sdptool add SP

Save / close the dbus-org.bluez.service file.

Then reload the configuration file.

sudo systemctl daemon-reload

Then restart the Bluetooth service.

sudo systemctl restart bluetooth.service

3. Auto-Pairing Setup

Open rc.local file to add the services that will start at boot:

sudo nano /etc/rc.local

The new commands in rf.local will make bluetooth discoverable and listen for the incoming connections automatically. (no need to deal with MAC address!)

Copy the below commands and paste it in the file (at end of file, just before exit 0 line)

sudo bluetoothctl <<EOF
discoverable on
EOF

sudo rfcomm watch hci0 &

then save rc.local.

Then, edit Bluetooth main.conf file:

sudo nano /etc/bluetooth/main.conf

Un-comment both lines:

discoverabletimeout=0 

and

pairabletimeout=0 

then save main.conf.

Then reboot the Raspberry Pi system to update the system settings for Bluetooth services.

sudo reboot

After reboot, open your Bluetooth Serial Terminal app on your phone, then pair it, then connect it.

Then, in Raspberry Pi online, launch minicom

sudo minicom -b 19200 -o -D /dev/rfcomm0

You should be able to send/receive the messages from both ends (BT Serial Terminal app and Minicom terminal screen).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.