Skip to main content

Connect to BLE on Raspberry Pi 4

Learn how to use Bluetooth Low Energy (BLE) on a virtual Raspberry Pi 4 and Android devices.

In this guide, we show how to communicate between two virtual Raspberry Pi 4 virtual boards as well as between a virtual Raspberry Pi 4 board and a virtual Android device.

Use BLE Between Two Raspberry Pi Devices

In this example, we will set up a Raspberry Pi 4 BLE Node and a Raspberry Pi 4 BLE Master.

Set Up the Raspberry Pi 4 BLE Node

To set up the Raspberry Pi 4 BLE node, do the following:

  1. Create a Raspberry Pi 4 device. In this example we will use the "lite" stock firmware, and name the device "RPi-RPi BLE Node". After device is created, log in using the default credentials (pi/raspberry). Quickstart for Raspberry Pi 4 explains this step in more details.

    Raspberry Pi node name

  2. Open the Bluetooth control tool.

    bluetoothctl

    Raspberry Pi open Bluetooth

  3. Turn on Bluetooth.

    power on

    Power on Bluetooth

  4. Open the advertise menu.

    menu advertise

    Menu advertise

  5. Set the manufacturer to 0xffff 0x12 0x34 then read back the values.

    manufacturer 0xffff 0x12 0x34
    manufacturer

    Manufacturer node

  6. Set the name to avh-demo-1 then read back the value.

    name avh-demo-1
    name

    Set name node

  7. Go back to the main menu.

    back

    Main menu

  8. Open the generic attributes menu.

    menu gatt

    Generic attributes menu

  9. Register a new service. At the prompt, enter yes.

    register-service e2d36f99-8909-4136-9a49-d825508b297b

    Register a new service

  10. Register a new read-only characteristic. In our example, we will enter 07 at the prompt.

    register-characteristic 0x1234 read

    Register a read-only characteristic

  11. Register a new read-write characteristic. In our example, we will enter 13 at the prompt.

    register-characteristic 0x5678 read,write

    Register a read-write characteristic

  12. Register the application. You should see a response Application Registered.

    register-application

    Register the application

  13. Go back to the main menu again.

    back

    Main menu

  14. Turn on advertising.

    advertise on

    Advertise node

  15. Your Bluetooth node is now set up. Proceed to the next step to connect to this node using a second Raspberry Pi 4 board.

Connect to the Node with the Raspberry Pi 4 BLE Master

To connect to the node with the Raspberry Pi 4 BLE master, do the following:

  1. Create a new Raspberry Pi 4 instance using the lite firmware. In our example, we will use the name "RPi-RPi BLE Master".

    Create Raspberry Pi 4 instance

  2. Open the Bluetooth control tool.

    bluetoothctl

    Raspberry Pi Login

  3. Turn on Bluetooth.

    power on

    Power on Bluetooth

  4. Scan for Bluetooth devices.

    scan on

    Scan node

  5. Connect to the avh-demo-1 device. Make note of the two IDs starting with 00001234 and 00005678, referring to the custom characteristics that you set up.

    connect 3E:A3:EC:CE:B1:61

    Connect node

    Connect node

  6. Open the generic attributes menu.

    menu gatt
  7. Select the read-only attribute with an ID starting with 00001234. The prompt should change to the appropriate characteristic.

    select-attribute 00001234-0000-1000-8000-00805f9b34fb

    Select attribute

  8. Read the value. You should see the value you set previously, represented in hexidecimal.

    read

    Read node

  9. Attempt to write a new value. You should see an error because the characteristic is read-only.

    write 1234

    Write error

  10. Select the read-write characteristic with an ID starting with 00005678. The prompt should change to the appropriate characteristic.

    select-attribute 00005678-0000-1000-8000-00805f9b34fb

    Read-write characteristic

  11. Read the value. You should see the value you set previously, represented in hexidecimal.

    read

    Read node

  12. Write a new value then read it back. In our example, we will use 14, which is 0d in hexidecimal.

    write 14
    read

    New value node

Use BLE Between a Raspberry Pi 4 Board and an Android Device

In this example, we will set up a Raspberry Pi 4 BLE Node and an Android BLE Master.

Set Up the Raspberry Pi 4 BLE Node

To set up the Raspberry Pi 4 BLE node, do the following:

  1. Create a new Raspberry Pi 4 board. In our example, we will use the name "RPi-Android BLE Node".

    Create Raspberry Pi 4 instance

  2. Follow the steps in the previous section to set up a Raspberry Pi BLE Node. For our example, we will set the name of the Bluetooth node to "avh-demo-2".

    bluetoothctl
    power on
    menu advertise
    manufacturer 0xffff 0x12 0x34
    name avh-demo-2
    back
    menu gatt
    register-service e2d36f99-8909-4136-9a49-d825508b297b
    yes
    register-characteristic 0x1234 read
    07
    register-characteristic 0x5678 read,write
    13
    register-application
    back
    advertise on
  3. When the node is advertising, make note of the node's MAC address on the first line of the response (3E:A3:EC:AE:72:9A in our example).

Connect to the Node with the Android BLE Master

To connect to the node with the Android BLE master, do the following:

  1. Create a new Android device running firmware 12.0.0. For our example, we will use the name "RPi-Android BLE Master".

    Create Android instance

  2. Download the Bluetooth LE Scanner APK to your local computer then install the APK through the Apps tab.

    Install Bluetooth LE scanner

  3. Open the Bluetooth LE Scanner app and click Scan. You should see avh-demo-2 appear with the matching MAC address.

    Scan for node

  4. Click on avh-demo-2 to open up additional details. Scroll to the bottom of the page to see the manufacturer [ff, ff, 12, 34] we set previously.

    Additional details view

    Additional details view

  5. Click the CONNECT button in the Android app.

    Connect to AVH demo

  6. After the device connects successfully, you will see an Unknown Service with the ID matching what we set up. Expand the service.

    Unknown service screen

  7. You will see the two custom characteristics 1234 and 5678 that we set up.

    Custom characteristics

  8. Click on the 1234 and the 5678 services to see the values we had set previously (07 and 0d, respectively).

    Custom characteristics values

    Custom characteristics values