1

I am using a pi as a beacon and I have implemented EddyStone beacon URL. I can broadcast short URLs with no issue but now I want to broadcast a simple message which is stored as a string in a text file.

I would like to read the text file and broadcast it when the beacon is on. Can anyone help me with this? thanks in advance. Here's my code:

import os

on_bluetooth = os.system("sudo hciconfig hci0 up")   # bluetooth on
discoverable_on = os.system("sudo hciconfig hci0 leadv 3")   # discoverable on but not connectable

cond = True
while cond:
    try:
        fh = open('/home/Desktop/message.txt')
        cond = False
    
    except:
        print('File does not exist.')
        continue
    
    for line in fh:
        line = line.rstrip()
        text = line
    
    print("The message is:", text)

message = os.system("sudo hcitool -i hci0 cmd 0x08 0x0008 <insert text variable here>")   # broadcast message

1 Answer 1

3

I find Jos Ryke's Beacon Cheat Sheet that he posted on Twitter very helpful to understand what is going on:

https://twitter.com/josryke/status/763006284052463617

As you will see there is only a maximum of 18 bytes for the URL. Even with the bit of encoding they do in the Eddystone format, it is still not a very long url. A common way to get around this is to use a url shortener.

As a side note, hciconfig and hcitools are part of the list of tools that have been deprecated by the BlueZ project

To do a beacon on the command line you can use either bluetoothctl:

(venv) pi@raspberrypi:~ $ bluetoothctl 
Agent registered
[bluetooth]# menu advertise 
[bluetooth]# uuids feaa
[bluetooth]# service 0xfeaa 0x10 0x00 0x00 0x62 0x6c 0x75 0x65 0x74 0x6f 0x6f 0x74 0x68 0x00
[bluetooth]# back
[bluetooth]# advertise on
[CHG] Controller B8:27:EB:22:57:E0 SupportedInstances: 0x04
[CHG] Controller B8:27:EB:22:57:E0 ActiveInstances: 0x01
Advertising object registered
UUID: (feaa)
UUID: Google(0xfeaa)
  10 00 00 62 6c 75 65 74 6f 6f 74 68 00           ...bluetooth.   
Tx Power: off
Name: off
Appearance: off
Discoverable: off
[bluetooth]# 

Or btmgmt:

pi@raspberrypi:~ $ sudo btmgmt add-adv -u feaa -d 1016aafe100001626c7565746f6f746800 -g 1

As you are doing this in a Python script, there is an API available to do it without system calls.

There is a question that already covers this: https://stackoverflow.com/questions/63625654/how-to-create-eddystone-beacon-using-bluez-and-python/63627007#63627007

They do it modifying the TestAdvertisement class in the example advertisement in the BlueZ source code: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/example-advertisement

4
  • I tried the test advertisement code on raspberry pi but i can't find it with a beacon scanner app on my phone. Could u tell me how to use it ?
    – snow
    Commented Sep 17, 2020 at 7:42
  • I've used bluetoothctl before but I'm not sure how to include the bluetoothctl commands in my python file and execute it as a service upon boot?
    – snow
    Commented Sep 17, 2020 at 7:56
  • also i keep receiving this error [bluetooth]# advertise broadcast Failed to register advertisement: org.bluez.Error.Failed
    – snow
    Commented Sep 17, 2020 at 8:12
  • 1
    There is a way to do it in python explained in the answer: As you are doing this in a Python script, there is an API available to do it without system calls. Commented Sep 17, 2020 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.