5

I have a bluetooth dongle (something that costs around $2) from which I want to send serial data to my android phone.

i have installed :

sudo apt-get install bluetooth bluez-utils blueman

on Scanning , I found my android device (i guess that means the dongle is working)

hcitool scan

after that I ran this:

sudo bluez-simple-agent hci0 xx:xx:xx:xx:xx:xx

I have edited this file : /etc/bluetooth/rfcomm.conf

to this:

rfcomm1 {
    bind yes;
    device xx:xx:xx:xx:xx:xx;
    channel 1;
    comment "Connection to Bluetooth serial module";
}

but on running

sudo rfcomm connect 1 , I am getting 'Can't connect RFCOMM socket: Connection refused'

What is the problem ?

2 Answers2

3

I seem to have gotten this working now. Bluetooth seems a bit finicky. I'm recapping my steps in full in case someone else finds it useful (though its pretty much what I tried initially). This is for Android JB (4.2.2) on a Nexus 4 and Arch Linux 3.6.7-1, with bluez 4.101 on Gnome 3.6 (w/ gnome-bluetooth).

(this step may not do anything useful) Turn Bluetooth on Android off and disconnect your USB/Bluetooth Adapter from your Linux machine (or if you have an in built one, reset it using hcitool devname reset)

Connect/turn on your bluetooth adapter on Linux. Ensure your adapter is visible (can be set in gnome-bluetooth -- you should see a bluetooth system tray icon).

Turn on bluetooth on your Android device. Use Android to pair to the adapter (I was unable to pair the other way around from Linux). A dialog will come up asking you for a key. Put in any PIN you want. Gnome should pop up a notification asking you for a key; put in the same PIN you entered earlier. Your Android device and the key should be paired at this point.

In Linux, open up a terminal and check what bluetooth services are available by typing in

sdptool browse local

If you already have a serial port service, make a note of what channel it is. If you don't, you can add the service:

sdptool add --channel=22 SP

Now listen on this channel using rfcomm:

sudo rfcomm listen /dev/rfcomm0 22

rfcomm will block, listening for a connection with a message like:

Waiting for connection on channel 22

Back on Android, I used the BlueTerm application (http://pymasde.es/blueterm/, also available freely on the google play store) though any similar application should work. Open up BlueTerm, go to options > Connect Device: select the paired adapter.

Hopefully, the application was able to connect. You'll see additional verification in the terminal where you blocked listening with a message like:

Waiting for connection on channel 22
Connection from 22:22:22:22:22:22 to /dev/rfcomm0
Press CTRL-C for hangup

Anything you type into the BlueTerm app should be going to /dev/rfcomm0. You can see stuff show up as you type by opening up a new terminal and doing something like:

cat /dev/rfcomm0

Source: How do I connect and send data to a bluetooth serial port on Linux?


I would like to discuss the packaging of bluez and bluez-utils, since the move to Bluez 5 has broken various commandline utilities, such as sdptool. For example (same as in this question),

$ sdptool browse local
Failed to connect to SDP server on FF:FF:FF:00:00:00: No such file or directory

This is because Bluez 5 deprecated the old C interface which communicates over the socket /var/run/sdp, and this socket no longer exists. The help message from bluetoothd gives the solution:

$ /usr/lib/bluetooth/bluetoothd --help

...

 -C, --compat                Provide deprecated command line interfaces

I apply this as follows

# nano /etc/systemd/system/dbus-org.bluez.service

like:

ExecStart=/usr/lib/bluetooth/bluetoothd --compat

From muru's comment: On the above editing↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

Use systemctl edit dbus-org.bluez.service to create an override file which won't be lost on package update, instead of editing the service file directly.


then:

# systemctl daemon-reload
# systemctl restart bluetooth

Needless to say, this edit is reverted every time bluez is updated. Now /var/run/sdp exists, but sdptool is still broken:

$ sdptool browse local
Failed to connect to SDP server on FF:FF:FF:00:00:00: Permission denied

Comparing to Ubuntu (14.04 is still using Bluez 4) we see that the permissions on /var/run/sdp are different, and setting them to

# chmod 777 /var/run/sdp

(not persistent) finally makes sdptool work.

Source: https://bbs.archlinux.org/viewtopic.php?id=201672

  • 2
    Use systemctl edit dbus-org.bluez.service to create an override file which won't be lost on package update, instead of editing the service file directly. – muru Jun 07 '19 at 05:57
0

The problem is that, although a socket was created and bound on your side, it could not perform a successful call to connect(). (from bluez-X-Y.Z/tools/rfcomm.c source). A connect() call that fails (i.e. returns a value <0) can be caused by a lot of reasons.

I guess your real question is "how can I get a successful connection?"

To solve this issue, you must first be absolutely sure you have a listening socket on the other machine. I suggest you do it with another bluez stack running on a computer.

On the other machine run:

rfcomm listen 0           # listens on channel 1 by default

and on the test machine run:

rfcomm connect 0 00:11:22:33:44:55 1

the expected behavior is having no error.

Then you can use this mechanism to connect sockets and login, transfer file, etc... but that goes beyond your question.