34

I'm trying to setup a virtual serial port though a USB-Bluetooth adapter on Linux and send a message to it through an Android device. I'm on kernel 3.6. I'm able to pair to the device successfully using gnome-bluetooth and also able to send files to it.

To set up the serial port I first add a channel with an SP profile to my adapter:

sdptool add --channel=22 SP

Then I call 'listen' with rfcomm:

rfcomm listen /dev/rfcomm0 22

which blocks on

Waiting for connection on channel 22

Apparently rfcomm will create /dev/rfcomm0 upon a successful connection. Once that happens, I'd like to use something like cutecom to send messages back and forth to the connected device.

On my Android device I open up a Bluetooth SPP terminal (there are several out there, I tried a few different ones) and try to connect. They all fail.

Given that I can pair successfully and send files without any issues, I know that bluetooth pairing and communication works.

I'm not too sure what else I can try. I used 'sdptool browse' on my local device and the Android device to ensure that there aren't any RFCOMM channel conflicts.

Pris
  • 671
  • 1
    for those wondering why all below answers don't work: with BlueZ >= 5 you need compatibility mode, see here: https://raspberrypi.stackexchange.com/questions/41776/failed-to-connect-to-sdp-server-on-ffffff000000-no-such-file-or-directory – JPT Dec 15 '19 at 15:25

4 Answers4

21

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
Pris
  • 671
  • 1
    Good answer. I got it working with Ubuntu 16/Bluez 5 and this info: https://bbs.archlinux.org/viewtopic.php?id=201672. – wojciii Nov 14 '17 at 07:46
11

The steps bellow worked for me:

Firstly you have to pair the devices. Pairing is relatively easy. I will call client (who starts talking) and server (who replies)

You have to setup the server before: Server side (as root):

sdptool add --channel=3 SP
mknod -m 666 /dev/rfcomm0 c 216 0
rfcomm watch /dev/rfcomm0 3 /sbin/agetty rfcomm0 115200 linux

Client side(as root):

sdptool add --channel=3 SP
rfcomm connect /dev/rfcomm0 [SERVER_ADDR] 3

Now to open a serial terminal on the client:

screen /dev/rfcomm0 115200

Comments:

When you call the last command rfcomm connect... in the client, a device /dev/rfcomm0 will be created and associated to the server /dev/recomm0. This represents the serial link between both

The last server command: rfcomm watch.... will 'listen' for incoming connections. In connection lost, the command will restart a new 'listen' state.

HalosGhost
  • 4,790
ismaia
  • 231
  • 1
    Answer provided by ismaia almost gets you there. You need to remember though that newer bluez stack needs the special --compat option, otherwise adding an SP may fail. See here. So adjust/amend your systemd/initd startup startup scripts so that --compat is there. – blacktofu Oct 16 '15 at 10:10
5

I solved this with a slight variation to Pris's commands. Give these a shot if anyone is still having problems setting up a rfcomm connection.

sudo service bluetooth restart

This ^ ensures that you are starting with a clean slate everytime you try to setup a connection.

sdptool add --channel=<a_channel_#> SP

This channel number should be different than any channel currently assigned.

(OPTIONAL) To check channels:

sdptool browse local | grep Channel

I'm not sure why this next command is needed, but it worked for me.

rfcomm release 0

Then to listen for incomming connections:

rfcomm watch 0 <a_channel_#>

NOTE: the bt MAC address in /etc/bluetooth/rfcomm.conf but be your phone's bt MAC. Also the channel in this file must be the same as the one picked for a_channel_#.

Once I did all this, I used a bt terminal emulator on my phone to check it all.

checksum
  • 103
Ethan Plummer
  • 51
  • 1
  • 2
0

I have tried different bluetooth tools and it has been hard to find the correct sequence of commands to connect and exchange data with a bluetooth module. Try using rfcomm and minicom:

This is my /etc/bluetooth/rfcomm.conf

rfcomm0 {
  # Automatically bind the device at startup
  bind no;
  # Bluetooth address of the device
  device 11:22:33:44:55:66;
  # RFCOMM channel for the connection
  channel 3;
  # Description of the connection
  comment "This is Device 1's serial port.";
}

Scan for bluetooth devices:

hcitool scan
Scanning ...
    20:15:12:08:62:95   HC-06

Bind using rfcomm

sudo rfcomm bind 0 20:15:12:08:62:95 1

NB: bind 0 refers to device number 0 (rfcomm0) and 1 is the channel.

Then use minicom with sudo and save a configuration in which you specify the baudrate and the port. You can find more informations here.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
UserK
  • 2,424