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
Related
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
From time to time, I end up with a Bluetooth Low Energy connection in a strange state from which I cannot disconnect:
$ sudo hcitool con
Connections:
< LE F1:11:52:84:77:25 handle 70 state 1 lm MASTER
$ sudo hcitool ledc F1:11:52:84:77:25
Could not disconnect: Input/output error
I can't get any information on the connection either:
$ sudo hcitool leinfo F1:11:52:84:77:25
Requesting information ...
Could not create connection: Input/output error
I can't re-connect either (checked the code: the error comes down from l2cap_connect() where it tries to open a socket to this remote address and fails to).
$ sudo gatttool -t random -b F1:11:52:84:77:25 -I
[F1:11:52:84:77:25][LE]> connect
Attempting to connect to F1:11:52:84:77:25
Error: connect: Device or resource busy (16)
The only solution I have so far is to unplug and replug my BLE dongle ;( but that's not very satisfactory...
Is there a command or something to erase that buggy connection?
This is on Linux (Mint 18), and using bluez 5.37.
sudo hcitool ledc <handle>
ledc takes handle as argument not the mac.
type sudo hcitool con to find out the handle of the connection you want to disconnect. Then use that handle as argument of ledc.
Example:
sudo hcitool con
Output:
connections:
<LE XX:XX:XX:XX:XX:XX handle 64 state 1 lm MASTER
To disconnect this connection type:
sudo hcitool ledc 64
I can confirm this problem on Ubuntu 14.04 with BlueZ 5.40 and 5.43.
Workaround 1: Disconnect from the other side.
Workaround 2: Use
sudo hciconfig hciX down
sudo hciconfig hciX up
where hciX is your controller (e.g. hci0). Unfortunately, this will remove all connections.
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
I installed bluez-5.15 from source with the following configuration:
$ ./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc \
--localstatedir=/var --libexecdir=/lib --disable-systemd
Then I had to copy gatttool manually into the /usr/local/bin dir
$ sudo cp attrib/gatttool /usr/local/bin/
I rebooted the Raspberry Pi and tried the following to connect to my BLE device:
$ sudo hciconfig hci0 up
$ sudo hcitool lescan
LE Scan ...
EA:FB:B5:CE:B0:13 DfuTarg
$ sudo hcitool lewladd EA:FB:B5:CE:B0:13
$ sudo hcitool lecc EA:FB:B5:CE:B0:13
Could not create connection: Input/output error
So then I tried to connect with gatttool:
$ gatttool -b EA:FB:B5:CE:B0:13 --interactive
[EA:FB:B5:CE:B0:13][LE]> connect
Attempting to connect to EA:FB:B5:CE:B0:13
Error: connect error: Device or resource busy (16)
[EA:FB:B5:CE:B0:13][LE]>
What am I missing here - is this a configuration issue? What do the errors mean?
First off, I spoke with a colleague about my configuration and for Raspbian (or more specifically, Debian) he recommended the following configuration settings:
./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --enable-library --with-systemdsystemunitdir=/lib/systemd/system --with-systemduserunitdir=/usr/lib/systemd
Afterward, upon further inspection, here is how I was able to successfully connect. So what was happening was that the device I was trying to connect to (an NRF-based BLE device) required the LE address flag to be set to 'random'. See below:
gatttool -t random -b EA:FB:B5:CE:B0:13 -I
Then the result:
[EA:FB:B5:CE:B0:13][LE]> connect
Attempting to connect to EA:FB:B5:CE:B0:13
Connection successful
[EA:FB:B5:CE:B0:13][LE]> characteristics
handle: 0x0002, char properties: 0x0a, char value handle: 0x0003, uuid: 00002a00
-0000-1000-8000-00805f9b34fb
Huzzah! Also, there may be a need to set the security level to something lower than high:
[EA:FB:B5:CE:B0:13][LE]> sec-level medium
From my experience with Bluez 5.15
Ensure you have no connections to the BLE device (check with hcitool con).
Do not run hcitool lecc.
Start gatttool like in your question.
In gatttool run connect command. If the command hangs, then press button on the device (i.e. sometimes needed when connecting to SensorTag).
I believe earlier versions of Bluez required hcitool lecc step, but it is not the case anymore.
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