How to use bluetooth on NAO

Modified on Thu, 10 Mar, 2022 at 5:33 PM

     This article explains how to use Bluetooth connectivity on NAO 6.

     Specifically, you will learn how to broadcast NAO's voice or sounds to an external Logitech Bluetooth adapter connected to a speaker.


CONTENT



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


     As an example, the use case here is 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.


     Note: 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 :



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.



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 


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article