Bluetooth serial port ubuntu

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

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:

rfcomm listen /dev/rfcomm0 22 
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.

for those wondering why all below answers don’t work: with BlueZ >= 5 you need compatibility mode, see here: raspberrypi.stackexchange.com/questions/41776/…

4 Answers 4

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

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:

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.

Читайте также:  Наушники xiaomi mi sport bluetooth headset mini

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:

Источник

Bluetooth Serial Communication Between Linux & Android

Most laptops and smart phones(Android/iPhone) have builtin Bluetooth modules. We can use this bluetooth module to communicate with other devices or with other bluetooth modules like HC-05 or HM-10.

In this article, we will learn how to send data between laptop and android bluetooth.

First, we need to pair with a bluetooth device to send information. From Ubuntu, we can pair to a Bluetooth device from Bluetooth settings. Alternatively, we can also use CLI to do the same.

$ bluetoothctl [NEW] Controller 24:0A:64:D7:99:AC asus [default] [NEW] Device 94:E9:79:BB:F8:3A DESKTOP-C4ECO3K [NEW] Device 88:79:7E:7B:4C:87 athene [NEW] Device 94:65:2D:8C:2E:10 OnePlus 5 [NEW] Device 98:0C:A5:61:D5:64 Lenovo VIBE K5 Plus [NEW] Device AC:C3:3A:A0:CE:EF Galaxy J2 [NEW] Device 98:D3:35:71:02:B3 HC-05 [bluetooth]# power on Changing power on succeeded [bluetooth]# agent on Agent registered [bluetooth]# default-agent Default agent request successful [bluetooth]# scan on Discovery started [CHG] Controller 24:0A:64:D7:99:AC Discovering: yes [CHG] Device 94:E9:79:BB:F8:3A RSSI: -88 [CHG] Device 88:79:7E:7B:4C:87 RSSI: -66 [bluetooth]# pair 88:79:7E:7B:4C:87 Attempting to pair with 88:79:7E:7B:4C:87 [CHG] Device 88:79:7E:7B:4C:87 Paired: yes Pairing successful 

To communicate with paired devices, we will use RFCOMM protocol. RFCOMM is just a serial port emulation and provides reliable data tranfer like TCP.

From ubuntu, lets open a port for communication.

$ sudo rfcomm listen /dev/rfcomm0 3 

From Android, we have to connect to ubuntu. For this, we can use Roboremo app which supports RFCOMM.

$ sudo rfcomm listen /dev/rfcomm0 3 Waiting for connection on channel 3 Connection from 88:79:7E:7B:4C:87 to /dev/rfcomm0 Press CTRL-C for hangup

Once the connection is established, we can communicate between devices.

In Unix like systems, OS provides a device file as an interface for device driver. To send and read messages from Linux or Mac is as easy as reading and writing to a file.

# to send message to bluetooth $ echo 'hello from ubuntu' > /dev/rfcomm0

We can see the received messages on Android

We can also send messages from android and read from ubuntu.

# to read messages from bluetooth $ cat /dev/rfcomm0 hello from android

This way, we can communicate with any bluetooth module using a laptop or a smart phone.

Chillar Anand
A blog about python, careers & life.
To contact me, send a message here.

Источник

Setting up a bluetooth device for use with serial communication in Linux

In this post I will explain how to setup a Bluetooth device (for example a GPS receiver) so that it can be used for serial communication through Linux’ rfcomm system.
I created some Bash scripts to make the process somewhat easier.

Читайте также:  Не включается блютуз колонка причины

Prerequisites

  • Linux distribution with Bluetooth support (BlueZ stack, available on most distro’s)
  • Bluetooth MAC address of the device to be configured. This can be found by pairing your device with
    for example a mobile phone and check in the settings for a string in the form of XX:XX:XX:XX:XX:XX

Pairing

Pairing of a device is done with the bluetoothctl program (which is part of the BlueZ stack) It’s a interactive program but with some tricks it can be executed from a script along the lines of:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
pair_device() < MAC=$1 # BT MAC address PIN=$2 # PIN needed for pairing printf "\n" echo "Setup device $MAC with PIN $PIN" printf "\n" # This block will be piped to bluetoothctl line by line < printf "power on\n\n" # Enable BT controller printf "scan on\n\n" # Enable scan mode, adds discovered devices to an internal list sleep 20 # Wait a bit until we're sure that our device is discovered printf "scan off\n\n" # Disable scan mode printf "agent on\n\n" # Enable agent (needed for pairing) printf "pair $MAC\n\n" # Start pairing process sleep 5 # Wait for the 'Enter pin' prompt printf "$PIN\n\n" # Send PIN sleep 5 printf "trust $MAC\n\n" # Trust this device so that the PIN is not sleep 5 # needed the next time the device is accessed printf "quit\n\n" >| bluetoothctl # The 'info' command gives information about a device, among others if a device is paired and/of trusted STATUS=$(bluetoothctl info $MAC | grep yes) # Check output if [[ $STATUS == *"Paired"* ]] && [[ $STATUS == *"Trusted"* ]] ; then echo "Successfully paired $MAC " return 0 fi echo "ERROR: $MAC not succesfully paired" printf "\n" return 1 >

Notes:

  • Script needs to be run as root.
  • The scan delay ( sleep 20 ) is something to play around with.
  • This script needs only to be executed once, the pairing survives reboots.

Serial port setup

