Skip to main content
All CollectionsFeatures
Communicate Using Bluetooth Low Energy
Communicate Using Bluetooth Low Energy

Learn how to use BLE on virtual Raspberry Pi 4 boards and virtual Android 12 devices.

avhsupport avatar
Written by avhsupport
Updated over 2 years ago

Table of Contents

AVH virtual devices support communicating through virtual Bluetooth Low Energy.

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

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

  2. Log in using the default credentials (pi/raspberry) and open the Bluetooth tool.

    bluetoothctl

  3. Turn on Bluetooth.

    power on

  4. Open the advertise menu.

    menu advertise

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

    manufacturer 0xffff 0x12 0x34
    manufacturer

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

    name avh-demo-1
    name

  7. Go back to the main menu.

    back

  8. Open the generic attributes menu.

    menu gatt

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

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

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

    register-characteristic 0x1234 read

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

    register-characteristic 0x5678 read,write

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

    register-application

  13. Go back to the main menu again.

    back

  14. Turn on advertising.

    advertise on

  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

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

  2. Log in using the default credentials (pi/raspberry) and open the Bluetooth tool.

    bluetoothctl

  3. Turn on Bluetooth.

    power on

  4. Scan for Bluetooth devices.

    scan on

  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

  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

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

    read

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

    write 08

  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

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

    read

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

    write 14
    read


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

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

  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
    register-characteristic 0x1234 read
    register-characteristic 0x5678 read,write
    register-application
    back
    advertise on

  3. When the node is advertising, make note of the node's MAC address (3E:A3:EC:7B:47:66 in our example).

Connect to the Node with the Android BLE Master

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

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

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

  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.

  5. Click the CONNECT button in the app.

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

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

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

Did this answer your question?