繁体   English   中英

如何使用 BlueZ 和 Python 创建 EddyStone Beacon?

[英]How to create EddyStone Beacon using BlueZ and Python?

我正在嵌入式 Linux 板上开发 BLE(低功耗蓝牙)。 我们使用 BlueZ 和 Python。 我需要创建 EddyStone Beacon。 我发现有一种创建 iBeacon 的方法: https ://scribles.net/creating-ibeacon-using-bluez-example-code-on-raspberry-pi/。 我尝试过这个。 有效。 但是我们需要创建 EddyStone Beacon。 所以我使用这里的 Beacon 数据格式( https://ukbaz.github.io/howto/beacon_scan_cmd_line.html )来创建制造商数据。 但是我的代码不起作用。 我的代码有什么问题? 这是我的代码:

def __init__(self, bus, index):
    eddystone_id = 0xAAFE
    beacon_type = [0x14, 0x16]  # Length = 0x14, EddyStone type = 0x16
    uuid = [0xAA, 0xFE] # EddyStone UUID = 0xAAFE
    frame_type = [0x10] # Frame Type = 0x10
    power = [0x00]      # Power = 0x00
    prefix = [0x02]     # URL scheme = 0x02 (http://)
    url = [0x73, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x77, 0x65, 0x62, 0x73, 0x69, 0x74, 0x65, 0x07]

    Advertisement.__init__(self, bus, index, 'peripheral')
    self.add_manufacturer_data(eddystone_id, beacon_type + uuid + frame_type + power + prefix + url)
    

但是,如果我使用此命令,则会创建 EddyStone Beacon。 我可以看到它在 nRF 移动应用程序中显示了 EddyStone Beacon:

sudo hcitool -i hci0 cmd 0x08 0x0008 1c 02 01 06 03 03 aa fe 14 16 aa fe 10 00 02 73 61 6d 70 6c 65 77 65 62 73 69 74 65 07 00 00 00

可以看到,我在add_manufacturer_data()函数中放入的数据和命令中的数据是一样的。 但是为什么 Python 代码不起作用呢?

iBeacon 使用manufacturer_data而 Eddystone 信标使用service_data所以我希望您的代码看起来更像这样:

    def __init__(self, bus, index):
        Advertisement.__init__(self, bus, index, 'broadcast')
        self.add_service_uuid('FEAA')
        frame_type = [0x10] # Eddystone frame Type = 0x10
        power = [0x00]      # Beacon broadcast power = 0x00
        prefix = [0x02]     # URL scheme = 0x02 (http://)
        url = [0x73, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x77, 0x65, 0x62, 0x73, 0x69, 0x74, 0x65, 0x07]
        eddystone_data = frame_type + power + prefix + url
        self.add_service_data('FEAA', eddystone_data)

附带说明一下, hcitool是 BlueZ 开发人员不赞成使用的工具之一。 当前支持的从命令行创建 Eddystone 信标的方法是使用bluetoothctl 命令的顺序是:

bluetoothctl 
menu advertise
uuids 0xFEAA
service 0xFEAA 0x10 0x00 0x02 0x73 0x61 0x6D 0x70 0x6C 0x65 0x77 0x65 0x62 0x73 0x69 0x74 0x65 0x07
back
advertise broadcast
discoverable on

我将广告类型peripheral更改为broadcast因为通常人们不希望信标可连接,但这取决于您的应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM