Scan for Bluetooth Beacons using RPi 3
In this tutorial I'll show you how to scan for Bluetooth beacons using a Raspberry Pi 3. There are lots of interesting use cases for beacons, including environmental sensors.
If you're not familiar with beacons, they are basically one-way Bluetooth broadcasters, broadcasting data to any listener in range. They have long battery life (years), their range is decent (50 yards) and getting better with newer designs, and they are more reliable than your typical Bluetooth connection.
So lets get to it. When you've finished this tutorial you'll be scanning for beacons using your own Node.js application running on the RPi 3.
If you're not familiar with beacons, they are basically one-way Bluetooth broadcasters, broadcasting data to any listener in range. They have long battery life (years), their range is decent (50 yards) and getting better with newer designs, and they are more reliable than your typical Bluetooth connection.
So lets get to it. When you've finished this tutorial you'll be scanning for beacons using your own Node.js application running on the RPi 3.
What you will need?
|
You can use any beacon you'd like, but above are a few examples.
I'm going to assume your RPi3 has the Raspbian OS installed and you can access the terminal either through SSH or the GUI. If this is not the case, check out these tutorials for setting up Raspbian and SSH on the RPi 3
I'm going to assume your RPi3 has the Raspbian OS installed and you can access the terminal either through SSH or the GUI. If this is not the case, check out these tutorials for setting up Raspbian and SSH on the RPi 3
- Installing Raspbian OS: https://www.raspberrypi.org/documentation/installation/installing-images/README.md
- Getting SSH working: https://www.raspberrypi.org/documentation/remote-access/ssh/
Before doing anything, make sure you have the latest Raspbian updates. Here are the commands to run to update everything. If you don't do this you can run into all kinds of strange Bluetooth or setup issues which you'd rather avoid.
Code Editor
Step 1: Get Bluetooth Setup
Lets setup and enable the Bluetooth on the PI. Run the following command to install bluez and the required libraries. Our Node.js app will rely on these libraries to talk Bluetooth.
Now lets quickly verify Bluetooth is working and move on to writing code! Use the commands below to scan for beacons. You should see your Bluetooth beacons show up with an ID that looks like "Device F8:AB:......". If you see this you're good.
Code Editor
Step 2: Install Node.js
We'll write our code using Node.js. If you're using Raspbian, node is probably already installed. Run the following commands to check.
npm is probably not installed by default. Use the commands below to install node and npm as needed.
Step 3: Create our Project
We're ready to create our node project and bring in the required libraries to talk Bluetooth. Create a directory for the project, CD into it, and run the following commands to initialize the project and then add the noble library.
Step 4: Write our Code
Now let’s write the code. Create a file in the project directory. Call it “index.js” (node default name). Copy in the following code.
index.js
Step 5: Run it
Use the following command to run the program.
If everything is working you should see the raw beacon ID's printed to the terminal. It should look like this.
Congratulations, you're scanning Beacons!
What's next?
Beacons advertisements also contain data. In the next tutorial I'll show you how to parse out this data and make it useful. We'll cover iBeacon and Eddystone (the most common), but we'll also look at Ruuvi and Estimote, which use custom formats that contain enviornmental sensor data.