I am doing a project where I need to create communication between two Raspberry Pi 3 on stretch (RPi) wirelessly with callback functionalities using C++ code. The first is supposed to send data to the second one. I decided to create a bluetooth connexion between the two using the on-board bluetooth-chips.
So first I decided to compile Linux Bluetooth Stack: Bluez on both RPi from source, version 5.50. Following this tutorial : https://learn.adafruit.com/install-bluez-on-the-raspberry-pi/installation on Adafruit. I managed to send a message from one pi to another using these codes :https://people.csail.mit.edu/albert/bluez-intro/x502.html. But to read a message sent by the client, I have to call the function "read(client, buf, sizeof(buf))" as shown in the server code which is a bit restrictive. I want to be notified whenever a message is received so that a callback function can be called. I saw it could be done by enabling notifications on the GATT Characteristics used for the communication.
This level of specification is not really shown on the C++ codes I got from https://people.csail.mit.edu/albert/bluez-intro/x502.html. After having improved my knowledge on GATT with this : https://www.novelbits.io/bluetooth-gatt-services-characteristics/ , I decided to follow Tony D's youtube tutorial on how to use Bluez APi https://www.youtube.com/watch?v=5fQR2PHMDWE.
To make it easier, I decided to left one RPi aside and tried to connect a Bluetooth audio headset JBL T450BT which use Bluetooth 4.0, so it is supposed to support Bluetooth Low Energy.
On bluetoothctl here's the problem, I manage to power on the controller, scan on and detect the headset. I then trust, pair and connect. In GATT menu, I enter the command : list-attributes [dev address]. Bluetoothctl prints no error but returns nothing and is ready to receive other command lines.
sudo bluetoothctl
[bluetooth]# show
Controller XX:XX:XX:XX:XX:XX (public)
Name: VoitureRPi
Alias: VoitureRPi
Class: 0x004c0000
Powered: yes
Discoverable: no
Pairable: yes
UUID: Headset AG (00001112-0000-1000-8000-00805f9b34fb)
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control (0000110e-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: Audio Source (0000110a-0000-1000-8000-00805f9b34fb)
UUID: Audio Sink (0000110b-0000-1000-8000-00805f9b34fb)
UUID: Handsfree Audio Gateway (0000111f-0000-1000-8000-00805f9b34fb)
Modalias: usb:v1D6Bp0246d0532
Discovering: no
[bluetooth]# scan on
Device 78:44:05:BC:12:0C JBL T450BT
[bluetooth]# trust 78:44:05:BC:12:0C
[CHG] Device 78:44:05:BC:12:0C Trusted: yes
Changing 78:44:05:BC:12:0C trust succeeded
[bluetooth]# pair 78:44:05:BC:12:0C
Attempting to pair with 78:44:05:BC:12:0C
[CHG] Device 78:44:05:BC:12:0C Connected: yes
[CHG] Device 78:44:05:BC:12:0C UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Device 78:44:05:BC:12:0C UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device 78:44:05:BC:12:0C UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device 78:44:05:BC:12:0C UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 78:44:05:BC:12:0C UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device 78:44:05:BC:12:0C ServicesResolved: yes
[CHG] Device 78:44:05:BC:12:0C Paired: yes
Pairing successful
[CHG] Device 78:44:05:BC:12:0C ServicesResolved: no
[CHG] Device 78:44:05:BC:12:0C Connected: no
[bluetooth]# connect 78:44:05:BC:12:0C
Attempting to connect to 78:44:05:BC:12:0C
[CHG] Device 78:44:05:BC:12:0C Connected: yes
Connection successful
[CHG] Device 78:44:05:BC:12:0C ServicesResolved: yes
[JBL T450BT]# menu gatt
[JBL T450BT]# list-attributes 78:44:05:BC:12:0C
[JBL T450BT]#
From there I get no answer from bluetoothctl but no error as well, as I told you. I am not able to discover services/characteristics. On the youtube tuto I followed,it was told that the GATT services and Characteristics discovery was done during the pairing/first connexion. I assume that the Pi intends to do it but fails. That's maybe why I get the sequence : connected: yes, ServicesResolved: yes Paired successfully and then ServicesResolved: no, connected no. Nothing like : [NEW] XXX Service or Characteristcs is shown. I don't think the problem comes from the headset because it does the same thing on other devices I connect to. I have both pulseaudio and bluealsa packages installed on the RPi.
As mentionned in this tuto
Here the bluetooth status :
systemctl status bluetooth
● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-11-08 11:29:38 CET; 27min ago
Docs: man:bluetoothd(8)
Main PID: 2646 (bluetoothd)
Status: "Running"
CGroup: /system.slice/bluetooth.service
└─2646 /usr/local/libexec/bluetooth/bluetoothd --experimental
Here's what hciconfig gives :
hciconfig -a
hci0: Type: Primary Bus: UART
BD Address: B8:27:EB:A8:0C:20 ACL MTU: 1021:8 SCO MTU: 64:1
UP RUNNING
RX bytes:11205 acl:76 sco:0 events:520 errors:0
TX bytes:219869 acl:433 sco:0 commands:150 errors:0
Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: RSWITCH SNIFF
Link mode: SLAVE ACCEPT
Name: 'VoitureRPi'
Class: 0x4c0000
Service Classes: Rendering, Capturing, Telephony
Device Class: Miscellaneous,
HCI Version: 4.1 (0x7) Revision: 0x168
LMP Version: 4.1 (0x7) Subversion: 0x2209
Manufacturer: Broadcom Corporation (15)
Here's also what the D-Bus introspection tool returns :
busctl tree org.bluez
└─/org
└─/org/bluez
└─/org/bluez/hci0
└─/org/bluez/hci0/dev_78_44_05_BC_12_0C
├─/org/bluez/hci0/dev_78_44_05_BC_12_0C/fd1
└─/org/bluez/hci0/dev_78_44_05_BC_12_0C/player0
For people who got problems with pairing and connecting, this source was a huge help :https://www.sigmdel.ca/michel/ha/rpi/bluetooth_02_en.html
Anyway so that's my problem. I want to access Characteristics and their UUID's to be able to modify their permission or to create a new service with its own characteristics suited to my project. To this point I think this is the only way to implement a notifier with a callback in a bluetooth communication. Maybe I'm wrong. Do you have any clue whatsoever ? :)
I got it working in RPi with python examples of advertisement and gatt server from bluez-5.50. I used nRF tool to test it.