Enabling Built in Bluetooth Module and a GATT server application on Raspberry Pi 3 B Plus - cu-ecen-aeld/buildroot-assignments-base GitHub Wiki

(The following changes should be made based on the raspberrypi3_defconfig) The document has been updated for Bluez v 5.63. Buildroot source branch: 2022.02.x

Built-in Bluetooth Module setup

This page describes configuration of Bluetooth on Raspberry Pi for Buildroot. For Yocto instructions, see the page here

Settings in menuconfig:

Change the C library from uClibc-ng to glibc

Prompt: glibc                                               
Location:                                                              
-> Toolchain  
-> C library

This change should be done first before you can continue to make changes shown below.
After making this change, you may want to run the clean.sh and do a clean build in order to create the new toolchain.

Include bluez5-util

Prompt: bluez-utils 5.x                                                
Location:                                                             
-> Target packages                                              
-> Networking applications 

Include bluez-tools

Prompt:bluez-tools 
Location:   
-> Target packages                                              
-> Networking applications 

Include the deprecated tools

Prompt: build CLI client 
Location:                                             
-> Target packages                                                
-> Networking applications                                     
-> bluez-utils 5.x

install tool: hciattach, gatttool,hciconfig, etc (Note: The name of the prompt is mentioned as deprecated, the tools are necessary for the setup. hci tools are deprecated and if it does not work with your configuration, use btattach instead. This will be included with the install deprecated tools option.)

Prompt: install deprecated tool
Location:                                             
-> Target packages                                                
-> Networking applications                                     
-> bluez-utils 5.x
-> build CLI client

Include the RPI BT firmware. Note: In recent buildroot versions, this is included under brcmfmac-sdio-firmware-rpi

Prompt: brcmfmac-sdio-firmware-rpi-bt
Location: 
-> Target packages 
-> Hardware handling 
-> Firmware 

Bluez Utils packages verification

If you are using bluetoothctl or bluetoothd or other Bluez utilities, you need to verify that the hci tools have been included. These tools are used to attach the bcm43xx Bluetooth chip on the hci bus. There is an error in Buildroot which sometimes does not include the deprecated tools. In order to verify this, complete the build with the above configuration. After that:

  • Navigate to buildroot/output/build/bluez5-utils-5.xx
  • Open the config.log file using vi and check if '--enable-deprecated-tools' has been included. Also, check for '--enable-experimental-tools'. This ensures that you have built-in hci and BLE tools built into your image.
  • If this is not included, delete the bluez5-utils-5.xx folder using 'rm -rf bluez5-utils-5.xx'.
  • Navigate back to the buildroot folder and type in 'make bluez5-utils-5.xx' in order to build it manually. Now check the config.in file again to verify. image

Perform either the manual set up after booting as explained in section Manual set up of Bluetooth adapter or include an initialization script as explained in section Initialization script

Manual set up of Bluetooth adapter

Run the following commands to load the BlueTooth HCI UART Driver module, attach a serial UART to the Bluetooth stack, and power it up

modprobe hci_uart
hciattach /dev/ttyAMA0 bcm43xx 921600 flow -
hciconfig hci0 up

Troubleshooting: 
* If hciattach fails, attempt the following command: 'hciattach /dev/ttyS0 bcm43xx 921600 flow -'
* If hciattach is not detected in your build, use btattach instead. 'btattach -B /dev/ttyS0 -P bcm -S 921600'.
* The above btattach command works for a RPi 3B+. If you are using a different hardware kit, use btattach man to assess the bluetooth chip protocol on your board and replace 'bcm' with it. 

Initialization script

To include an initialization script that does the manual set up as described above, Set the root file system overlay as ../base_external/rootfs_overlay

Prompt: Root filesystem overlay directories     
Location: 
-> System configuration 

Initialization script

Verification

After bootup (if initialization script present) or on running the above manual setup commands, the command bt-adapter -l should list available BT controllers as shown below.

Available adapters:
BlueZ 5.55 (<MAC address>)

To view additional information about the controller, enter the command bluetoothctl show redirect

GATT server on PI (Python implementation)

Pull request for the BT setup The below settings are required if a python implementation is being performed. For C implementation, these sections are

Settings in menuconfig:

Include Python

