Minimal setup for bluetooth SDP in 2021 - linux

I have minimal linux system (no GUI, raspberry pi like board, based on buildroot) which should be able to do the following:
Allow all bluetooth clients to pair (no pin) after pressing a button for 60 seconds
Allow connection using SPP (serial port profile)
Endpoint of the SPP should be something like /dev/rfcomm
In an older version of buildroot (setup around 2016) I was able to do this using the following commands executed after the button press:
dbus-daemon &
bluetoothd --compat &
hciconfig hci0 up
hciconfig hci0 noauth
hciconfig hci0 sspmode 1
hciconfig hci0 class 0x080500
hciconfig hci0 name foobar
hciconfig hci0 piscan
sdptool add --channel=22 SP
rfcomm listen /dev/rfcomm0 22
I could then pair from my android phone without pin and connect to the SPP interface using this app. Since then hciconfig, sdptool and rfcomm where deprecated. If I try the above method I can't pair from my phone (got connection refused). Without pairing the SPP stuff can't work obviously.
What would be the preferred way in 2021 to setup a headless system which behaves as described above?

Related

Bluetooth broadcast - host name vs friendly name vs alias

My Raspberry Pi is configured for Bluetooth. I change the friendly name using hciconfig:
$ sudo hciconfig hci0 name "MyTest"
$ sudo hciconfig -a hci0
hci0: Type: Primary Bus: UART
...
UP RUNNING PSCAN ISCAN
Name: 'MyTest'
...
On a different Ubuntu box, I scan for nearby devices:
$ sudo bluetoothctl
# scan on
...
[NEW] Device DC:A6:32:88:A6:12 raspberrypi
[CHG] Device DC:A6:32:88:A6:12 Name: MyTest
[CHG] Device DC:A6:32:88:A6:12 Alias: MyTest
Why does it show three different items? And where is it picking the name "raspberrypi" from? Is Pi also broadcasting its host name? Regards.
The first thing to mention is that hciconfig has been deprecated by the BlueZ developers in 2017 so will be removed from systems at some point in the future.
The current tool for this job is btmgmt (assuming you have an up-to-date system).
sudo btmgmt name MyRaspberryPi MyPi
In /etc/bluetooth/main.conf there is this information:
# Defaults to 'BlueZ X.YZ', if Name is not set here and plugin 'hostname' is not loaded.
# The plugin 'hostname' is loaded by default and overides the Name set here so
# consider modifying /etc/machine-info with variable PRETTY_HOSTNAME=<NewName> instead.
#Name = BlueZ

Bluetooth LE on Raspbian does not bond with nRF

I have an Rpi3 and I want to bond it with nRF which is connected to my laptop via UART. This nRF-laptop connection shows me whether Rpi3 is connected and/or paired with nRF. I am able to connect to the nRF device by running:
gatttool -t random -b XX:XX:XX:XX:XX:XX -I
I found that to be able to pair it, all I need to do is run the following command after I connected using gattool:
sec-level medium
However, nRF device only showed "Connected".
Then I tried using bluetoothctl command. I made sure the power is on, agent is on, it's discoverable and etc. After I ran the command pair XX:XX:XX:XX:XX:XX, it said it was successful. This was also confirmed after running info XX:XX:XX:XX:XX:XX. Yet nRF device did not react to the pairing. If I connect to nRF device through my phone using "nRF Connect" app, then nRF shows that my phone is paired.
I am running:
Linux 4.9.13-v7+ #974 SMP Wed Mar 1 20:09:48 GMT 2017 armv7l GNU/Linux
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
Bluez-5.44
I followed tutorials from adafruit and stackoverflow. I read that for Bluez-5.44 I do not need to run bluetooth in the experimental mode.
What am I missing? I would appreciate any help.
To any lost souls who might have also been stuck in this situation, here is how I solved it (you need to run sudo for the following commands):
$ service bluetooth stop
$ /etc/init.d/bluetooth start
If the nRF device has been paired once and then restarted, that means the pairing key is still on Pi's side which needs to be removed and can be done by:
$ bluetoothctl
$ remove XX:XX:XX:XX:XX:XX
$ exit
After this you need to start bluetooth device:
$ hciconfig hci0 up

