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

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

Related

Minimal setup for bluetooth SDP in 2021

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?

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

Cannot connect to BLE device on Raspberry Pi

I'm trying to connect to a BLE device (Heart rate sensor, Polar H7), on a Raspberry Pi 2. I use the last version of bluez (5.35) found here: http://www.bluez.org/download/
But when I'm trying to connect using gatttool, I always have "connection refused" error.
Here is what I do :
pi#raspberrypi ~ $ sudo su
root#raspberrypi:/home/pi# hciconfig dev
hci0: Type: BR/EDR Bus: USB
BD Address: 5C:F3:70:69:54:3D ACL MTU: 1021:8 SCO MTU: 64:1
DOWN
RX bytes:616 acl:0 sco:0 events:34 errors:0
TX bytes:380 acl:0 sco:0 commands:34 errors:0
root#raspberrypi:/home/pi# hciconfig dev up
root#raspberrypi:/home/pi# hcitool lescan
LE Scan ...
00:22:D0:6D:E0:E6 (unknown)
00:22:D0:6D:E0:E6 Polar H7 6DE0E61C
^Croot#raspberrypi:/home/pi# hcitool lecc 00:22:D0:6D:E0:E6
Connection handle 64
root#raspberrypi:/home/pi# gatttool -b 00:22:D0:6D:E0:E6 -I
[00:22:D0:6D:E0:E6][LE]> connect
Attempting to connect to 00:22:D0:6D:E0:E6
Error connect: Connection refused (111)
[00:22:D0:6D:E0:E6][LE]>
I tried to follow this topic: BLE gatttool cannot connect even though device is discoverable with hcitool lescan
but it did not work for me.
I was able to connect via Bluetooth from Raspberry to my Android device using gatttool after setting the LE address type to random with the -t random argument, i.e. :
sudo gatttool -t random -b DD:9D:0B:43:A1:77 -I
connect
From gatttool man
--t, ---addr-type=[public | random]
# Set LE address type. Default: public
USAGE gatttool [OPTION...]
Help Options:
-h, --help Show help options
-h, --help Show help options
--help-all Show all help options
--help-gatt Show all GATT commands
--help-params Show all Primary Services/Characteristics
arguments
--help-char-read-write Show all Characteristics Value/Descriptor
Read/Write arguments
Application Options:
--i, ---adapter=hciX Specify local adapter interface
--b, ---device=MAC Specify remote Bluetooth address
--t, ---addr-type=[public | random] Set LE address type. Default: public
--m, ---mtu=MTU Specify the MTU size
--p, ---psm=PSM Specify the PSM for GATT/ATT over BR/EDR
--l, ---sec-level=[low | medium | high] Set security level. Default: low
--I, ---interactive Use interactive mode
By default GATT is not enable. Add the below lines to /etc/bluetooth/main.conf
EnableLE = true // Enable Low Energy support. Default is false.
AttributeServer = true // Enable the GATT attribute server. Default is false.
I got it running with:
btmgmt le on
Fixed the connection refused (111) issue in my raspberry pi 3B running raspbian buster by Updating/installing BlueZ via apt-get
> sudo apt-get install --no-install-recommends bluetooth
and re running
> sudo gatttool -t random -b E9:1C:89:B7:16:F9 -I
I had to disable the plugin pnat in /etc/bluetooth/main.conf -> DisablePlugins=pnat. I read that it's unstable, but I don't know much about this plugin.
If you are still wondering why the random worked. I have gone through the code and this what I found.
​-t ​(Addr:Type: Set LE Address Type)
Public | random
Default: Public
A random or static address is a 48-bit randomly generated address and shall meet the following
requirements:
• The two most significant bits of the static address shall be equal to ‘1’
• All bits of the random part of the static address shall not be equal to ‘1’
• All bits of the random part of the static address shall not be equal to ‘0’
(Source)
You could try to use gatttool -b 00:22:D0:6D:E0:E6 -I and then connect, just after discovering the MAC without connecting before with hcitool lecc 00:22:D0:6D:E0:E6

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