Using Bluetooth Serial Port Profile Print

 

Bluetooth is widely used when it is necessary to provide a wireless access to the devices located in short distances. In this application note we will show how to organize a wireless control channel to the NXP i.MX RT1060 EVK running Linux using a USB Bluetooth adapter. In practical embedded applications such a wireless channel may be used to implement a command/response protocol to control your i.MX RT1060 based device via Bluetooth from the host machine (computer, notebook, smart-phone, etc.).


Hardware Platform

The hardware platform used in this application note is the NXP i.MX RT1060 EVK board with a USB Bluetooth adapter plugged into the USB OTG 2 (J10) connector. The adapter used by Emcraft to perform the tests documented below was based on the LM506 chip. The generic Linux kernel device driver for the USB transport HCI layer (CONFIG_BT_HCIBTUSB) is used in this configuration so other USB Bluetooth adapters should work as described below too.


Software Platform

The goal is to have the i.MX RT1060 act as a device with the Serial Port Profile (SPP) in the Bluetooth network. The i.MX RT1060 will emulate the serial port over the Bluetooth transport and therefore the host will see the i.MX RT1060 as an ordinary serial RS-232 port.

The Bluetooth Serial Port Profile interface is implemented with the BT_RFCOMM_TTY kernel driver. The sdptool and rfcomm utilities, ported from the bluez-utils package to the Linux NXP i.MX RT1060 EVK BSP, are used for configuration. The sdptool utility is used to create the "Serial Port" profile, exported over Bluetooth. The rfcomm utility is started in the "listen" mode and, on the "Serial Port" profile activation, rfcomm emulates a virtual COM port on the /dev/rfcommX device file, looking from the i.MX RT1060 side.

The functionality described below is available from the rootfs.uImage project provided by Emcraft for NXP i.MX RT1060 EVK.


Test Setup

We will use the following terminology below:

  • Target: NXP i.MX RT1060 EVK board with the LM506 Bluetooth adapter plugged into the USB OTG 2 (J10) port.
    The Bluetooth <Target address> in the examples below is 5C:F3:70:70:FA:D5.
  • Host: Any computer with a Bluetooth interface, running Linux with the Bluetooth tools (bluez-utils) installed.
    The Bluetooth <Host address> in the examples below is C4:D9:87:8C:B2:24.

Test Connectivity

Power-on the NXP i.MX RT1060 EVK board and wait for the Linux to boot on the Target. Run the Bluetooth daemons in the background:

...
init started: BusyBox v1.24.2 (2017-10-18 21:56:31 +0400)
/ # hcid -n&
/ # sdpd -n&

Plug-in the Bluetooth adapter to the USB OTG 2 (J10) interface of the NXP i.MX RT1060 EVK board. Observe the messages like these in the Target console:

usb 1-1: new full-speed USB device number 2 using ci_hdrc
hcid[99]: HCI dev 0 registered
Bluetooth: hci0: BCM: chip id 63
Bluetooth: hci0: BCM20702A
Bluetooth: hci0: BCM20702A1 (001.002.014) build 0000
Bluetooth: hci0: BCM20702A1 (001.002.014) build 1338
Bluetooth: hci0: Broadcom Bluetooth Device
hcid[99]: HCI dev 0 up
hcid[99]: Starting security manager 0

Get the <Target address> Bluetooth address of the adapter you have just plugged-in:

/ # cat /sys/class/bluetooth/hci0/address
5c:f3:70:70:fa:d5

Add the Serial Port profile to the list of Bluetooth profiles. In the example below the Bluetooth channel number to access this profile is 1:

/ # sdptool add --channel=1 SP
Serial Port service registered

Start the background listening for raw connections on the channel 1:

/ # rfcomm --raw listen /dev/rfcomm0 1 &
Waiting for connection on channel 1

On the Host, open a raw Bluetooth connection to the Target (<Target Address> Bluetooth Device, channel 1):

[yur@ubuntu rootfs]$ sudo rfcomm --raw connect 0 5c:f3:70:70:fa:d5 1
Connected /dev/rfcomm0 to 5C:F3:70:70:FA:D5 on channel 1
Press CTRL-C for hangup

In the Target console observe an indication of the connection from the Host:

Connection from C4:D9:87:8C:B2:24 to /dev/rfcomm0
Press CTRL-C for hangup

Send a text string (command) to the Target over the Bluetooth serial port from the Host:

[yur@ubuntu rootfs]$ echo "Hello from Host over the BT Serial" | sudo tee /dev/rfcomm0
Hello from Host over the BT Serial

On the Target receive the text string (command) just sent from Host:

/ # cat /dev/rfcomm0
Hello from Host over the BT Serial
^C

Send a text string (response) from the Target to Host over the Bluetooth serial port:

/ # echo "Hello from Target over the BT Serial" > /dev/rfcomm0

Receive the text string (response) just sent from the Target over the Bluetooth serial port:

[yur@ubuntu rootfs]$ sudo cat /dev/rfcomm0
Hello from Target over the BT Serial
^C

Disconnect the Host from the Bluetooth serial line by pressing Ctrl-C in the rfcomm --raw connect ... terminal window:

^C
Disconnected

Observe the rfcomm termination on the Target side:

Disconnected
[2] Donerfcomm --raw listen /dev/rfcomm0 1
/ #

Please note that the rfcomm --raw listen ... command must be re-run on the Target to reopen the Bluetooth serial link again.