问题描述
如果我想实现信标并向蓝牙设备发送消息, 我需要吗
- 树莓派 + 信标设备
- 树莓派
哪个是对的? 我发现有的人用树莓派和小信标设备,有的人只用树莓派本身发送消息,两者都可以发送URL或数据,我想知道树莓派在这个实现中的特点。
谢谢!!
解决方法
蓝牙 BLE 设备可以通告其服务,并且作为通告的一部分,最多可以包含 28 字节的广告数据。蓝牙规范中没有定义该数据的格式,因此由制造商定义以便蓝牙信号的两端都能理解它。这是广告数据,信标是如何创建的,公司已经定义了它们使用的结构。例如 iBeacon 和 Eddystone 格式。
这条推文很好地总结了流行/常见格式使用的数据结构: https://twitter.com/josryke/status/763006284052463617
在 Raspberry Pi 上,蓝牙堆栈是 BlueZ,它具有 API,允许您使用您喜欢的编程语言创建所需的广告: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/advertising-api.txt
还有一些命令行工具可让您手动创建信标。
使用 bluetoothctl
创建 Eddystone URL 信标的脚本:
(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]#
或使用 btmgmt
工具相同的广告:
pi@raspberrypi:~ $ sudo btmgmt add-adv -u feaa -d 1016aafe100001626c7565746f6f746800 -g 1
使用 btmgmt
工具的 iBeacon 示例:
sudo btmgmt add-adv -d 1AFF4C000215000102030405060708090A0B0C0D0E0F1234567800 -g 1