bluez5 : Cannot connect to a paired bluetooth device on Ubuntu 17.10

I'm using bluez v5.46 on Ubuntu 17.10 and I cannot connect to a paired bluetooth device.

Here is the output of the bluetoothctl command :

$ bluetoothctl 
[NEW] Controller 6C:0B:84:27:43:66 tensorFlowPHY [default]
Agent registered
[bluetooth]# scan on
Discovery started
[CHG] Controller 6C:0B:84:27:43:66 Discovering: yes
[NEW] Device FC:58:FA:A1:C2:70 OE-P51
[bluetooth]# pair FC:58:FA:A1:C2:70 
Attempting to pair with FC:58:FA:A1:C2:70
[CHG] Device FC:58:FA:A1:C2:70 Connected: yes
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 ServicesResolved: yes
[CHG] Device FC:58:FA:A1:C2:70 Paired: yes
Pairing successful
[CHG] Device FC:58:FA:A1:C2:70 ServicesResolved: no
[CHG] Device FC:58:FA:A1:C2:70 Connected: no
[CHG] Controller 6C:0B:84:27:43:66 Discoverable: no
[bluetooth]# connect FC:58:FA:A1:C2:70 
Attempting to connect to FC:58:FA:A1:C2:70
Failed to connect: org.bluez.Error.Failed

Here is a little bit more info :

$ journalctl -u bluetooth | tail
Mar 29 12:06:01 tensorFlowPHY systemd[1]: Started Bluetooth service.
Mar 29 12:06:01 tensorFlowPHY bluetoothd[1136]: Starting SDP server
Mar 29 12:06:01 tensorFlowPHY bluetoothd[1136]: Bluetooth management interface 1.14 initialized
Mar 29 14:36:12 tensorFlowPHY bluetoothd[1136]: a2dp-sink profile connect failed for FC:58:FA:A1:C2:70: Protocol not available
Mar 29 14:36:59 tensorFlowPHY bluetoothd[1136]: a2dp-sink profile connect failed for FC:58:FA:A1:C2:70: Protocol not available

And systemctl says :

$ systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-03-30 16:30:15 CEST; 21s ago
     Docs: man:bluetoothd(8)
 Main PID: 1492 (bluetoothd)
   Status: "Running"
    Tasks: 1 (limit: 4915)
   Memory: 1.5M
      CPU: 42ms
   CGroup: /system.slice/bluetooth.service
           └─1492 /usr/lib/bluetooth/bluetoothd

Mar 30 16:30:15 tensorFlowPHY systemd[1]: Starting Bluetooth service...
Mar 30 16:30:15 tensorFlowPHY bluetoothd[1492]: Bluetooth daemon 5.46
Mar 30 16:30:15 tensorFlowPHY systemd[1]: Started Bluetooth service.
Mar 30 16:30:15 tensorFlowPHY bluetoothd[1492]: Starting SDP server
Mar 30 16:30:15 tensorFlowPHY bluetoothd[1492]: Bluetooth management interface 1.14 initialized
Mar 30 16:30:20 tensorFlowPHY bluetoothd[1492]: a2dp-sink profile connect failed for FC:58:FA:A1:C2:70: Protocol not available
Mar 30 16:30:31 tensorFlowPHY bluetoothd[1492]: a2dp-sink profile connect failed for FC:58:FA:A1:C2:70: Protocol not available

EDIT 1: I installed one Ubuntu package that was missing : pulseaudio-module-bluetooth and restarted pulseaudio with the command : pulseaudio -k

Now the output of the bluetooth log after connection trial is :

Mar 30 16:36:59 tensorFlowPHY bluetoothd[1492]: Unable to get connect data for Headset Voice gateway: getpeername: Transport endpoint is not connected (107)
Mar 30 16:37:00 tensorFlowPHY bluetoothd[1492]: connect error: Connection refused (111)
Mar 30 16:37:02 tensorFlowPHY bluetoothd[1492]: connect error: Connection refused (111)

Any idea why ?

EDIT 2 : I just restarted Ubuntu as someone asked to do. Still have the same pb.

EDIT 3 : I already have both modules loaded in /etc/pulse/default.pa :

$ egrep "(policy|discover)" /etc/pulse/default.pa
.ifexists module-bluetooth-policy.so
load-module module-bluetooth-policy
.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover

But I cannot find those on the hdd, do I need to install something else ?

EDIT 4: My mistake, I used the locate command. After a sudo updatedb, the locate command is now able to find those 2 files.

EDIT 5 : Here is the pactl output :

$ pactl list modules | grep "Name:"
    Name: module-device-restore
    Name: module-stream-restore
    Name: module-card-restore
    Name: module-augment-properties
    Name: module-switch-on-port-available
    Name: module-udev-detect
    Name: module-alsa-card
    Name: module-alsa-card
    Name: module-bluetooth-policy
    Name: module-bluetooth-discover
    Name: module-bluez5-discover
    Name: module-native-protocol-unix
    Name: module-default-device-restore
    Name: module-rescue-streams
    Name: module-always-sink
    Name: module-intended-roles
    Name: module-suspend-on-idle
    Name: module-console-kit
    Name: module-systemd-login
    Name: module-position-event-sounds
    Name: module-role-cork
    Name: module-filter-heuristics
    Name: module-filter-apply
    Name: module-switch-on-connect
    Name: module-x11-publish
    Name: module-x11-cork-request
    Name: module-x11-xsmp

and the dpkg -l output :

