{"id":23883,"date":"2018-06-18T17:31:53","date_gmt":"2018-06-18T22:31:53","guid":{"rendered":"http:\/\/www.multitech.net\/developer\/?page_id=23883"},"modified":"2019-09-04T09:13:50","modified_gmt":"2019-09-04T14:13:50","slug":"bluez-5-serial-setup","status":"publish","type":"page","link":"https:\/\/www.multitech.net\/developer\/software\/mlinux\/using-mlinux\/bluez-5-serial-setup\/","title":{"rendered":"bluez5 Serial Setup"},"content":{"rendered":"<p><strong>NOTE:<\/strong>\u00a0This information only applies to devices with Bluetooth using\u00a0mLinux<sup>\u2122<\/sup> 4.x or higher which has bluez5. For devices using mLinux 3.x, refer to\u00a0<a href=\"https:\/\/www.multitech.net\/developer\/software\/mlinux\/using-mlinux\/bluetooth\/\">Bluetooth<\/a>.<\/p>\n<h2>Incompatible with Bluez 4<\/h2>\n<p>Bluez 5 is not compatible with Bluez 4 for the most part. There is more emphasis on services using UUID&#8217;s in standard Bluetooth. The command rfcomm is deprecated. A DBUS implementation has replaced the rfcomm command line utility.<\/p>\n<p>For Bluez 5, new applications should use PAN in most cases. PAN creates an internet connection which is much more flexible than RFCOMM. See: <a title=\"Bluez 5 PAN setup\" href=\"https:\/\/www.multitech.net\/developer\/software\/mlinux\/using-mlinux\/bluez-5-pan-setup\/\">Bluez 5 PAN_setup.<\/a><\/p>\n<h2>Name the device<\/h2>\n<ol>\n<li>Give the device a reasonable name.<\/li>\n<li>Create the file \/etc\/bluetooth\/main.conf:<\/li>\n<\/ol>\n<pre class=\"brush:shell\" style=\"padding-left: 60px;\">[General]\r\nName = jakMTCDT<\/pre>\n<h2>Pair the device<\/h2>\n<p>For Linux pairing, see the <a href=\"https:\/\/www.multitech.net\/developer\/software\/mlinux\/using-mlinux\/pairing\/\">Pairing<\/a>.<\/p>\n<p>For Windows, see\u00a0<a title=\"Bluez 5 PAN setup\" href=\"https:\/\/www.multitech.net\/developer\/software\/mlinux\/using-mlinux\/bluez-5-pan-setup\/\">Bluez 5 PAN_setup.<\/a><\/p>\n<h2>rfcomm Python script<\/h2>\n<p>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 <i>rfcomm<\/i>. rfcomm is found in the directory \/usr\/libexec\/bluetooth\/.<\/p>\n<h3>syntax<\/h3>\n<pre class=\"brush:shell\">nrt0:~$ \/usr\/libexec\/bluetooth\/rfcomm --help\r\nusage: rfcomm [-h] [-u uuid_or_shortcut] [--pseudoterminal] [--loopback]\r\n              [--debug] [--login]\r\n\r\nBlueZ RFCOMM server.\r\n\r\noptional arguments:\r\n  -h, --help            show this help message and exit\r\n  -u uuid_or_shortcut, --uuid uuid_or_shortcut\r\n                        Service UUID to use. Can be either full UUID or one of\r\n                        the shortcuts: gn, panu, nap. Default: spp.\r\n  --pseudoterminal      Create a pseudoterminal and put slave in \/run\/rfcomm\r\n                        Suitable for background operation.\r\n  --loopback            Echo data for testing (exclusive with pseudoterminal)\r\n  --debug               Verbose operation mode.\r\n  --login               Use RFCOMM to log into this device.<\/pre>\n<p>There are basically four modes of operation for rfcomm. The uuid and debug mode may be used with any of the four modes.<\/p>\n<ul>\n<li>Terminal &#8212; 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.<\/li>\n<li>Loopback &#8212; Anything written by the remote application is echoed back. This is a test mode.<\/li>\n<li>Pseudoterminal &#8212; A link to a pseudoterminal is created in \/run\/rfcomm. This pseudoterminal communicates with the remote program from the device.<\/li>\n<li>Login &#8212; Login is started and communicates with the remote application.<\/li>\n<\/ul>\n<h3>Qwerty Bluetooth Terminal Application<\/h3>\n<p>This example uses the Qwerty Bluetooth Terminal Application, which lists one author as <i>qwerty<\/i>.<\/p>\n<p>But there is more than one author. Refer to app link for more details:\u00a0<a href=\"https:\/\/play.google.com\/store\/apps\/details?id=Qwerty.BluetoothTerminal\">https:\/\/play.google.com\/store\/apps\/details?id=Qwerty.BluetoothTerminal<\/a><\/p>\n<h3>Prep for all four cases<\/h3>\n<p>First, pair your phone with the device. Follow the instructions for\u00a0<a href=\"https:\/\/www.multitech.net\/developer\/software\/mlinux\/using-mlinux\/pairing\/\">Pairing<\/a>.<\/p>\n<h2>Example: Using Android Bluetooth Terminal with Bluez 5<\/h2>\n<h3>Set up Android<\/h3>\n<p>The Android terminal is used on Android 7 in this example.<\/p>\n<p>First, pair the device as shown. If you have not already done so, refer to:\u00a0<a href=\"https:\/\/www.multitech.net\/developer\/software\/mlinux\/using-mlinux\/pairing\/\">Pairing<\/a><\/p>\n<ol>\n<li>Download and install the Android Bluetooth Terminal App.<\/li>\n<li>Start the application, and select Make discoverable.<\/li>\n<li>Then give permissions so that discovery is allowed.<\/li>\n<li>Also, from the menu at the top right choose <i>Setup<\/i> and select <i>Append newline<\/i>. The new line is unfortunately a carriage return followed by a line feed, which is not what most Linux programs expect.<\/li>\n<\/ol>\n<h3>Loopback test<\/h3>\n<p>For the first example, execute \/usr\/libexec\/bluetooth\/rfcomm in loopback mode:<\/p>\n<pre class=\"brush:shell\">\/usr\/libexec\/bluetooth\/rfcomm --loopback\r\nStarting Serial Port Profile...<\/pre>\n<p>Now try to connect from the phone&#8217;s Bluetooth terminal application menu using <i>Connect a device &#8211; Insecure<\/i>. Once it connects, you can start typing. The\u00a0Conduit<sup>\u00ae<\/sup> does not receive the message until the send button is pressed. Anything you type is echoed on the screen after hitting\u00a0<i>Send<\/i>. Each line appears twice. Enter &lt;Ctrl&gt;C on the Conduit to terminate the test.<\/p>\n<h3>Terminal<\/h3>\n<p>Enter:<\/p>\n<pre class=\"brush:shell\">\/usr\/libexec\/bluetooth\/rfcomm<\/pre>\n<p>Running the program:<\/p>\n<pre class=\"brush:shell\">mtcdt:\/home\/mtadm# \/usr\/libexec\/bluetooth\/rfcomm\r\nmain: doterm is True\r\nStarting Serial Port Profile...\r\nNewConnection(\/org\/bluez\/hci0\/dev_DC_74_A8_05_39_77, John Cell Phone, UnixFd:9)\r\nTTY&gt; \r\nhello\r\n\r\nTTY&gt; hi\r\nTTY&gt;<\/pre>\n<p>By typing <i>hello<\/i> &lt;Send&gt; on the Android phone, the text appears after the TTY&gt; prompt on the device. When you type hi on the device, it appears on the device and the phone. Type &lt;Ctrl&gt;C to terminate the experiment.<\/p>\n<h3>Pseudo-Terminal<\/h3>\n<p>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.<\/p>\n<p>1. Set up the rfcomm daemon by editing \/etc\/default\/rfcomm. This is what it should look like after editing:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">ENABLED=\"yes\"\r\nRFCOMMOPTS=\"--pseudoterminal<\/pre>\n<p>2. Now start the daemon:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">\/etc\/init.d\/rfcomm restart<\/pre>\n<p>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:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">mtcdt:\/run\/rfcomm# ls\r\ndev_DC_74_A8_05_39_77_John Cell Phone_pts1<\/pre>\n<p>5. To use microcom, we use pts1:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">mtcdt:~$ ls \/dev\/pts\/1\r\n\/dev\/pts\/1<\/pre>\n<p>6. To use minicom, run as root for access to the pseudo-terminal:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">mtcdt:\/run\/rfcomm# minicom -p \/dev\/pts\/1<\/pre>\n<p>NOTE: You may want to turn local echo on:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">&lt;ctrl&gt;AZE<\/pre>\n<p>In this case, your typing appears on the screen along with the Android Phone&#8217;s typing after hitting &lt;send&gt;.\u00a0Microcom is simpler. Minicom does not echo characters.<\/p>\n<p>7. To establish the psuedoterminal connection in microcom, include the link information from step 4.<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">mtcdt:\/run\/rfcomm# microcom dev_DC_74_A8_05_39_77_John\\ Cell\\ Phone_pts1<\/pre>\n<h3>RFCOMM with login<\/h3>\n<p>1. Edit \/etc\/default\/rfcomm to use login:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">ENABLED=\"yes\"\r\nRFCOMMOPTS=\"--login\"<\/pre>\n<p>2. Restart the daemon:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">mtcdt:\/run\/rfcomm# \/etc\/init.d\/rfcomm restart\r\nStopping \r\nStarting rfcomm\r\n\/usr\/sbin\/start-stop-daemon -S -p \/run\/rfcomm.pid -x \/usr\/bin\/python2.7 -b -- \/usr\/libexec\/bluetooth\/rfcomm --login<\/pre>\n<p>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:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">mtadm\r\nmtcdt login mtadm\r\n[password]\r\nLast login: Tue Jun 12 15:01:51 CDT 2018 on pts\/1\r\nmtcdt:-$ stty igncr<\/pre>\n<p>4. The first item entered on the phone is <strong>mtadm<i>&lt;send&gt;<\/i><\/strong>. This is followed by the <strong>password&lt;send&gt;<\/strong>.<\/p>\n<p>5. Instruct the terminal handler to ignore carriage returns with the command <strong>stty igncr<\/strong>. The extra carriage return causes sudo to interpret the carriage return as part of the password. Entering <i>exit<\/i> on the Bluetooth Terminal program terminates your session.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NOTE:\u00a0This information only applies to devices with Bluetooth using\u00a0mLinux\u2122 4.x or higher which has bluez5. For devices using mLinux 3.x, refer to\u00a0Bluetooth. Incompatible with Bluez 4 Bluez 5 is not compatible with Bluez 4 for the most part. There is more emphasis on services using UUID&#8217;s in standard Bluetooth. The command rfcomm is deprecated. A [&hellip;]<\/p>\n","protected":false},"author":1478,"featured_media":0,"parent":9184,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"class_list":["post-23883","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/23883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/users\/1478"}],"replies":[{"embeddable":true,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/comments?post=23883"}],"version-history":[{"count":14,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/23883\/revisions"}],"predecessor-version":[{"id":23887,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/23883\/revisions\/23887"}],"up":[{"embeddable":true,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/9184"}],"wp:attachment":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/media?parent=23883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}