文章目录
bluetoothctl 命令工作在 应用层(Application Layer),具体来说:
在蓝牙协议栈中的位置
应用层 (Application Layer) ← bluetoothctl 工作在这一层
↓
BlueZ D-Bus API
↓
蓝牙协议栈核心 (BlueZ Core)
↓
HCI 层 (Host Controller Interface)
↓
蓝牙控制器硬件
具体说明
-
应用层工具:
bluetoothctl是 BlueZ 蓝牙协议栈提供的用户空间命令行工具- 它通过 D-Bus 与蓝牙守护进程 (
bluetoothd) 通信
-
不直接处理协议:
- 不直接处理 L2CAP、RFCOMM 等传输层协议
- 不处理 HCI 命令的直接发送
- 不处理基带级别的蓝牙数据包
-
管理功能:
- 设备发现、配对、连接管理
- 控制器配置(如可发现性、可配对性)
- 服务浏览和设备信息查询
对比其他工具
- hcitool:工作在网络层,更接近 HCI
- sdptool:工作在服务发现协议层
- rfcomm:工作在 RFCOMM 串口仿真层
- bluetoothctl:最高层的管理工具,提供用户友好的设备管理界面
所以 bluetoothctl 是面向最终用户和系统管理员的高级管理工具,而非底层协议调试工具。
常见用法
1. 启动与基本结构
# 进入交互模式
bluetoothctl
# 执行单条命令后退出
bluetoothctl -- help
bluetoothctl show
交互模式提示符:
[bluetooth]#
[DEVICE_MAC_ADDRESS]#
2. 设备发现与扫描
# 开启扫描
[bluetooth]# scan on
# 关闭扫描
[bluetooth]# scan off
# 查看发现的设备
[bluetooth]# devices
# 获取设备信息
[bluetooth]# info [MAC地址]
3. 设备配对与连接
# 配对设备
[bluetooth]# pair [MAC地址]
# 信任设备(自动连接)
[bluetooth]# trust [MAC地址]
# 取消信任
[bluetooth]# untrust [MAC地址]
# 连接设备
[bluetooth]# connect [MAC地址]
# 断开连接
[bluetooth]# disconnect [MAC地址]
# 移除设备
[bluetooth]# remove [MAC地址]
4. 控制器管理(适配器设置)
# 显示控制器信息
[bluetooth]# show
# 选择控制器(多适配器时)
[bluetooth]# select [控制器MAC]
# 设置设备名称
[bluetooth]# system-alias "My Bluetooth"
# 设置可被发现
[bluetooth]# discoverable on
# 设置可被发现时长
[bluetooth]# discoverable-timeout 180
# 设置可配对
[bluetooth]# pairable on
# 电源控制
[bluetooth]# power on/off
- show查看控制器信息
[bluetooth]# show
Controller 10:A5:62:0B:E6:EB (public)
Name: BlueZ 5.64
Alias: Mower_X4_187358
Class: 0x00000000
Powered: yes
Discoverable: yes
DiscoverableTimeout: 0x000000b4
Pairable: yes
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: Vendor specific (6e400001-b5a3-f393-e0a9-e50e24dcca91)
UUID: Device Information (0000180a-0000-1000-8000-00805f9b34fb)
Modalias: usb:v1D6Bp0246d0540
Discovering: no
Roles: central
Roles: peripheral
Advertising Features:
ActiveInstances: 0x01 (1)
SupportedInstances: 0x04 (4)
SupportedIncludes: appearance
SupportedIncludes: local-name
解读下,这是一个蓝牙控制器的信息显示,我来为您解读各个参数的含义:
-
基本设备信息
- MAC地址: 10:A5:62:0B:E6:EB (公开地址)
- 名称: BlueZ 5.64 (这是Linux蓝牙协议栈的名称)
- 别名: Mower_X4_187358 (设备显示名称,看起来像割草机设备)
-
状态配置
- 电源状态: 已开启 ✓
- 可被发现: 是 ✓ (持续180秒)
- 可配对: 是 ✓
- 正在扫描: 否
-
支持的协议服务(UUID)
- Generic Access Profile - 通用访问配置
- Generic Attribute Profile - 通用属性配置
- Device Information - 设备信息服务
- PnP Information - 即插即用信息
- Vendor specific - 厂商特定服务 (6e400001…)
-
角色能力
- Central - 可以作为主设备连接其他设备
- Peripheral - 可以作为从设备被连接
-
广播功能
- 当前实例: 1个活跃广播
- 支持实例: 最多4个同时广播
- 支持包含: 外观和本地名称信息
5. 代理与配对授权
# 注册默认代理(处理PIN码等)
[bluetooth]# agent on
# 设置默认代理
[bluetooth]# default-agent
# 取消代理
[bluetooth]# agent off
6. 常用命令组合示例
连接蓝牙耳机:
bluetoothctl
scan on
# 等待发现设备后...
devices
pair 11:22:33:44:55:66
trust 11:22:33:44:55:66
connect 11:22:33:44:55:66
exit
一键连接已知设备:
bluetoothctl connect 11:22:33:44:55:66
配置适配器:
bluetoothctl power on
bluetoothctl discoverable on
bluetoothctl pairable on
7. 实用技巧
在脚本中使用:
#!/bin/bash
echo "power on" | bluetoothctl
echo "connect 11:22:33:44:55:66" | bluetoothctl
监控事件:
bluetoothctl
# 然后可以实时看到设备连接、断开等事件
获取帮助:
[bluetooth]# help
[bluetooth]# help [命令名]
8. 退出
[bluetooth]# exit
# 或 Ctrl+D
这个工具提供了完整的蓝牙设备管理功能,从基本的开关控制到复杂的设备配对和连接管理。
3447

被折叠的 条评论
为什么被折叠?



