Raspberry Pi Bluetooth Data Transfer to Smartphone
Procedure
Hardware Setup
- Connect the Vcc of the HC-05 Bluetooth module to the 5V power pin of the Raspberry Pi.
- Connect the TX pin of the HC-05 Bluetooth module to the UART0 RX pin of the Raspberry Pi.
- Connect the RX pin of the HC-05 Bluetooth module to the UART0 TX pin of the Raspberry Pi.
- Connect the GND pin of the HC-05 Bluetooth module to the GND pin of the Raspberry Pi.
- On the smartphone, click the Bluetooth icon to enable Bluetooth.
- Toggle the Bluetooth switch on the smartphone screen to turn it on.
- Select the Raspberry Pi from the list of available devices.
- Click the Pair option to pair the smartphone with the Raspberry Pi.
- After completing the circuit, the user can enter the data into the code, and it will be transferred to the smartphone via Bluetooth, displaying on the smartphone.
Software Setup
1. Setting up Raspberry Pi’s Bluetooth - In the beginning, you'll need a monitor + keyboard connected, alternatively access the Raspberry Pi over SSH just to be able to establish all the configurations required through the Raspbian Terminal.
Run the commands below carefully to establish the proper configurations:
- Install bluez (Python bluetooth library)
$ sudo apt-get install python-bluez
/etc/systemd/system/dbus-org.bluez.service
, by running the command below:$ sudo nano /etc/systemd/system/dbus-org.bluez.service
ExecStart
param:ExecStart=/usr/lib/bluetooth/bluetoothd –C

$ sudo sdptool add SP

$ sudo reboot

After the Rebooting let us pair the Bluetooth with our android phone
1. Pairing Raspberry Pi and Android Phone.
Pair your Android phone with your Raspberry Pi. To do this, turn your phone's bluetooth on, and run the command below in your Pi:

Then, once the pairing process starts inserting the following parameters. (Refer to the image to get a better idea of the flow process)
- power on
- discoverable on
- scan on
- At this point, your phone will appear in the list of available devices. Take note of the address of your phone.
- trust <PHONE_ADDRESS>
- pair <PHONE_ADDRESS>

2. To just exit the bluetooth ctl, write the quit command:
$ quit
3. You can skip the above setup, by setting up the Bluetooth with UI of
Python Code
import bluetooth
def start_bluetooth_server(): server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) port = 1 # You can use any available port server_sock.bind(("", port)) server_sock.listen(1) print("Waiting for Bluetooth connection...") client_sock, client_info = server_sock.accept() print("Accepted connection from", client_info) try: while True: message = input("Enter your message: ") if not message: break client_sock.send(message) print(f"Sent: {message}") except KeyboardInterrupt: print("Interrupted by user") finally: print("Closing connection...") client_sock.close() server_sock.close() print("Server closed")
if name == "main": start_bluetooth_server()