蓝牙功能
蓝牙是一种支持设备短距离通信的无线电技术,使用 2.4GHz 频段,数据速率为1Mbps 。采用时分复用方案实现全双工传输。
蓝牙技术将设备分为两种:主设备和从设备。
蓝牙主设备的特点:主设备一般具有输入端。在进行蓝牙匹配操作时,用户通过输入端可输入随机的匹配密码来将两个设备匹配。蓝牙手机、安装有蓝牙模块的 PC 等都是主设备。(例如:蓝牙手机和蓝牙 PC 进行匹配时,用户可在蓝牙手机上任意输入一组数字,然后在蓝牙PC上输入相同的一组数字,来完成这两个设备之间的匹配。)
蓝牙从设备特点:从设备一般不具备输入端。因此从设备在出厂时,在其蓝牙芯片中,固化有一个4位或6位数字的匹配密码。蓝牙耳机等都是从设备。
BlueZ 是 Linux 上的官方蓝牙协议栈,它提供了一套丰富的 API 来与蓝牙设备进行交互。HCI(Host Controller Interface)层是 BlueZ 协议栈中非常关键的一部分,它作为主机(Host)和蓝牙硬件(通常称为蓝牙dongle或模块)之间的接口。
BlueZ 包提供了蓝牙编程库和各种工具:
bccmd : is used to issue BlueCore commands to Cambridge Silicon Radio devices.
bluemoon : is a Bluemoon configuration utility.
bluetoothctl : is the interactive Bluetooth control program.
bluetoothd : is the Bluetooth daemon.
btmon : provides access to the Bluetooth subsystem monitor infrastructure for reading HCI traces.
ciptool : is used to set up, maintain, and inspect the CIP configuration of the Bluetooth subsystem in the Linux kernel.
hciattach : is used to attach a serial UART to the Bluetooth stack as HCI transport interface.
hciconfig : is used to configure Bluetooth devices.
hcidump : reads raw HCI data coming from and going to a Bluetooth device and prints to screen commands, events and data in a human-readable form.
hcitool : is used to configure Bluetooth connections and send some special command to Bluetooth devices.
hex2hcd : is used to convert a file needed by Broadcom devices to hcd (Broadcom bluetooth firmware) format.
l2ping : is used to send a L2CAP echo request to the Bluetooth MAC address given in dotted hex notation.
l2test : is L2CAP testing program.
rctest : is used to test RFCOMM communications on the Bluetooth stack.
rfcomm : is used to set up, maintain, and inspect the RFCOMM configuration of the Bluetooth subsystem in the Linux kernel.
sdptool : is used to perform SDP queries on Bluetooth devices.
libbluetooth.so : contains the BlueZ 4 API functions.
查看设备信息
# hciconfig -a
hci0: Type: Primary Bus: USB
BD Address: 88:49:2D:E5:0D:41 ACL MTU: 251:5 SCO MTU: 0:0
UP RUNNING
RX bytes:1704 acl:0 sco:0 events:113 errors:0
TX bytes:584 acl:0 sco:0 commands:76 errors:0
Features: 0x00 0x00 0x00 0x00 0x60 0x00 0x00 0x00
Packet type: DM1 DH1 HV1
Link policy:
Link mode: SLAVE ACCEPT
蓝牙设备连接流程
hciconfig hci0 up
systemctl start bluetooth
下面命令是在bluetoothctl下运行的
显示设备信息
[bluetooth]# show
Controller 88:49:2D:E5:0D:41 (public)
Name: hello
Alias: gw111
Class: 0x00000000
Powered: no
Discoverable: no
DiscoverableTimeout: 0x000000b4
Pairable: yes
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control (0000110e-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
Modalias: usb:v1D6Bp0246d0532
Discovering: no
打开电源
bluetooth]# power on
[HCC] enter hci_bt_open
plat_soc:W]bsle_open_close_cmd, service[1], type[1].
[HCC] start hcc_adapt_bsle_msg_rx_proc,type:2,device_msg:1
[HCC] start hcc_adapt_bsle_msg_rx_proc,type:4,device_msg:0
[HCC] Get bsle device action msg, type=[0]
[HCC] ble btc open finish
[HCC] ble open time:30
Changing power on succeeded
[CHG] Controller 88:49:2D:E5:0D:41 Powered: yes
设置可发现和配对模式
[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller 88:49:2D:E5:0D:41 Discoverable: yes
[bluetooth]# pairable on
Changing pairable on succeeded
[bluetooth]# show
Controller 88:49:2D:E5:0D:41 (public)
Name: hello
Alias: gw111
Class: 0x00000000
Powered: yes
Discoverable: yes
DiscoverableTimeout: 0x000000b4
Pairable: yes
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control (0000110e-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
Modalias: usb:v1D6Bp0246d0532
Discovering: no
扫描蓝牙设备
[bluetooth]# scan on
Discovery started
配对设备
[bluetooth]# pair XX:XX:XX:XX:XX:XX
信任设备
[bluetoothctl]# trust XX:XX:XX:XX:XX:XX
默认自动连接设备
[bluetoothctl]# default XX:XX:XX:XX:XX:XX
查看连接状态
[bluetoothctl]# devices
bluetoothctl命令
bluetoothctl 是 Linux 系统中用于控制蓝牙设备的一个交互式命令行工具。
以下是 bluetoothctl 的一些关键特性和用途:
设备发现:使用
bluetoothctl可以扫描并列出周围可见的蓝牙设备。设备配对:它可以与远程蓝牙设备进行配对,建立信任关系。
连接管理:
bluetoothctl可以用于创建和断开与远程设备的连接。设备信息获取:它可以获取并显示远程设备的详细信息,如名称、地址、类、服务等。
音频管理:用于管理蓝牙音频设备,如耳机和扬声器。
代理功能:
bluetoothctl可以作为代理来控制其他蓝牙应用程序。配置文件管理:它可以加载和卸载配置文件,以改变蓝牙设备的行为。
启动bluetoothctl。打开终端,输入以下命令启动 bluetoothctl:
bluetoothctl
电源管理,使用以下命令来开启或关闭蓝牙电源:
power on # 打开电源
power off # 关闭电源
设备发现,开始或停止扫描附近的蓝牙设备:
scan on # 开始扫描
scan off # 停止扫描
设备配对,显示已配对设备:
[LBQ]# paired-devices
Device C8:94:02:FA:48:86 LBQ
Device B4:AE:C1:8B:8F:C3 Arnold的iPhone
配对指定设备(替换 xx:xx:xx:xx:xx 为设备地址):
pair xx:xx:xx:xx:xx
连接指定设备:
connect xx:xx:xx:xx:xx
断开与指定设备的连接:
disconnect xx:xx:xx:xx:xx
设置设备为信任,以便自动连接:
trust xx:xx:xx:xx:xx
删除配对信息,停止自动连接:
remove xx:xx:xx:xx:xx
hcitool/hciconfig/hcitop/hcidump
hcitop
hcitop 是一个用于 Linux 的实时蓝牙 HCI (Host Controller Interface) 监控工具,显示与蓝牙 HCI 相关的各种统计信息和状态。
HCI FLAGS RX TX RX/s TX/s
hci0 U R 62 KB 4 KB 0 B 0 B
hcidump
hcidump 是一个在 Linux 系统中用于捕获和分析蓝牙 HCI (Host Controller Interface) 层数据包的工具。它能够显示蓝牙设备之间的通信细节,对于开发、调试以及分析蓝牙通信非常有用。
hcidump -t -X
HCI sniffer - Bluetooth packet analyzer ver 5.62
device: hci0 snap_len: 1500 filter: 0xffffffffffffffff
hciconfig
hciconfig 主要用于配置和查看蓝牙设备的 HCI (Host Controller Interface) 设置。这个工具是 BlueZ 蓝牙协议栈的一部分,BlueZ 是 Linux 的官方蓝牙协议栈。
以下是 hciconfig 的一些关键特性和用途:
查看配置:使用
hciconfig可以查看当前蓝牙设备的配置,包括设备名称、类、电源管理设置等。修改配置:
hciconfig可以用来修改蓝牙设备的运行时配置,例如开启或关闭电源管理,设置设备的类等。设备管理:它可以用于启动和停止蓝牙设备,以及设置设备的模式(如连接模式、发现模式等)。
接口设置:
hciconfig可以设置蓝牙设备的接口,例如更改设备的 MAC 地址或设置接口的 UP/DOWN 状态。连接管理:虽然
hciconfig主要用于配置设备,但它也可以用于查看当前的连接状态。
hcitool
hcitool 用于配置蓝牙设备并与之交互。它是 BlueZ 蓝牙协议栈的一部分,BlueZ 是 Linux 的官方蓝牙协议栈。
以下是 hcitool 的一些关键特性和用途:
设备发现:
hcitool可以扫描并列出周围可见的蓝牙设备。设备信息获取:它能够获取远程蓝牙设备的名称和信息。
连接管理:
hcitool可以用来创建和断开与远程设备的连接。数据传输:它可以发送数据到蓝牙设备,或从蓝牙设备接收数据。
配置HCI设备:
hcitool能够配置 HCI 设备的参数,比如设置本地名称、类、PIN 码等。低功耗扫描:支持对低功耗蓝牙设备(BLE)进行扫描。
显示本地HCI设备:
hcitool dev
Devices:
hci0 14:F5:F9:F9:B1:86
查找蓝牙设备:
hcitool scan
Scanning ...
C8:94:02:FA:48:86 LBQ
A8:93:4A:0B:DF:3A n/a
常见问题
修改蓝牙设备名称
有两种方法,一种是system-alias重命名
systemctl start bluetooth
bluetoothctl
systemctl-alias "mydev"
第二种是写入配置文件
vi /etc/machine-info
PRETTY_HOSTNAME="mydev"
如果没有这个文件,就创建文件,并写入内容。
最后编辑:SteveChen 更新时间:2025-11-20 00:20