BLE connection established by bluetoothctl disconnect automatically - bluetooth

I have 2 raspberry pi. I set them as ble (bluetooth low energy) gatt server and client to communicate to each other.
On the server-side, I use https://github.com/Jumperr-labs/python-gatt-server to establish the GATT server; on the client-side, I use bluetoothctl to connect to the server-side (as follows)
[bluetooth]# connect C0:EE:40:3A:D0:45
Attempting to connect to C0:EE:40:3A:D0:45
[CHG] Device C0:EE:40:3A:D0:45 Connected: yes
[ABCDEF123456789
[CHG] Device C0:EE:40:3A:D0:45 Name: ABCDEF123456789
[ABCDEF123456789
[CHG] Device C0:EE:40:3A:D0:45 Alias: ABCDEF123456789
[ABCDEF123456789
[CHG] Device C0:EE:40:3A:D0:45 ServicesResolved: yes
[ABCDEF123456789
Failed to connect: org.bluez.Error.NotAvailable
[ABCDEF123456789
[CHG] Device C0:EE:40:3A:D0:45 ServicesResolved: no
[ABCDEF123456789
[CHG] Device C0:EE:40:3A:D0:45 Connected: no
[bluetooth]#
However, the ble connection always disconnects automatically after the connection just established; on the other hand, if I use hcitool to connect to the server-side (as follows), it can successfully connect.
root#raspberrypi:~# hcitool lecc C0:EE:40:3A:D0:45
Connection handle 64
root#raspberrypi:~# bluetoothctl
Agent registered
[CHG] Controller C0:EE:40:3B:39:5D Pairable: yes
[ABCDEF123456789]#
My question is, why the connection established by bluetoothctl break? Is there anything I missed to start a ble connection using bluetoothctl?
Thanks.

By default the RPi is both BR/EDR (classic) and LE Bluetooth. When acting as a BLE peripheral it is good to configure the controller to Bluetooth Low Energy only. It has been discovered that to get a reliable connection to other devices that support both types of Bluetooth, this is required. This configuration change is done in the /etc/bluetooth/main.conf file. Ensure that the file contains the following:
ControllerMode = le
This can also be done on the command line with:
$ sudo btmgmt power off
$ sudo btmgmt bredr off
$ sudo btmgmt power on

Related

Error connecting two raspberry pi with bluetooth

I'm working on a project in which one raspberry pi is going to be a BLE GATT server and the other the client. The first step was testing a normal bluetooth connection via command line but it's not working and I can't figure out why.
Both are raspberry pi 4 model B; the first has raspbian buster with Linux raspberrypi 5.10.17-v7l+ while the second has Ubuntu 20.04 server with Linux ubuntu 5.4.0-1036-raspi.
Using bluetoothctl, on the first (the slave) I set agent on discoverable on and pairable on while on the second I use scan on. The second raspberry finds the other MAC address so I continue with pair DC:A6:32:2C:D4:90. The pairing seems to be successful but it doesn't even ask for the passcode:
slave RPI output:
Changing discoverable on succeeded
[CHG] Controller DC:A6:32:2C:D4:90 Discoverable: yes
[NEW] Device DC:A6:32:FD:47:E0 ubuntu
[CHG] Device DC:A6:32:FD:47:E0 Modalias: usb:v1D6Bp0246d0535
[CHG] Device DC:A6:32:FD:47:E0 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device DC:A6:32:FD:47:E0 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device DC:A6:32:FD:47:E0 UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device DC:A6:32:FD:47:E0 UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device DC:A6:32:FD:47:E0 UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device DC:A6:32:FD:47:E0 ServicesResolved: yes
[CHG] Device DC:A6:32:FD:47:E0 Paired: yes
[CHG] Device DC:A6:32:FD:47:E0 ServicesResolved: no
[CHG] Device DC:A6:32:FD:47:E0 Connected: no
master RPI output:
Attempting to pair with DC:A6:32:2C:D4:90
[CHG] Device DC:A6:32:2C:D4:90 Connected: yes
[CHG] Device DC:A6:32:2C:D4:90 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device DC:A6:32:2C:D4:90 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device DC:A6:32:2C:D4:90 UUIDs: 0000112d-0000-1000-8000-00805f9b34fb
[CHG] Device DC:A6:32:2C:D4:90 UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device DC:A6:32:2C:D4:90 ServicesResolved: yes
[CHG] Device DC:A6:32:2C:D4:90 Paired: yes
Pairing successful
[CHG] Device DC:A6:32:2C:D4:90 ServicesResolved: no
[CHG] Device DC:A6:32:2C:D4:90 Connected: no
ubuntu is the name of the other RPI and its MAC is DC:A6:32:FD:47:E0 as you can see from the output. During the pairing the connection is established for few seconds and then disconnects. When I try to connect with connect DC:A6:32:2C:D4:90 from the master basically the same happens:
[CHG] Device DC:A6:32:2C:D4:90 Connected: yes
[CHG] Device DC:A6:32:2C:D4:90 ServicesResolved: yes
Failed to connect: org.bluez.Error.NotAvailable
[CHG] Device DC:A6:32:2C:D4:90 ServicesResolved: no
[CHG] Device DC:A6:32:2C:D4:90 Connected: no
using bluetoothctl with sudo and trusting the devices doesn't seem to help.
edit: by using default-agent on both devices, I was asked to confirm the pin but the connection still fails

Raspberry Pi bluetooth connection error: "Connection reset by peer"

Good day. I am trying connect bluetooth loudspeaker to Raspberry Pi (RP). I've successfully connected the bluetooth headset using:
sudo bluetoothctl
pair <Headphones' MAC Address>
connect <Headphones' MAC Address>
And everything is all right. But when I am trying connect to soundspeaker it is not.
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:15:83:15:A3:10 Discovering: yes
[NEW] Device F9:56:48:48:A6:8C 17979 speaker
[bluetooth]# pair F9:56:48:48:A6:8C
Attempting to pair with F9:56:48:48:A6:8C
[CHG] Device F9:56:48:48:A6:8C Connected: yes
[CHG] Device F9:56:48:48:A6:8C Connected: no
[CHG] Device F9:56:48:48:A6:8C Connected: yes
[CHG] Device F9:56:48:48:A6:8C Connected: no
[CHG] Device F9:56:48:48:A6:8C Paired: yes
Pairing successful
[bluetooth]# trust F9:56:48:48:A6:8C
[CHG] Device F9:56:48:48:A6:8C Trusted: yes
Changing F9:56:48:48:A6:8C trust succeeded
[bluetooth]# info F9:56:48:48:A6:8C
Device F9:56:48:48:A6:8C (public)
Name: 17979 speaker
Alias: 17979 speaker
Class: 0x00240418
Icon: audio-card
Paired: yes
Trusted: yes
Blocked: no
Connected: no
LegacyPairing: yes
[bluetooth]# connect F9:56:48:48:A6:8C
Attempting to connect to F9:56:48:48:A6:8C
[CHG] Device F9:56:48:48:A6:8C Connected: yes
[CHG] Device F9:56:48:48:A6:8C Connected: no
[CHG] Device F9:56:48:48:A6:8C Connected: yes
[CHG] Device F9:56:48:48:A6:8C Connected: no
Failed to connect: org.bluez.Error.Failed
Here is result of journalctl: (I think, 2 last lines are important)
journalctl -u bluetooth | tail
Feb 27 22:52:58 raspberrypi bluetoothd[318]: F9:56:48:48:A6:8C: error updating services: Connection reset by peer (104)
Feb 27 22:54:57 raspberrypi bluetoothd[318]: F9:56:48:48:A6:8C: error updating services: Connection reset by peer (104)
Feb 27 22:59:16 raspberrypi bluetoothd[318]: Authentication attempt without agent
Feb 27 22:59:16 raspberrypi bluetoothd[318]: Hands-Free Voice gateway rejected 17:09:22:17:0D:31: org.bluez.Error.Rejected
Feb 27 23:00:17 raspberrypi bluetoothd[318]: Authentication attempt without agent
Feb 27 23:00:17 raspberrypi bluetoothd[318]: Hands-Free Voice gateway rejected 17:09:22:17:0D:31: org.bluez.Error.Rejected
Feb 27 23:00:51 raspberrypi bluetoothd[318]: /org/bluez/hci0/dev_17_09_22_17_0D_31/fd0: fd(25) ready
Feb 27 23:02:04 raspberrypi bluetoothd[318]: Unable to get io data for Hands-Free Voice gateway: getpeername: Transport endpoint is not connected (107)
Feb 27 23:34:39 raspberrypi bluetoothd[318]: F9:56:48:48:A6:8C: error updating services: Connection reset by peer (104)
Feb 27 23:35:48 raspberrypi bluetoothd[318]: F9:56:48:48:A6:8C: error updating services: Connection reset by peer (104)
What have I done wrong ? I'll appreciate any advice.

Apple Magic Keyboard/Trackpad 2 will pair with but wont connect to linux device

I have been using Apple's latest version of the Magic Keyboard and Magic Trackpad on my Archlinux machine for a while by establishing a USB connection. This works just fine.
Now, in order to minimize cable clutter on my desk I want to use both of them using a wireless bluetooth connection.
Finding and pairing with either of the two works just fine, but connecting fails every time.
I have tried both bluetoothctl and blueman-manager.
While blueman-manager gives a "Connection Failed: Input/Output error" error message, this is what happens if I try to connect using bluetoothctl:
➜ ~ bluetoothctl
Agent registered
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# pairable on
Changing pairable on succeeded
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:19:86:00:0F:AF Discovering: yes
[NEW] Device 50:A0:09:B0:5B:F7 50-A0-09-B0-5B-F7
[NEW] Device 56:43:CD:7D:4B:7C 56-43-CD-7D-4B-7C
[NEW] Device 2C:33:61:E2:30:C0 Magic Keyboard
[bluetooth]# pair 2C:33:61:E2:30:C0
Attempting to pair with 2C:33:61:E2:30:C0
[CHG] Device 2C:33:61:E2:30:C0 Connected: yes
Request confirmation
[agent] Confirm passkey 194572 (yes/no): yes
[CHG] Device 2C:33:61:E2:30:C0 UUIDs: 00001124-0000-1000-8000-00805f9b34fb
[CHG] Device 2C:33:61:E2:30:C0 UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device 2C:33:61:E2:30:C0 ServicesResolved: yes
[CHG] Device 2C:33:61:E2:30:C0 Paired: yes
Pairing successful
[CHG] Device 2C:33:61:E2:30:C0 ServicesResolved: no
[CHG] Device 2C:33:61:E2:30:C0 Connected: no
[bluetooth]# trust 2C:33:61:E2:30:C0
[CHG] Device 2C:33:61:E2:30:C0 Trusted: yes
Changing 2C:33:61:E2:30:C0 trust succeeded
[CHG] Device 2C:33:61:E2:30:C0 RSSI: -51
[bluetooth]# connect 2C:33:61:E2:30:C0
Attempting to connect to 2C:33:61:E2:30:C0
[CHG] Device 2C:33:61:E2:30:C0 Connected: yes
Connection successful
[CHG] Device 2C:33:61:E2:30:C0 Connected: no
[CHG] Device 2C:33:61:E2:30:C0 Class: 0x00000540
[CHG] Device 2C:33:61:E2:30:C0 Icon: input-keyboard
[CHG] Device 2C:33:61:E2:30:C0 Connected: yes
[CHG] Device 2C:33:61:E2:30:C0 Connected: no
Now, whenever I press any key on the keyboard/touch the trackpad these last two lines are repeated:
[CHG] Device 2C:33:61:E2:30:C0 Connected: yes
[CHG] Device 2C:33:61:E2:30:C0 Connected: no
I suspect this is some sort of driver problem, but I'm not sure.

RFCOMM Connection refused because of pin

i am having issues connecting to a serial to bluetooth device on my raspberry pi.
in bluetoothctl i scan for the ip and try to pair (after trusting) but i get rejected and i never get prompted for a pin.
i also tried
rfcomm connect /dev/rfcomm1 <bt_addr> 1
i check the hci dump and see the hci event
HCI Command: PIN Code request
then right under that i see
HCI Command: PIN Code request Negative reply
but i never get and option to put in my pin. i have connected to other bluetooth devices without this issue but for some reason i'm seeing this on this item enter link description here
i'm using a more recent version of bluez so the hcid.conf doesn't exist anymore, and unfortunately most of my searches involved that file as a solution. how can i fix this with the more recent bluez versions? is there a way to inject that pin when i attempt to connect?
i am able to connect an android device and windows os and read data, but for some reason linux is giving me a problem
thank you
You are missing to register the agent in bluetoothctl for prompting. You can try the following commands in order.
In short,
hciconfig hci0 up
systemctl start bluetooth
bluetoothctl
[bluetooth]# discoverable on
[bluetooth]# pairable on
[bluetooth]# agent NoInputNoOutput
[bluetooth]# scan on
[bluetooth]# scan off #after you found the device
[bluetooth]# trust 44:D8:84:02:A3:17
[bluetooth]# pair 44:D8:84:02:A3:17
[bluetooth]# connect 44:D8:84:02:A3:17
There are various possible options for "agent". Check the below log.
root#mx6q:~# hciconfig
hci0: Type: BR/EDR Bus: USB
BD Address: 00:02:72:A9:B6:45 ACL MTU: 1021:7 SCO MTU: 64:1
UP RUNNING PSCAN
RX bytes:1038 acl:0 sco:0 events:50 errors:0
TX bytes:1405 acl:0 sco:0 commands:49 errors:0
root#mx6q:~# hciconfig hci0 up
root#mx6q:~# systemctl start bluetooth
root#mx6q:~# ps -ef | grep bluetooth
root 324 1 0 00:07 ? 00:00:00 /usr/lib/bluez5/bluetooth/bluetoothd
root 339 314 0 04:25 pts/0 00:00:00 grep bluetooth
root#mx6q:~#
root#mx6q:~# bluetoothctl
[NEW] Controller 00:02:72:A9:B6:45 BlueZ 5.33 [default]
[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller 00:02:72:A9:B6:45 Discoverable: yes
[bluetooth]# pairable on
Changing pairable on succeeded
[bluetooth]# agent
DisplayOnly DisplayYesNo KeyboardDisplay KeyboardOnly NoInputNoOutput off on
[bluetooth]# agent NoInputNoOutput
Agent registered
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:02:72:A9:B6:45 Discovering: yes
[NEW] Device 44:D8:84:02:A3:17 Parthiban
[bluetooth]# scan off
[CHG] Device 44:D8:84:02:A3:17 RSSI is nil
Discovery stopped
[CHG] Controller 00:02:72:A9:B6:45 Discovering: no
[bluetooth]# trust 44:D8:84:02:A3:17
[CHG] Device 44:D8:84:02:A3:17 Trusted: yes
Changing 44:D8:84:02:A3:17 trust succeeded
[bluetooth]# pair 44:D8:84:02:A3:17
Attempting to pair with 44:D8:84:02:A3:17
[CHG] Device 44:D8:84:02:A3:17 Connected: yes
[CHG] Device 44:D8:84:02:A3:17 Modalias: usb:v05ACp12A0d0930
[CHG] Device 44:D8:84:02:A3:17 UUIDs: 00000000-deca-fade-deca-deafdecacafe
[CHG] Device 44:D8:84:02:A3:17 UUIDs: 00001000-0000-1000-8000-00805f9b34fb
[CHG] Device 44:D8:84:02:A3:17 UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Device 44:D8:84:02:A3:17 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device 44:D8:84:02:A3:17 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 44:D8:84:02:A3:17 UUIDs: 00001116-0000-1000-8000-00805f9b34fb
[CHG] Device 44:D8:84:02:A3:17 UUIDs: 0000111f-0000-1000-8000-00805f9b34fb
[CHG] Device 44:D8:84:02:A3:17 UUIDs: 0000112f-0000-1000-8000-00805f9b34fb
[CHG] Device 44:D8:84:02:A3:17 UUIDs: 00001132-0000-1000-8000-00805f9b34fb
[CHG] Device 44:D8:84:02:A3:17 UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device 44:D8:84:02:A3:17 Paired: yes
Pairing successful
[CHG] Device 44:D8:84:02:A3:17 Connected: no
[bluetooth]#

BlueZ bluetoothctl on Raspbian Jessie, connecting a Raspberry Pi to a Sphero

When trying to connect to a Sphero (from Orbotix) through Bluetooth, on a Raspbian Jessie fully updated:
if I do (being root, with Sphero paired and trusted)
# bluetoothctl
[bluetooth]# connect {sphero-mac-address}
[CHG] Device {sphero-mac-address} Connected:yes
[CHG] Device {sphero-mac-address} Connected:no
As you can see the connection fails
but if I do instead
# rfcomm connect /dev/rfcomm0 {sphero-mac-address}
Connected /dev/rfcomm0 to {sphero-mac-address} on channel 1
The connection succeeds, and the Sphero appears connected in bluetoothctl also, afterwards.
What do I do wrong with bluetoothctl connect?
i have the same phenomenom on raspberry pi 3 Jessie when i forgot to do
pulseaudio --start
before bluetoothctl

Resources