Linux 使用 BlueZ Stack 作为外设(广告商)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16151360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use BlueZ Stack As A Peripheral (Advertiser)
提问by Ceryni
Goal: Use BlueZ and an Bluetooth 4LE dongle to create a peripheral that advertises the bluetooth equivalent of "Hello World".
Where I'm At: I've currently got the BlueZ stack setup and downloaded, I can use the hci tool to recognize and see the Bluetooth dongle. I've tinkered with hciconfig leadv but I'm just not quite getting it/understanding what's going on.
What Help I Think I Need: I need to get it to the next step. If anyone can either point me towards a good resource, walk me through this, or anything, It would be much appreciated. If I need to do additional leg(search)work I can but I've scoured Google and SO with as many different derivatives of this question as I can think of.
目标:使用 BlueZ 和蓝牙 4LE 加密狗创建一个外围设备,用于宣传“Hello World”的蓝牙等效项。
我在哪里:我目前已经安装并下载了 BlueZ 堆栈,我可以使用 hci 工具来识别和查看蓝牙加密狗。我已经对 hciconfig Leadv 进行了修改,但我只是不太明白/理解发生了什么。
我认为我需要什么帮助:我需要让它进入下一步。如果有人可以将我指向一个好的资源,引导我完成这个或任何事情,我将不胜感激。如果我需要做额外的腿部(搜索)工作,我可以,但我已经用我能想到的这个问题的尽可能多的不同衍生品搜索了谷歌和 SO。
*I tagged this as CoreBluetooth as well in hopes that maybe an iOS dev has tinkered with this at some point.
*我也将其标记为 CoreBluetooth,希望 iOS 开发人员在某个时候对此进行了修补。
EDIT: In response to a comment, It seems prudent to state what my end goal is. I'd ultimately like to advertise via the dongle the simplest of simple messages/signals and pick that up on an iOS device (CoreBluetooth). I've been able to get the iOS side of things working well (tons of documentation compared to the Linux side of things) but that hard part for me is getting this adapter setup as a peripheral. The BlueZ stack is a terrible enigma for me.
编辑:为了回应评论,陈述我的最终目标似乎是谨慎的。我最终想通过加密狗最简单的简单消息/信号做广告,然后在 iOS 设备(CoreBluetooth)上接收它。我已经能够让 iOS 方面运行良好(与 Linux 方面相比有大量文档),但对我来说最困难的部分是将此适配器设置为外围设备。BlueZ 堆栈对我来说是一个可怕的谜。
EDIT: After more digging, I eventually stumbled upon this post: Raspberry Pi Bluetooth 4.0 Connection. This has led me toward the topic of a GATT server, I'll continue pursuing this topic.
编辑:经过更多的挖掘,我最终偶然发现了这篇文章:Raspberry Pi Bluetooth 4.0 Connection。这让我转向了 GATT 服务器的话题,我将继续探讨这个话题。
EDIT: Alright so my quest for learning goes on. Over the past couple of days I've dived deeper into the hci*, sdptool, and gatttool tools. I've gotten to the point of being able to set the adapter to advertise, "hciconfig hci0 leadv". At this point, I can successfully "see" the adapter, but I cannot actually read anything off of it. I'm not even seeing the friendly name. I'll keep trucking on but as always, any help/suggestions are more than welcome.
编辑:好的,所以我对学习的追求仍在继续。在过去的几天里,我深入研究了 hci*、sdptool 和 gatttool 工具。我已经到了能够将适配器设置为广告“hciconfig hci0 Leadv”的地步。在这一点上,我可以成功地“看到”适配器,但实际上我无法从中读取任何内容。我什至没有看到友好的名字。我会继续努力,但一如既往,我们非常欢迎任何帮助/建议。
EDIT: Relevant Link, solid overview of Bluetooth LE pertaining to iOS. https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/AboutCoreBluetooth/Introduction.html#//apple_ref/doc/uid/TP40013257-CH1-SW1
编辑:相关链接,与 iOS 相关的蓝牙 LE 的完整概述。 https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/AboutCoreBluetooth/Introduction.html#//apple_ref/doc/uid/TP40013257-CH1-SW1
LAST EDIT: Hey all, this link covers how to create an iBeacon using a Raspberry Pi + BlueZ... http://www.wadewegner.com/2014/05/create-an-ibeacon-transmitter-with-the-raspberry-pi/
最后编辑:大家好,此链接介绍了如何使用 Raspberry Pi + BlueZ 创建 iBeacon... http://www.wadewegner.com/2014/05/create-an-ibeacon-transmitter-with-the-raspberry -pi/
采纳答案by davidgyoung
With your Bluetooth dongle plugged in, running the following command will tell you the device name and give its state:
插入蓝牙适配器后,运行以下命令将告诉您设备名称并给出其状态:
$ hciconfig
$ hciconfig
The output should look something like this:
输出应如下所示:
hci0: Type: BR/EDR Bus: USB
BD Address: 00:01:02:aa:bb:cc ACL MTU: 1021:8 SCO MTU: 64:1
DOWN
RX bytes:1000 acl:0 sco:0 events:47 errors:0
TX bytes:1072 acl:0 sco:0 commands:47 errors:0
This indicates the device is called hci0 is in a down state. Issue the following command to bring it up:
这表明名为 hci0 的设备处于关闭状态。发出以下命令以启动它:
$ sudo hciconfig hci0 up
$ sudo hciconfig hci0 up
Now it should look like:
现在它应该看起来像:
$ hciconfig
hci0: Type: BR/EDR Bus: USB
BD Address: 00:01:02:aa:bb:cc ACL MTU: 1021:8 SCO MTU: 64:1
UP RUNNING
RX bytes:1000 acl:0 sco:0 events:47 errors:0
TX bytes:1072 acl:0 sco:0 commands:47 errors:0
Next, execute the following example command to configure the advertising data to be sent.
接下来,执行以下示例命令来配置要发送的广告数据。
$ sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 00 00 00 00 c5 00 00 00 00 00 00 00 00 00 00 00 00 00
You can change the hex bytes (starting with 1e) to send different byte sequences for your advertisement. One that literally sends the ASCII codes for "HELLO WORLD" would use: 48 45 4c 4c 4f 57 4f 52 4c 44
(EDIT:But you will also have to prefix this message with a valid header, see here.)
您可以更改十六进制字节(以 1e 开头)为您的广告发送不同的字节序列。一,从字面上发送ASCII码为“HELLO WORLD”将使用:48 45 4c 4c 4f 57 4f 52 4c 44
(编辑:但你也必须前缀一个有效的头这条消息,请参见这里)
Now, use the following command to activate advertising on the dongle, this will start sending "Helo World" packets.
现在,使用以下命令激活加密狗上的广告,这将开始发送“Helo World”数据包。
$ sudo hciconfig hci0 leadv 0
$ sudo hciconfig hci0 leadv 0
EDIT:the above command makes the advertised service connectable. If you don't want to allow connections, change it to $ sudo hciconfig hci0 leadv 3
编辑:上述命令使广告服务可连接。如果您不想允许连接,请将其更改为$ sudo hciconfig hci0 leadv 3
You can also disable advertising using the following command:
您还可以使用以下命令禁用广告:
$ sudo hciconfig hci0 noleadv
$ sudo hciconfig hci0 noleadv
回答by user3891941
adding some more information here (ref BlueZ5.x):
在此处添加更多信息(参考 BlueZ5.x):
bluez is not exposing DBUS api for gatt server registeration; you have some profile implementation available but you need to configure and compile it with --enable-experimental option or enable gatt-example under plugin folder tha tregister some gatt server as example
when you advertise doing hciconfig hci0 leadv pay attention to advertised data. in the example below for iBeacon
sudo hcitool -i hci0 cmd 0x08 0x0008 1e **02 01 1a** ff 4c 00 02 15 e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 00 00 00 00 c5 00 00 00 00 00 00 00 00 00 00 00 00 00
the bolded part is for an AD structure that is releated to flags field (check under bluetooth spec for advertisgin data format). With this flags settings the device is advertising itself as simultaneous LE and BR-EDR.
In my experience when advertising in this way an android device that scan the bluez device acting as BLE peripheral will trigger a connection over Classic bluetooth (due to flag settings) and not over BLE (note that connection procedures are different for LE and Classic). Not sure how Apple central device act in this case. Anyway to avoid it you may set 06 instead of 1A so that BR-EDR support isnot advertised. In this way you will see a connection over BLE.
bluez 没有为 gatt 服务器注册公开 DBUS api;您有一些可用的配置文件实现,但您需要使用 --enable-experimental 选项配置和编译它,或者在插件文件夹下启用 gatt-example 以注册一些 gatt 服务器作为示例
当你做广告做 hciconfig hci0 Leadv 注意广告数据。在下面的 iBeacon 示例中
sudo hcitool -i hci0 cmd 0x08 0x0008 1e **02 01 1a** ff 4c 00 02 15 e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 00 00 00 00 c5 00 00 00 00 00 00 00 00 00 00 00 00 00
粗体部分用于与标志字段相关的 AD 结构(在蓝牙规范下检查广告数据格式)。使用此标志设置,设备将自己宣传为同时 LE 和 BR-EDR。
根据我的经验,当以这种方式做广告时,扫描作为 BLE 外围设备的 bluez 设备的 android 设备将触发通过经典蓝牙(由于标志设置)而不是通过 BLE 的连接(请注意,LE 和经典的连接程序不同)。不确定 Apple 中央设备在这种情况下如何运作。无论如何,为了避免它,您可以设置 06 而不是 1A,这样就不会宣传 BR-EDR 支持。通过这种方式,您将看到通过 BLE 的连接。
If you want to test it using a central Android device there is a free Nordic app for that.
如果您想使用中央 Android 设备对其进行测试,则可以使用免费的 Nordic 应用程序。