Bluetooth Low Energy : Android Gatt-Client connect to Linux Gatt Server

I try to connect android to linux via bluetooth low energy GATT mode.
There is a example code in Bluez: btgatt-server.c, I use it as server.
The Android Side be as the Android official example:Connecting to a GATT Server. That is, use function BluetoothDevice::connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback).
After running, the Android part returned the connection be successful,(the parameter of BluetoothGattCallback::onConnectionStateChange be STATE_CONNECTED), but the program in linux(btgatt-server) did not get any response, that still block in waiting accepting.
( line 485).
I also used another example code : l2cap-ble.c, the result was totally the same.
Anyone could help me? my goal is : after connecting, linux gatt server would send a data to Android. Then disconnecting.
thank you lots.
I just now faced the same problems.
This seems like a bug that happens with Android and Broadcom bluetooth modules. Android somehow connects to the wrong l2cap channel.
A workaround is to explicitely disable BR/EDR support in the advertisements.
The bug report on google with the solution in #8 can be found here, the quote on the hint that helped me:
I have obtained information from Broadcom regarding this bug and I don't think its a bug on their part. If one does not specifically set the 'BR/EDR not supported' bit in the advertisement (bit 2), the Broadcom stack will use BR/EDR and BTLE.
Changing the leading advertisement package from 020102 to 020106 solved the problem.
Using bluez btmgmt for configuring the advertisements on device hci0 i issue the following commands:
btmgmt -i hci0 power off
btmgmt -i hci0 le on
btmgmt -i hci0 connectable on
btmgmt -i hci0 bredr off # Disables BR/EDR !
btmgmt -i hci0 advertising on
btmgmt -i hci0 power on
Starting the bluez btgatt-server example:
btgatt-server -i hci0 -s low -t public -r -v

bluetooth pairing and connection from command line

I want to connect and pair to available nearby bluetooth devices from command line in linux.
I have searched through internet, and found that we can pair through simple-agent, but it is not available in my device.
following commands are available in device:
hcitool, hciconfig, hcidump, sdptool, l2ping, rfcomm.
for connecting to remote device i am running the following command.
hcitool cc BD_ADDRESS
but above command is connecting the device for a while, and again the device is getting disconnected. in hcidump output , i am seeing the disconnection event. Is there anyway in linux command line through which i can test my bluetooth? What can be the reason for device is getting disconnected after a while?
I saw the same issue once and was able to solve it with the rfcomm command. Try the following:
# rfcomm connect /dev/rfcomm0 00:11:22:33:44:55 1 &
Additionally, it is good practice to enable secure simple pairing and enable page and inquiry scan with the following commands:
# hciconfig hci0 sspmode 1
# hciconfig hci0 piscan
Let me know if you still have problems.

Raspberry Pi Bluetooth LE connection to RedBearLab shield

I am trying to connect to RedBearLab BLE shield from RaspPi. I am using
CSR4.0 USB dongle http://www.adafruit.com/products/1327
Raspberry Pi Rev-B board with Raspbian OS
Bluez-5.11
I have the RedBearLab BLE shield connected to Arduino UNO R3. I am trying to connect to the shield from the RaspPi(attached CSR4.0). But it can't connect - connection attempts time out/resource busy.
Below steps I use
$ sudo hcitool lescan
LE Scan ...
xx:xx:xx:xx:xx:xx BLE Shield
$ sudo hcitool lecc xx:xx:xx:xx:xx:xx
Could not create connection: Connection timed out
Using gatttool doesn't solve the problem either
$ sudo gatttool -b xx:xx:xx:xx:xx:xx --interactive
[ ][xx:xx:xx:xx:xx:xx][LE]> **connect**
Attempting to connect to xx:xx:xx:xx:xx:xx
Error: connect error: Device or resource busy (16)
[xx:xx:xx:xx:xx:xx][LE]>
Thanks a bunch!
Seems like your adapter is busy. Something is using it. Is there any other device connected to your adapter? Try resetting your adapter with:
$ sudo hciconfig hci0 reset
Some BLE devices require a random address at connection time. Try:
sudo gatttool -t random -b xx:xx:xx:xx:xx:xx --interactive

Resources