Cannot disconnect BLE connection - linux

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.

Related

hcitool lescan returns an I/O error on Manjaro

I'm trying to connect MX Master 3 to my computer.
OS: Manjaro 21.2.1 Qonos
Kernel: x86_64 Linux 5.10.89-1-MANJARO
Computer: Dell XPS 7390
I tried:
sudo hcitool lescan
which gives:
Set scan parameters failed: Input/output error
rfkill list all gives:
0: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
1: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
and hcitool dev gives:
Devices:
hci0 84:1B:77:B4:B4:D1
Things I have already tried:
hciconfig hci0 down
hciconfig hci0 up
sudo systemctl restart bluetooth.service
sudo systemctl restart dbus.service
Inside bluetoothctl:
menu scan
transport le
back
scan on
This resolved the problem.

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.

Bluetooth LE on Raspbian

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.

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