How to read BLE Advertising Packets without pairing - linux

I need to read advertising packets from BLE sensors that broadcast temperature in an advertising packet every 5 seconds. I was told by the manufacturer that packet will be a GAP Scan Response Event and that I don't have to connect to the device to read this packet.
The sensor's MAC address shows up when I run sudo hcitool lescan
However, when I run sudo gatttool -I -t random -b 06:09:16:41:87:DB this happens:
[06:09:16:41:87:DB][LE]> connect
Attempting to connect to 06:09:16:41:87:DB
Error: connect error: Connection refused (111)
Also when I run sudo gatttool -b 06:09:16:41:87:DB -I and connect, I get:
Error: connect error: Transport endpoint is not connected (107)
How can I read this advertising packet if I am unable to make a Bluetooth connection via gatttool?

Well, if you want to listen to advertising events then you shouldn't connect since advertisements are broadcast. You should just scan. If you use the debugging tools "hcitool lescan" and "sudo btmon" in a separate terminal concurrently then you should see the scan response data.

Related

Cannot disconnect BLE connection

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.

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 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