BluetoothLE on a Raspberry Pi

From csn
Revision as of 03:53, 12 October 2020 by David (talk | contribs) (→‎Challenge)
Jump to navigation Jump to search

Sending Bluetooth LE Beacons

In this first activity, we will show you how you can craft a Bluetooth LE beacon using the Raspberry Pi.

Start by logging into your Raspberry Pi and then check that Bluez is installed:

sudo apt install bluez

We are going to use the Raspberry Pi to transmit an Eddystone beacon.

Lets look at the available commands:

hciconfig -h

Lets bring up the Bluetooth device on our Raspberry Pi using below command:

sudo hciconfig hci0 up

Then, lets set the Bluetooth interface to advertise but not be connectable:

sudo hciconfig hci0 leadv 3

Beacons containing URLs

Now we will construct and send the Bluetooth LE beacon:

sudo hcitool -i hci0 cmd 0x08 0x0008 1c 02 01 06 03 03 aa fe 14 16 aa fe 10 00 03 6D 75 72 64 6F 63 68 2E 65 64 75 2E 61 75 00 00 00
[Linux commands        ]  OFG   OCF  [Len] [Flag+L] [UUID + ? ] [TypeIF] [URL+TX] [This is the URL based Payload                    ]

This will create a Bluetooth low energy beacon that points to murdoch.edu.au

We will read this BTLE becon on our smartphone. Download and open the Bluetooth app linked to below based on your phone:

Pickup the Eddystone beacon and visit the URL, where does it take you?

Challenge

Go here https://www.rapidtables.com/convert/number/ascii-hex-bin-dec-converter.html and then reverse engineer the string above. You know it points to murdoch.edu.au.

Hints

The maximum characters that can be transmitted are 16, so you will want to look at a URL shortener like:

*https://bitly.com
*https://tinyurl.com/

Please also read the following link to workout how .com is really encoded.

Beacons containing Sensor data

Try the following:

sudo hcitool -i hci0 cmd 0x08 0x0008 1c 02 01 06 03 03 aa fe 14 16 aa fe 20 00 03 01 02 03 04

Scan on your phone, play with and try changing the values while reading: https://github.com/google/eddystone/blob/master/eddystone-tlm/tlm-plain.md

Challenge

The following python code will report the temp of the CPU. Can you get this temperature value into the beacon:

#!/usr/bin/python3

import RPi.GPIO as GPIO
import time
import os
import sys
import signal
import subprocess

temp = os.popen('vcgencmd measure_temp').readline()
print(temp)
temp= temp.replace("temp=","").replace("'C\n","")
print(temp)

while True:
    temp = os.popen('vcgencmd measure_temp').readline()
    print(temp)
    temp= temp.replace("temp=","").replace("'C\n","")
    print(temp)
    time.sleep(3)

Scanning for Bluetooth LE Devices

Use Bluetooth Low Energy to scan the environment around you with:

hcitool -i hci0 lescan

A different way of doing this is with Python. Get python-pip and a supporting library

sudo apt-get install python-pip libglib2.0-dev

Then you can run:

sudo pip install bluepy

You can then run:

sudo blescan
  • What is the difference between a -34dBm device and an -97dBm device which one is closer?