How to use bluetooth on NAO

Sending robot audio output to external speakers with bluetooth
How to use bluetooth on NAO

How to activate bluetooth on SoftBank Robotics NAO6 robots is what we present here. Specifically, you will learn how to broadcast NAO's voice or sounds to an external Logitech Bluetooth adapter connected to a speaker. This tip is usable on NAO6.

How to broadcast robot audio outputs using bluetooth to an external speaker

To illustrate the use of Bluetooth technology on our robots, we chose a useful and simple use case to get NAO's sound redirected to a non-standard output. In a noisy environment, it can be difficult to hear the sound coming from the robot's loudspeakers, or to understand the human voice input correctly. In these environments, you may need to consider broadcasting the audio output to external speakers and getting voice input from an external microphone.

To keep things simple, we will simply show you how to broadcast sound to speakers: we present here the general case of connecting NAO to a Bluetooth adapter which is in turn connected to a speaker using a cable. It can be adapted to your own equipment.

What is bluetooth?

Bluetooth (BT) has become over the years a standard and very popular short-range wireless interconnection of cellular phones, computers and electronic devices as keyboards, mouses, headphones, microphones, IoT objects… We decided at SoftBank Robotics to embed such a device on the NAO6 robot and on the latest Pepper versions.

Bluetooth logo
Bluetooth logo

Bluetooth is a radio signal that operates at 2.4Ghz (IMS band) in a short range, which means a distance between 10 to 100 meters.

Bluetooth first appeared in 1999 and got improved over the years with increase in bandwidth, security upgrades, power consumption enhancement... The versions, also known as bluetooth standards, are published by an industry consortium known as the Bluetooth SIG (Special Interest Group).

Overview of the versions:

  • 1.x - 1999 : limitations including interoperability issues
  • 2.0 + EDR - 2004 - Enhanced Data Rate, up to 2.1 Mbits/s
  • 2.1 + EDR - 2007 - Secure Simple Pairing
  • 3.0 + HS - 2009 - up to 24 Mbits/s (using WiFi)
  • 4.0 - 2010 - Low Energy
  • 4.1 - 2013 - Further LE improvements
  • 4.2 - 2014 - LE security improvements, IoT
  • 5.0 - 2016 - IoT improvements
  • 5.1 - Jan 2019 - Tracking
  • 5.2 - Dec 2019 - LE improvements, multi audio connection

On NAO robots the bluetooth device (WPAN IEEE 80.5.1) uses standard 4.0 Low Energy (LE).

In Linux, the canonical implementation of the Bluetooth protocol stack is BlueZ. That's what we use on NAO and that's what you will interact with.

What you will need

In this article, we will show how to connect a robot to external speakers through a bluetooth receiver, so you will obviously need:

  • A NAO6
  • A Bluetooth Adapter to receive the bluetooth signal from NAO
  • A speaker to connect to the BT receiver
  • Optionally: an amplifier

We based this tutorial on a Logitech Bluetooth Audio receiver like the one presented below.

Logitech Bluetooth Audio receiver
Logitech Bluetooth Audio receiver

Logitech Bluetooth® Audio receiver
Dimensions (HWD) : 235158mm

You can adapt the example using a bluetooth-ready speaker for instance.

Let's connect the devices together

You need to perform several actions: ssh connection to your robot, activation of bluetooth device and tethering, connection to bluetooth device (pairing) and making the connection permanent after a reboot. Let's go!

1.1 Connecting to the robot through ssh

To activate bluetooth on the robot, you need to connect to it using ssh.

Connect using the username "nao" and the password you set when you first configured the robot.

$ ssh nao@<ROBOT-IP>
Password: ***
Nao [0] ~ $ 

1.2 Enabling Bluetooth and tethering

1.2.1 Enabling bluetooth on your device

Bluetooth is not enabled by default on NAO.

To check if it is enabled use the command rfkill as follows:

Nao [0] ~ $ rfkill list
0: phy0: Wireless LAN
  Soft blocked: no
  Hard blocked: no
1: hci0: Bluetooth
  Soft blocked: yes
  Hard blocked: no

"Soft blocked: yes" means it is not activated.

To enable bluetooth on the robot run:

Nao [0] ~ $ connmanctl enable bluetooth
Enabled bluetooth

You can check again with rfkill. The line for the bluetooth device should display 'Soft blocked: no'.

1.2.2 Turning on bluetooth tethering

Now you need to enable tethering. To do so run the following command:

Nao [0] ~ $ connmanctl tether bluetooth on
Enabled tethering for bluetooth

1.3 Detecting and connecting to the bluetooth device

Now you need to manually make your robot bluetooth device scan and connect to the BT audio receiver device. We use bluetoothctl, a console front-end to pair a device from the shell, as it is the simplest and most reliable option.

The steps are the following:

  • Scan
  • Pair
  • Trust
  • Connect

But first you need to set the device you want to connect to "detectable". On the Logitech BT you need to press the top button, and you should hear a sound getting out of the speaker.

Next, launch the bluetoothctl command:

Nao [0] ~ $ bluetoothctl 
Agent registered
[bluetooth]# 

Then chain the following commands:

