简体   繁体   English

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

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

I'm working on BLE(BlueTooth Low Energy) on an Embedded Linux board.我正在嵌入式 Linux 板上开发 BLE(低功耗蓝牙)。 We use BlueZ and Python.我们使用 BlueZ 和 Python。 I need to create EddyStone Beacon.我需要创建 EddyStone Beacon。 I found there is a way to create iBeacon: https://scribles.net/creating-ibeacon-using-bluez-example-code-on-raspberry-pi/ .我发现有一种创建 iBeacon 的方法: https ://scribles.net/creating-ibeacon-using-bluez-example-code-on-raspberry-pi/。 I tried it.我尝试过这个。 It worked.有效。 But we need to create EddyStone Beacon.但是我们需要创建 EddyStone Beacon。 So I use the Beacon data format from here( https://ukbaz.github.io/howto/beacon_scan_cmd_line.html ) to create the manufacturer data.所以我使用这里的 Beacon 数据格式( https://ukbaz.github.io/howto/beacon_scan_cmd_line.html )来创建制造商数据。 But my code doesn't work.但是我的代码不起作用。 What is wrong with my code?我的代码有什么问题? Here is my code:这是我的代码:

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)
    

However, if I use this command, the EddyStone Beacon is created.但是,如果我使用此命令,则会创建 EddyStone Beacon。 I can see it shows EddyStone Beacon in nRF mobile app:我可以看到它在 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

As you can see, the data I put in the add_manufacturer_data() function is the same as the data in the command.可以看到,我在add_manufacturer_data()函数中放入的数据和命令中的数据是一样的。 But why the Python code doesn't work?但是为什么 Python 代码不起作用呢?

iBeacon uses manufacturer_data while Eddystone beacons use service_data so I would expect your code to look more like this: 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)

As a side note, hcitool is one of the tools that has been deprecated by the BlueZ developers.附带说明一下, hcitool是 BlueZ 开发人员不赞成使用的工具之一。 The currently supported way to create the Eddystone beacon from the command line would be with bluetoothctl .当前支持的从命令行创建 Eddystone 信标的方法是使用bluetoothctl The sequence of commands would be:命令的顺序是:

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

I changed the advertisement type from peripheral to broadcast because typically people don't want beacons to be connectable, but it depends on your application.我将广告类型peripheral更改为broadcast因为通常人们不希望信标可连接,但这取决于您的应用程序。

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

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