How to set up Bluetooth on the Raspberry Pi (…for a Lego Spike)

Jason Jurotich
May 23, 2020 · 5 min read

The title of this story should already tell you that it wasn’t as simple as one would think, but after some trial and error, I got it working, so I wanted to share what I did.

It would be nice if something that was already integrated in the Raspberry Pi would just work, but Bluetooth still seems to be seen as an extra for most operating systems, so you’ll have to install a few extra things for this to get up and running.

If you haven’t gotten your Raspberry Pi set up yet, please see my previous article on that, because I’ll be presupposing that you’ve already done those steps to have gotten to the stage where you’re now worried about this. Just keep in mind that I used Ubuntu in that article, not Raspian, given that Ubuntu is more universal.

So, the first thing you need to do is to make sure that everything is up to date. I can’t underline that enough because you’ll run into errors if you haven’t. Just in case, you can copy and paste the code below to make sure.

sudo apt update -y && sudo apt-get update -y && sudo apt-get upgrade -y && sudo apt dist-upgrade -y && sudo apt-get autoremove -y && sudo apt-get clean -y && sudo apt-get autoclean -y

When that is done, finish off with a sudo reboot .

Now that everything is up to date, let’s add the packages we’ll need for Bluetooth.

sudo apt-get install bluetooth bluez blueman pi-bluetooth python-dev libbluetooth-dev python3-pip -y && sudo pip3 install pybluez adafruit-ampy

After that, we’ll have edit the bluez.service file:

sudo vim /etc/systemd/system/dbus-org.bluez.service

In that file, there is a line that looks like this:


We’ll need to add a -C to that line and then add another right below so it ends up looking like this:

ExecStart=/usr/lib/bluetooth/bluetoothd -C
ExecStartPost=/usr/bin/sdptool add SP

(Please see my story on Vim, if you are not familiar with it.) As in the last step, when everything is set up, finish off with another sudo reboot .

So, why adafruit? Well, I wanted to use Bluetooth to comunicate with a Lego Spike. Not your average connection, I know, but seeing as we may have thousands of students playing around with these in the near future using Python, I though it would be good to get the ball rolling. We’ll need adafruit to communicate with the Spike.

After the second reboot, (the reboots are important and shouldn’t be omitted) we can now get everything configured. First, let’s get Bluetooth up and running. We’ll start with:

sudo service bluetooth start

After that we’ll enter into the bluetoothctl shell with:

sudo bluetoothctl

Once in, we will run the following commands, one after another, hitting RETURN after each one.

power on
pairable on
discoverable on
agent on

Ok, so now we can finally use our newly configured Bluetooth to set something up! First, let’s run:

bluetoothctl scan on

To find nearby bluetooth devices. We need the MAC address of the device we want. In my case, it came out this way (the MAC below is just an example):

[CHG] Device 40:HR:32:46:GH:00 Name LEGO: Hub@OBI-WAN

Now, let’s pair and trust it, hitting RETURN after each line below.

bluetoothctl pair 40:HR:32:46:GH:00
bluetoothctl trust 40:HR:32:46:GH:00

Up to this point, we have finished the steps you would follow for any bluetooth device you’d want to connect with. The following will now be applied more specifically to the Lego Spike Prime, but will be similar to any type of device that requires specific command to be sent to it.

We have a few ways to work with the Spike. The first way I never use, but I’ll mention it just to be thorough.

sudo rfcomm connect hci0 40:HR:32:46:GH:00

With that, the Spike will turn green, confirming you are connected. To escape that, we just type ctrl + c .

For me the most common use case is with adafruit. I’ll set up a .py file and have the Spike run it with the following, hitting RETURN after each line below, keeping in mind that could be any .py file you have, and you need to add the file directory before the file if it were somewhere else (ej. /testblue/

sudo rfcomm bind 0 40:HR:32:46:GH:00
sudo ampy --port /dev/rfcomm0 run

As for what you should put in the file, you always need to add import hub at the beginning or the Spike won’t know what to do with the commands you put in the file. A very simple example could be:

import hub'hello')

Or, a slightly more complex example:

import hub
accel = hub.motion.accelerometer()
xAccel = accel[0]

When you are done using the Spike, my suggestion would be to release the port:

sudo rfcomm release 0

The final way of using this is with screen. As before, if you havn’t binded the rfcomm to 0, you’ll need to do that first:

sudo rfcomm bind 0 40:HR:32:46:GH:00
screen /dev/rfcomm0

This will give you real-time access to the Spike, by means of a micro-python shell, which allows you to immediately send commands to the Spike and see outputs from the sensors.

When you first enter, it will immeditately start giving you info on the sensors, but with ctrl + c that will stop and give you a >>> so as to start sending commands.

To escape the screen (and the shell), you type ctrl + a, and then :quit

Don’t forget to sudo rfcomm release 0 when you are done.

If you want to get more involved in using Spike with micropython, you can take a look at this library here, using spikedev.

Have fun using your newly activated bluetooth on your Raspberry Pi. If you want to see a video tutorial about this, take a look here. Cheers.

Disclaimer: Some links in this article are affiliated (from Amazon). You are under no obligation to click on or use them. They are merely suggestions and are used because I wanted to help the reader find these things more easily, and was going to use links for this purpose either way.


Jason Jurotich

Written by

Masters in Philosophy, Author, Professor, Consultant, Tech Promoter and Programming Novice.


How can we use tech better? We’ll offer ways to hack what you use today to work better and smarter.

Jason Jurotich

Written by

Masters in Philosophy, Author, Professor, Consultant, Tech Promoter and Programming Novice.


How can we use tech better? We’ll offer ways to hack what you use today to work better and smarter.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store