bluez5 Serial Setup
NOTE: This information only applies to devices with Bluetooth using mLinux™ 4.x or higher which has bluez5. For devices using mLinux 3.x, refer to Bluetooth.
Incompatible with Bluez 4
Bluez 5 is not compatible with Bluez 4 for the most part. There is more emphasis on services using UUID’s in standard Bluetooth. The command rfcomm is deprecated. A DBUS implementation has replaced the rfcomm command line utility.
For Bluez 5, new applications should use PAN in most cases. PAN creates an internet connection which is much more flexible than RFCOMM. See: Bluez 5 PAN_setup.
Name the device
- Give the device a reasonable name.
- Create the file /etc/bluetooth/main.conf:
[General] Name = jakMTCDT
Pair the device
For Linux pairing, see the Pairing.
For Windows, see Bluez 5 PAN_setup.
rfcomm Python script
RFCOMM is controlled using DBUS and works best with a language like C or Python. To assist with writing an RFCOMM application, mLinux comes with an example program called rfcomm. rfcomm is found in the directory /usr/libexec/bluetooth/.
syntax
nrt0:~$ /usr/libexec/bluetooth/rfcomm --help usage: rfcomm [-h] [-u uuid_or_shortcut] [--pseudoterminal] [--loopback] [--debug] [--login] BlueZ RFCOMM server. optional arguments: -h, --help show this help message and exit -u uuid_or_shortcut, --uuid uuid_or_shortcut Service UUID to use. Can be either full UUID or one of the shortcuts: gn, panu, nap. Default: spp. --pseudoterminal Create a pseudoterminal and put slave in /run/rfcomm Suitable for background operation. --loopback Echo data for testing (exclusive with pseudoterminal) --debug Verbose operation mode. --login Use RFCOMM to log into this device.
There are basically four modes of operation for rfcomm. The uuid and debug mode may be used with any of the four modes.
- Terminal — This is a foreground operation. The python program copies data written to it to the remote terminal and it writes data sent by the remote bluetooth application.
- Loopback — Anything written by the remote application is echoed back. This is a test mode.
- Pseudoterminal — A link to a pseudoterminal is created in /run/rfcomm. This pseudoterminal communicates with the remote program from the device.
- Login — Login is started and communicates with the remote application.
Qwerty Bluetooth Terminal Application
This example uses the Qwerty Bluetooth Terminal Application, which lists one author as qwerty.
But there is more than one author. Refer to app link for more details: https://play.google.com/store/apps/details?id=Qwerty.BluetoothTerminal
Prep for all four cases
First, pair your phone with the device. Follow the instructions for Pairing.
Example: Using Android Bluetooth Terminal with Bluez 5
Set up Android
The Android terminal is used on Android 7 in this example.
First, pair the device as shown. If you have not already done so, refer to: Pairing
- Download and install the Android Bluetooth Terminal App.
- Start the application, and select Make discoverable.
- Then give permissions so that discovery is allowed.
- Also, from the menu at the top right choose Setup and select Append newline. The new line is unfortunately a carriage return followed by a line feed, which is not what most Linux programs expect.
Loopback test
For the first example, execute /usr/libexec/bluetooth/rfcomm in loopback mode:
/usr/libexec/bluetooth/rfcomm --loopback Starting Serial Port Profile...
Now try to connect from the phone’s Bluetooth terminal application menu using Connect a device – Insecure. Once it connects, you can start typing. The Conduit® does not receive the message until the send button is pressed. Anything you type is echoed on the screen after hitting Send. Each line appears twice. Enter <Ctrl>C on the Conduit to terminate the test.
Terminal
Enter:
/usr/libexec/bluetooth/rfcomm
Running the program:
mtcdt:/home/mtadm# /usr/libexec/bluetooth/rfcomm main: doterm is True Starting Serial Port Profile... NewConnection(/org/bluez/hci0/dev_DC_74_A8_05_39_77, John Cell Phone, UnixFd:9) TTY> hello TTY> hi TTY>
By typing hello <Send> on the Android phone, the text appears after the TTY> prompt on the device. When you type hi on the device, it appears on the device and the phone. Type <Ctrl>C to terminate the experiment.
Pseudo-Terminal
This mode of operation is the closest to mLinux 3. In this example, we run the rfcomm python program as a daemon. When connections are made to the daemon, a pseudo terminal is created. A link to the psuedoterminal is found in /run/rfcomm. This link can be used directly by microcom, but minicom does not accept links. The link name tells you the pseudo terminal number, which may be used with microcom.
1. Set up the rfcomm daemon by editing /etc/default/rfcomm. This is what it should look like after editing:
ENABLED="yes" RFCOMMOPTS="--pseudoterminal
2. Now start the daemon:
/etc/init.d/rfcomm restart
4. After making a connection, check the directory list for the link. Note the name. You will use this information later in a microcom command:
mtcdt:/run/rfcomm# ls dev_DC_74_A8_05_39_77_John Cell Phone_pts1
5. To use microcom, we use pts1:
mtcdt:~$ ls /dev/pts/1 /dev/pts/1
6. To use minicom, run as root for access to the pseudo-terminal:
mtcdt:/run/rfcomm# minicom -p /dev/pts/1
NOTE: You may want to turn local echo on:
<ctrl>AZE
In this case, your typing appears on the screen along with the Android Phone’s typing after hitting <send>. Microcom is simpler. Minicom does not echo characters.
7. To establish the psuedoterminal connection in microcom, include the link information from step 4.
mtcdt:/run/rfcomm# microcom dev_DC_74_A8_05_39_77_John\ Cell\ Phone_pts1
RFCOMM with login
1. Edit /etc/default/rfcomm to use login:
ENABLED="yes" RFCOMMOPTS="--login"
2. Restart the daemon:
mtcdt:/run/rfcomm# /etc/init.d/rfcomm restart Stopping Starting rfcomm /usr/sbin/start-stop-daemon -S -p /run/rfcomm.pid -x /usr/bin/python2.7 -b -- /usr/libexec/bluetooth/rfcomm --login
3. With the Android Bluetooth terminal, commands are sent one line at a time. Nothing is printed until the carriage return/line feed resulting in no prompt. Once connected, enter the user-name and password without a prompt. The output on the Android screen looks like this:
mtadm mtcdt login mtadm [password] Last login: Tue Jun 12 15:01:51 CDT 2018 on pts/1 mtcdt:-$ stty igncr
4. The first item entered on the phone is mtadm<send>. This is followed by the password<send>.
5. Instruct the terminal handler to ignore carriage returns with the command stty igncr. The extra carriage return causes sudo to interpret the carriage return as part of the password. Entering exit on the Bluetooth Terminal program terminates your session.