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
Create a new Raspberry Pi 4 instance using the lite firmware. In our example, we will use the name "RPi-RPi BLE Node".
Log in using the default credentials (
pi
/raspberry
) and open the Bluetooth tool.bluetoothctl
Turn on Bluetooth.
power on
Open the advertise menu.
menu advertise
Set the manufacturer to
0xffff 0x12 0x34
then read back the values.manufacturer 0xffff 0x12 0x34
manufacturerSet the name to
avh-demo-1
then read back the value.name avh-demo-1
nameGo back to the main menu.
back
Open the generic attributes menu.
menu gatt
Register a new service. At the prompt, enter
yes
.register-service e2d36f99-8909-4136-9a49-d825508b297b
Register a new read-only characteristic. In our example, we will enter
07
at the prompt.register-characteristic 0x1234 read
Register a new read-write characteristic. In our example, we will enter
13
at the prompt.register-characteristic 0x5678 read,write
Register the application. You should see a response
Application Registered
.register-application
Go back to the main menu again.
back
Turn on advertising.
advertise on
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
Create a new Raspberry Pi 4 instance using the lite firmware. In our example, we will use the name "RPi-RPi BLE Master".
Log in using the default credentials (
pi
/raspberry
) and open the Bluetooth tool.bluetoothctl
Turn on Bluetooth.
power on
Scan for Bluetooth devices.
scan on
Connect to the
avh-demo-1
device. Make note of the two IDs starting with00001234
and00005678
, referring to the custom characteristics that you set up.connect 3E:A3:EC:CE:B1:61
Open the generic attributes menu.
menu gatt
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
Read the value. You should see the value you set previously, represented in hexidecimal.
read
Attempt to write a new value. You should see an error because the characteristic is read-only.
write 08
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 the value. You should see the value you set previously, represented in hexidecimal.
read
Write a new value then read it back. In our example, we will use
14
, which is0d
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
Create a new Raspberry Pi 4 board. In our example, we will use the name "RPi-Android BLE Node".
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 onWhen 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
Create a new Android device running firmware 12.0.0. For our example, we will use the name "RPi-Android BLE Master".
Download the Bluetooth LE Scanner APK to your local computer then install the APK through the Apps tab.
Open the Bluetooth LE Scanner app and click Scan. You should see
avh-demo-2
appear with the matching MAC address.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.Click the
CONNECT
button in the app.After the device connects successfully, you will see an
Unknown Service
with the ID matching what we set up. Expand the service.You will see the two custom characteristics
1234
and5678
that we set up.Click on the
1234
and the5678
services to see the values we had set previously (07
and0d
, respectively).