Rfcomm

To use the Bluetooth device as a serial device the rfcomm command is used. This program creates a virtual device in /dev/rfcommX which then can be used as a regular serial device.

First determine the channel number by executing sdptool browse $MAC and find the channel number under the RFCOMM section.

1 2 3 4 5 6 7 8 9 10 11 12 13 14
pi@raspberrypi:~ $ sdptool browse 00:1C:88:01:DD:A4 Browsing 00:1C:88:01:DD:A4 . Service Name: SPP slave Service RecHandle: 0x10000 Service Class ID List: "Serial Port" (0x1101) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 1 Language Base Attr List: code_ISO639: 0x656e encoding: 0x6a base_offset: 0x100

Then execute the rfcomm command as follows:

sudo rfcomm bind /dev/rfcommX $MAC $CHANNEL # X is a number between 0 and 9 (e.g.) sudo rfcomm bind /dev/rfcomm0 00:1C:88:01:DD:A4 1

To check if everything went well, execute and check output:

pi@raspberrypi:~ $ rfcomm -a rfcomm0: 00:1C:88:01:DD:A4 channel 1 clean

Binding at startup

Since the binding of the port to the device as described in the previous section doesn’t survive a reboot a systemd service can be created that does the binding at system startup. This can be done as follows:

Create a file rfcomm.service with something along the lines of:

1 2 3 4 5 6 7 8 9 10 11 12 13 14
[Unit] Description=RFCOMM service After=bluetooth.service Requires=bluetooth.service [Service] Type=oneshot ExecStart=/usr/bin/rfcomm bind /dev/rfcommX $MAC $CHANNEL RemainAfterExit=true ExecStop=/usr/bin/rfcomm release /dev/rfcommX # Clean up things when service is stopped StandardOutput=journal [Install] WantedBy=multi-user.target

Copy this file to /etc/systemd/system .

Читайте также:  Не работает bluetooth nokia

Enable the service at boot time by executing:

sudo systemctrl enable rfcomm

To start the service immediately:

sudo systemctrl start rfcomm

Check if everything went well by checking:

sudo systemctrl status rfcomm

Источник

Serial Bluetooth Terminal on Linux

In this article we will look at how to establish a serial connection with devices via bluetooth .

Device Pairing

We need to pair the device before starting the communication

You can pair the device using standard bluetooth GUI utilities or command line utilties.In this article we will look at command-line utilities to do so.

[BlueZ](https://apps.ubuntu.com/cat/applications/bluez/) is the official Linux Bluetooth protocol stack This package contains tools and system daemons for using Bluetooth devices.

sudo apt-get install bluez bluez-tools 

The hciconfig command gives the Bluetooth adapter details.

#hciconfig hci0: Type: BR/EDR Bus: USB BD Address: 18:F4:6A:DE:EB:9D ACL MTU: 1021:8 SCO MTU: 64:1 UP RUNNING PSCAN ISCAN RX bytes:2492 acl:3 sco:0 events:99 errors:0 TX bytes:1421 acl:2 sco:0 commands:82 errors:0

hci0 is out bluetooth adapter name .

The hcitool scan command gives us the bluetooth device mac address and name

hcitool scan Scanning . 98:D3:31:30:1A:BA HC-05

98:D3:31:30:1A:BA is the mac address of HC-05 bluetooth module

bluez-simple-agent utility can be used to pair to the device with mac address 98:D3:31:30:1A:BA using the adapter hci0

bluez-simple-agent hci0 98:D3:31:30:1A:BA RequestPinCode (/org/bluez/3772/hci0/dev_98_D3_31_30_1A_BA) Enter PIN Code: 1234 Release New device (/org/bluez/3772/hci0/dev_98_D3_31_30_1A_BA)

To remove a paired device enter the command

bluez-simple-agent hci0 98:D3:31:30:1A:BA remove

RFComm communication protocol

RF Comm protocol handles the transmission of information and control commands from one communication device to another communication device (such as from a computer to an accessory device) that is performed serially (one bit at a time).

The Bluetooth system uses radio frequency communication (RFComm) protocol to setup and coordinate the transfer of serial data.

RFCOMM is intended to cover applications that make use of the serial ports of the devices in which they reside.In the present application the Bluetooth Device used is HC-05 which communicates using UART protocol with devices.

RFCOMM only allows one connected client per channel and supports up to 60 simultaneous connections between two BT device

  • Configure Rfcomm First Step is to configure the rfcomm module on linux to communicate with our Bluetooth module

In /etc/bluetooth/rfcomm.conf file make the following entry

98:D3:31:30:1A:BA is the mac address of our device.

The “bind no” is important, otherwise it will try to automatically bind to device and not when the user wants.

Type the command sudo rfcomm connect 0 to start the communication

#sudo rfcomm connect 0 Connected /dev/rfcomm0 to 98:D3:31:30:1A:BA on channel 1 Press CTRL-C for hangup

This will also create a serial device file /dev/rfcomm0 ,though which we can communicate with the bluetooth device.

If you have errors, restart the bluetooth service and enter the above command again

Communication with Bluetooth Device

User graphical serial terminal like CuteCom

use the /dev/rfcomm0 device to communicate with the bluetooth device.

You can send and receive commands in ASCII or HEX format via serial terminal interface.

Various bluetooth devices will correspond to different serial device files though which we can communicate with them.

Источник

Оцените статью
Adblock
detector