如何使用BlueZ和Python创建EddyStone信标?

2024-09-26 18:14:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在嵌入式Linux板上开发BLE(蓝牙低能)。我们使用BlueZ和Python。我需要创建EddyStone灯塔。我发现有一种方法可以创建iBeacon:https://scribles.net/creating-ibeacon-using-bluez-example-code-on-raspberry-pi/。我试过了。成功了。 但我们需要创造埃迪斯通灯塔。因此,我在这里使用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信标。我可以看到它在nRF移动应用程序中显示EddyStone信标:

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代码不起作用呢


Tags: 数据代码httpsselfcmdidindexuuid
1条回答
网友
1楼 · 发布于 2024-09-26 18:14:35

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

我将advertisement typeperipheral更改为broadcast,因为人们通常不希望信标是可连接的,但这取决于您的应用程序

相关问题 更多 >

    热门问题