$ dpkg -l | grep blue
ii  bluefish                                  2.2.9-1                                                    amd64        advanced Gtk+ text editor for web and software development
ii  bluefish-data                             2.2.9-1                                                    all          advanced Gtk+ text editor (data)
ii  bluefish-plugins                          2.2.9-1                                                    amd64        advanced Gtk+ text editor (plugins)
ii  blueman                                   2.0.4-1ubuntu3                                             amd64        Graphical bluetooth manager
ii  bluez                                     5.46-0ubuntu3                                              amd64        Bluetooth tools and daemons
ii  bluez-cups                                5.46-0ubuntu3                                              amd64        Bluetooth printer driver for CUPS
ii  bluez-obexd                               5.46-0ubuntu3                                              amd64        bluez obex daemon
ii  bluez-tools                               0.2.0~20140808-5build1                                     amd64        Set of tools to manage Bluetooth devices for linux
ii  gnome-bluetooth                           3.26.1-1                                                   amd64        GNOME Bluetooth tools
ii  indicator-bluetooth                       0.0.6+17.10.20170605-0ubuntu3                              amd64        System bluetooth indicator.
ii  libbluetooth3:amd64                       5.46-0ubuntu3                                              amd64        Library to use the BlueZ Linux Bluetooth stack
ii  libgnome-bluetooth13:amd64                3.26.1-1                                                   amd64        GNOME Bluetooth tools - support library
ii  pulseaudio-module-bluetooth               1:10.0-2ubuntu3.1                                          amd64        Bluetooth module for PulseAudio sound server

And here are the bluetooth pulseaudio modules loaded :

$ pactl list modules short | grep module-bluetooth
9   module-bluetooth-policy     
10  module-bluetooth-discover

EDIT 6: Your solution worked ! So to sum up :

$ bluetoothctl
[bluetooth]# power on
Changing power on succeeded
[CHG] Controller 6C:0B:84:27:43:66 Powered: yes
[bluetooth]# remove FC:58:FA:A1:C2:70
[DEL] Device FC:58:FA:A1:C2:70 OE-P51
Device has been removed
[bluetooth]# exit
Agent unregistered
[DEL] Controller 6C:0B:84:27:43:66 tensorFlowPHY [default]
$ sudo service bluetooth restart

and finally :

$ bluetoothctl
[NEW] Controller 6C:0B:84:27:43:66 tensorFlowPHY [default]
Agent registered
[bluetooth]# power on
Changing power on succeeded
[CHG] Controller 6C:0B:84:27:43:66 Powered: yes
[bluetooth]# scan on
Discovery started
[CHG] Controller 6C:0B:84:27:43:66 Discovering: yes
[NEW] Device FC:58:FA:A1:C2:70 OE-P51
[bluetooth]# pair FC:58:FA:A1:C2:70 
Attempting to pair with FC:58:FA:A1:C2:70
[CHG] Device FC:58:FA:A1:C2:70 Connected: yes
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 ServicesResolved: yes
[CHG] Device FC:58:FA:A1:C2:70 Paired: yes
Pairing successful
[CHG] Device FC:58:FA:A1:C2:70 ServicesResolved: no
[CHG] Device FC:58:FA:A1:C2:70 Connected: no
[bluetooth]# connect FC:58:FA:A1:C2:70 
Attempting to connect to FC:58:FA:A1:C2:70
[CHG] Device FC:58:FA:A1:C2:70 Connected: yes
Connection successful
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device FC:58:FA:A1:C2:70 ServicesResolved: yes
[OE-P51]#

Solution 1:

From Arch wiki, Pairing works, but connecting does not, section:

This may be due to the pulseaudio-bluetooth package not being installed. Install it if it missing, then restart pulseaudio.

But pulseaudio-bluetooth package's name is pulseaudio-module-bluetooth on Ubuntu.

So your problem should be solved after installing it:

sudo apt install pulseaudio-module-bluetooth

And after installing this package restart your system to load some pulse audio modules like module-bluetooth-policy and module-bluetooth-discover.

Update:

But if still, you get below message after reboot:

connect error: Connection refused (111)

According to this answer, you should add these lines to /etc/pulse/default.pa file. (If not exist):

load-module module-bluetooth-policy
load-module module-bluetooth-discover

But these lines should be exist by default and after reboot your system, those modules should be loaded.

Update2:

According to this article on Arch wiki:

This may be because you have already paired the device with another operating system using the same bluetooth adapter (e.g., dual-booting). Some devices can't handle multiple pairings associated with the same MAC address (i.e., bluetooth adapter). You can fix this by re-pairing the device

So please re-pair your device with this commands (Or you can use GUI method):

First run:

bluetoothctl

Then use this commands to show paired devices:

devices

And this should be have some output like this:

Device XX:XX:XX:XX:XX:XX Device Name

Then remove paired devices with this command:

remove XX:XX:XX:XX:XX:XX

And restart Bluetooth service:

sudo service bluetooth restart

Then try to pair device again.

Solution 2:

Disable NetworkManager!

...or similar network management. It is what solves most networking issues where you manually try to control a service which NetworkManager is programmed to oversee.

In Kali Linux edit /usr/sbin/update-rc.d which "auto-disables service we don't want to start by default".

Find this entry in the whitelist

# List of whitelisted init scripts
#
...
network-manager enabled
...

and move it to the blacklist and change enabled to disabled. Similarily, move bluetooth to the whitelist and enable if you want it to remain on through reboot if you manually turn it on. Secondly, don't forget to power on blutetooth to make the connection persistent.

...
[bluetooth]# power on
Changing power on succeeded
[CHG] Device FC:58:FA:A1:C2:70 Connected: yes

You can also disable NetworkManager for a single device in /etc/NetworkManager/NetworkManager.conf, mask it by MAC address or interface name:

[keyfile]
unmanaged-devices=mac:<hwaddr>
[keyfile]
unmanaged-devices=interface-name:<ifname>

To find the MAC address and interface name of the local bluetooth device use the command

hcitool dev