Prompt: python3
Location: 
-> Target packages
-> Interpreter languages and scripting    

Include dbus_python

Prompt: dbus-python
Location:
-> Target packages
-> Hardware handling 

Include python_gobject

Prompt: python-gobject 
Location: 
-> Target packages
-> Interpreter languages and scripting 
-> External python modules    

Verification

run ./GATT_Advertise on pi
run controller_client on Windows
You may need to try the client several times before it detects the PI board and receives notifications.

Pull request for GATT server/client

GATT server on PI (C- implementation)

Pull request for GATT Bluetooth Server C implementation

Manual set up of power, connection, and advertisement for server implementation

Using the BlueZ utilities and tools, the setup is performed by running the below commands.

cd /usr/bin
btmgmt -i hci0 le on
btmgmt -i hci0 connectable on
btmgmt -i hci0 advertising on

If using the initialisation script, the above is already done after bootup

Verification

In the /usr/bin directory, run the btgatt-server executable with option (-r) for the heart rate monitor to be enabled with notification capability. The below command implements the same.

cd /usr/bin
./btgatt-server -r

If using a mobile application as a client, enable notifications to see the randomly generated values being exposed by the heart rate measurement characteristic. Any BLE client with notifications enabled will receive these values. Explore more options in btgattserver by typing the following command which provides the help menu. ./btgatt-server -help

btgatt-server.c code
Bluetooth server C implementation repository
Fixed value for a characteristic commit

Using RPi as a server(command line implementation)

To run bluetoooth via command line implementation, bluetoothd executable needs to be run as Daemon before using bluetoothctl command. The following command needs to be run in root filesystem.

/usr/libexec/bluetooth/bluetoothd -E &

Interactive Command-line based tool started by running "bluetoothctl" in the terminal. Run the below commands to implement a Bluetooth server.

power on
menu advertise (to open advertise submenu)
manufacturer 0xffff 0x12 0x34 (to set manufacturer data)
name aesd_cli_device (friendly name to the device)
back (to return to the main menu)

step1_server

advertise on (to begin advertising with the friendly name and manufacturer data)
menu gatt (to open gatt submenu)

step2

register-service e2d36f99-8909-4136-9a49-d825508b297b (create a custom service with 128-bit UUID. Press yes, almost never used secondary service)
register-characteristic 0x1234 read, write (create a characteristic that can be read and written to. Provide initial value)

register-application (These instructions create objects on the D-bus and to inform BlueZ about the service and characteristic)

step3_server

Now the server can be connected by any Bluetooth client or Bluetooth LE client app such as lightblue, EFR connect).

Troubleshooting

  • If the device connection terminates immediately after connection, the reason could be that the client is already connected to the server. To resolve this if using a mobile as a client, click "Forget this device" in properties of the device found in the mobile's BlueTooth settings
  • For the C implementation, the name of the server is "short name". It can be changed by editing the btgatt-server.c file. The make file is to be updated to pull most recent contents before building again
  • When bluetooth is enabled, UART 1 is assigned for bluetooth chip hence "hciattach /dev/ttyS0 bcm43xx 115200 noflow -" command is used to run bluetooth on S0 port.
  • Alternately, if you are using UART 0 port "hciattach /dev/ttyAMA0 bcm43xx 921600 flow - " command is used to run bluetooth on AMA0 port. See Reference at https://docs.bitscope.com/pi-serial/#bluetooth
  • If BlueZ utlities directory which contains btgatt-server is not found after installing, the directory can be found in the "/buildroot-assignments-base/buildroot/output/build" directory after a successful build

Attach a serial UART to the Bluetooth stack

Using RPi as a client (Command Line implementation)

Interactive Command-line based tool started by running "bluetoothctl" in the terminal. Run the below commands to implement a Bluetooth server.

power on
scan on
connect <xx:xx:xx MAC Address>

bluetoothctl

scan on connect

  • Verify that device connects to a server.
menu gatt
list-attributes (Use this command to list the attributes of the server)
select-attribute <UUID> (UUID based on the attribute you want to write to from list-attributes)
write <data> (If the attribute has write permissions)
read (If the attribute has read permissions)

list attributes

writetoselectedattribute

⚠️ **GitHub.com Fallback** ⚠️