Nao [0] ~ $ bluetoothctl 
Agent registered
[bluetooth]# scan on
Discovery started

You should see the other devices appear after a few seconds. It takes some time.

You can list the devices as follow:

[bluetooth]# devices 
Device 0C:23:23:3F:11:8B PAR-1111-LA
[NEW] Device 01:23:45:AB:CD:EF Logitech BT Adapter
...

In the list you should see your device with its own uuid and device alias, such as "Logitech BT Adapter". We will use the dummy UUID 01:23:45:AB:CD:EF in this article.

You can pair, trust and connect to this device as follows. You can auto-complete the device number using the '<TAB>' key.

[bluetooth]# pair 01:23:45:AB:CD:EF 
Attempting to pair with 01:23:45:AB:CD:EF
[CHG] Device 01:23:45:AB:CD:EF Connected: yes
[CHG] Device 01:23:45:AB:CD:EF Paired: yes
Pairing successful

[bluetooth]# trust 01:23:45:AB:CD:EF 
[CHG] Device 01:23:45:AB:CD:EF Trusted: yes
Changing 01:23:45:AB:CD:EF trust succeeded

[bluetooth]# connect 01:23:45:AB:CD:EF 
Attempting to connect to 01:23:45:AB:CD:EF
[CHG] Device 01:23:45:AB:CD:EF Connected: yes
[CHG] Device 01:23:45:AB:CD:EF UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device 01:23:45:AB:CD:EF ServicesResolved: yes
[CHG] Device 01:23:45:AB:CD:EF Paired: yes
Connection successful

[Logitech BT Adapter]# 

You can now leave bluetoothclt console typing "exit", "quit" or Ctrl+D

1.4 Broadcasting sound to bluetooth device

Now let's redirect the audio output to the Logitech BT adapter. We will now use the console pulse audio tools named pactl and pacmd.

First, we need to list the 'sink' for pulse audio that can collect the audio output. Using the following command you should get this output:

Nao [0] ~ $ pactl list short sinks
0 speaker module-alsa-card.c  float32le 2ch 48000Hz SUSPENDED
1 bluez_sink.01_23_45_AB_CD_EF  module-bluez5-device.c  s16le 2ch 48000Hz IDLE

If the line containing the pattern "bluez_sink" does not appear, you are not connected to the bluetooth device anymore. Rerun the connect bluetoothctl command again.

Now you need to set the default sink to send the audio to. Use the pacmd command as follows (adapt the device id to yours):

Nao [0] ~ $ pacmd set-default-sink bluez_sink.01_23_45_AB_CD_EF
Nao [0] ~ $

On success the command returns nothing.

You can now test the audio output with any wav file installed on the robot:

Nao [0] ~ $ aplay  /opt/aldebaran/share/naoqi/wav/start_jingle.wav 

You should hear the "Ognagnouk" robot jingle coming out of the Logitech BT speaker.

Great job!

Set the default sink in a single line

To set the default sink in a single command you can chain the commands as follows:

Nao [0] ~ $ sink=$(pactl list short sinks | grep bluez | awk '{print $2}') ;  if [ -n "$sink" ]; then pacmd set-default-sink "$sink" && echo OK ; else echo Could not find sink ; fi 

It will return "OK" on success, and "Could not find sink" if no bluetooth output for audio is available.

1.5 How to make connection permanent on reboot

The previous configuration will not be permanent. When rebooting the robot, you will need to do the bluetooth connect command again to get the connection set back up.

In order to make your connection permanent, you need to change a few settings on the robot system configuration. This will be possible on NAO which grants you root privileges using sudo.

1.5.1 Pulse audio configuration

First edit /etc/pulse/default.pa, adding the following load-module command at the end of the file:

\# automatically switch to newly-connected devices
load-module module-switch-on-connect

The first line is a comment that can be removed.

1.5.2 Bluetooth policy configuration

Now edit /etc/bluetooth/main.conf: at the end of the file, add "AutoEnable" in the existing Policy section or create a policy section. It should look as follows:

[Policy]
AutoEnable=true

Now you can reboot, the connection will be automatic.

You can try it using Naoqi text-to-speech or playing a wav file with aplay as earlier.

Nao [0] ~ $ qicli call ALTextToSpeech.say "Bluetooth rocks" English 

Enjoy!

Learn more on our experiment

Since April 2019, a NAO robot animates a documentation space of "The ROBOTS" exhibition at the Cité des Sciences et de l'Industrie in Paris (the exhibition will last for 5 years). There, a NAO 6 robot behaves as the mascot of the exhibition. NAO welcomes visitors and introduces the various elements of the exhibition.

The robot is protected from visitors by a glass enclosure and is placed on a stage. We did not need to broadcast the sound to external speakers but the visitor's voice needed to be captured with an external microphone. This is another story that you could explore based on this example detailed above (as a hint, focus on 'sources' instead of 'sinks').

NAO at the Cité des Sciences
NAO at the Cité des Sciences

Read more on how Pepper and NAO show off at the Cité des Sciences et de l'Industrie and discover the interview of the project manager who was in charge of the set up of the exhibition in collaboration with Universciences.