LXR | KVM | PM | Time | Interrupt | Systems Performance | Bootup Optimization

Linux下基于USB的Bluetooth(RTL8723DU):RFKILL子系统、HCI/BlueZ、Bluetooth驱动、bluetoothd、bluetoothctl等

简单梳理RFKILL子系统、基于USB的Bluetooth驱动、Bluetooth守护进程bluetoothd、Bluetooth配置工具bluetoothctl/hcitool/hciconfig等。

1 RFKILL子系统

rfkill 是 Linux 系统中的一个工具和内核接口,用于启用或禁用无线设备,如Wi-Fi、蓝牙、UHF/VHF 等。它提供了一种机制来控制无线射频(RF)设备的状态,这些设备可能因为软件或硬件原因被阻止或解除阻止。

1.1 RFKILL子系统初始化

rfkill_init
  rfkill_update_global_state
  class_register--初测rfkill类,rfkill_class,创建rfkill设备时也创建一系列属性节点。
  misc_register--创建/dev/rfkill字符设备,操作函数集为rfkill_fops。
  rfkill_global_led_trigger_register

对/dev/rfkill文件操作会进入如下函数调用:

static const struct file_operations rfkill_fops = {
    .owner        = THIS_MODULE,
    .open        = rfkill_fop_open,
    .read        = rfkill_fop_read,
    .write        = rfkill_fop_write,
    .poll        = rfkill_fop_poll,
    .release    = rfkill_fop_release,
#ifdef CONFIG_RFKILL_INPUT
    .unlocked_ioctl    = rfkill_fop_ioctl,
    .compat_ioctl    = compat_ptr_ioctl,
#endif
    .llseek        = no_llseek,
};

#define RFKILL_NAME "rfkill"

static struct miscdevice rfkill_miscdev = {
    .fops    = &rfkill_fops,
    .name    = RFKILL_NAME,
    .minor    = RFKILL_MINOR,
};

rfkill类设备都会创建如下sysfs属性节点:

static struct class rfkill_class = {
    .name        = "rfkill",
    .dev_release    = rfkill_release,
    .dev_groups    = rfkill_dev_groups,
    .dev_uevent    = rfkill_dev_uevent,
    .pm        = RFKILL_PM_OPS,
};

static struct attribute *rfkill_dev_attrs[] = {
    &dev_attr_name.attr,
    &dev_attr_type.attr,--rfkill的设备类型。
    &dev_attr_index.attr,--序号。
    &dev_attr_persistent.attr,
    &dev_attr_state.attr,--读取状态:unblocked、soft blocked、hard blocked。
    &dev_attr_soft.attr,--soft block状态读取或者配置。
    &dev_attr_hard.attr,--hard block的状态读取或者配置。
    NULL,
};
ATTRIBUTE_GROUPS(rfkill_dev);

1.2 RFKILL数据结构和API

struct rfkill用于表示可以被软件控制的无线射频设备的状态。

struct rfkill {
    spinlock_t        lock;

    enum rfkill_type    type;

    unsigned long        state;

    u32            idx;

    bool            registered;
    bool            persistent;
    bool            polling_paused;
    bool            suspended;

    const struct rfkill_ops    *ops;
    void            *data;

#ifdef CONFIG_RFKILL_LEDS
    struct led_trigger    led_trigger;
    const char        *ledtrigname;
#endif

    struct device        dev;
    struct list_head    node;

    struct delayed_work    poll_work;
    struct work_struct    uevent_work;
    struct work_struct    sync_work;
    char            name[];
};

rfkill_ops 是一个结构体,定义了一组操作函数,这些函数由 rfkill 子系统用来查询和改变无线射频设备的状态。

struct rfkill_ops {
    void    (*poll)(struct rfkill *rfkill, void *data);
    void    (*query)(struct rfkill *rfkill, void *data);--此函数用于查询设备当前是否被阻止。
    int    (*set_block)(void *data, bool blocked);--此函数用于阻止设备,或解除。当设备被阻止时,它不能进行无线通信。
};

分配和注册rfkill:

struct rfkill * __must_check rfkill_alloc(const char *name,
                      struct device *parent,
                      const enum rfkill_type type,
                      const struct rfkill_ops *ops,
                      void *ops_data);
int __must_check rfkill_register(struct rfkill *rfkill);
void rfkill_unregister(struct rfkill *rfkill);
void rfkill_destroy(struct rfkill *rfkill);

rfkill_alloc指定父设备名称、所属父设备、类型、rfkill_ops、ops数据,进而创建struct rfkill结构体:

rfkill_alloc
    device_initialize--初始化rfkill对应的设备,设备所属类型为rfkill_class。

rfkill_register用于在内核中注册一个新的 rfkill 类型设备。当一个设备驱动程序初始化并准备好被 rfkill 管理时,它会调用这个函数。

rfkill_register
  list_add_tail--将当前rfkill加入到rfkill_list。
  device_add--创建rfkill设备,通常是某一设备子设备以rfkillX命名。
  rfkill_led_trigger_register

1.2 rfkill使用

rfkill 是一个在Linux系统中用于启用或禁用无线设备(如Wi-Fi、蓝牙、RF等)的命令行工具。

使用 rfkill 命令不带任何参数可以列出所有无线设备及其当前状态:

rfkill list
0: bt_default: Bluetooth
        Soft blocked: yes
        Hard blocked: no
1: phy0: Wireless LAN
        Soft blocked: no
        Hard blocked: no
2: hci0: Bluetooth
        Soft blocked: no
        Hard blocked: no
使用 rfkill 启用指定的无线设备。设备可以按索引(index)或类型(如 wifibluetooth)加序号指定:
rfkill unblock <index> # 解锁指定索引的设备
rfkill unblock <type> # 解锁指定类型的所有设备

使用 rfkill 禁用指定的无线设备:

rfkill block <index> # 封锁指定索引的设备
rfkill block <type> # 封锁指定类型的所有设备

2 Bluetooth DTS

    wireless_bluetooth: wireless-bluetooth {
        compatible = "bluetooth-platdata";
        clocks = <&hym8563>;
        clock-names = "ext_clock";
        uart_rts_gpios = <&gpio4 RK_PC4 GPIO_ACTIVE_LOW>;
        pinctrl-names = "default", "rts_gpio";
        pinctrl-0 = <&uart9m0_rtsn>, <&bt_reset_gpio>, <&bt_wake_gpio>, <&bt_irq_gpio>;
        pinctrl-1 = <&uart9_gpios>;
        BT,reset_gpio    = <&gpio0 RK_PC6 GPIO_ACTIVE_HIGH>;
        BT,wake_gpio     = <&gpio0 RK_PC5 GPIO_ACTIVE_HIGH>;
        BT,wake_host_irq = <&gpio0 RK_PA0 GPIO_ACTIVE_HIGH>;
        status = "okay";
    };

3 Bluetooth驱动

BlueZ 是 Linux 上的官方蓝牙协议栈,它提供了一套丰富的 API 来与蓝牙设备进行交互。HCI(Host Controller Interface)层是 BlueZ 协议栈中非常关键的一部分,它作为主机(Host)和蓝牙硬件(通常称为蓝牙dongle或模块)之间的接口。

BlueZ 分为内核空间(Kernel Space)和用户空间(User Space)两部分。内核空间主要负责实现蓝牙的硬件协议层,包括 HCI 层和 L2CAP 层等。

  • 内核模块组成:
    • 主要包括蓝牙内核核心子系统、L2CAP 和 SCO 音频内核层、RFCOMM、BNEP、CMTP 和 HIDP 的内核实现。
      • 2CAP 层提供了逻辑链路控制和适配协议,是蓝牙协议栈中的一个重要组成部分。它支持数据的分割和重组,允许上层协议如 RFCOMM、SDP 等传输数据。
        • RFCOMM 是串行通信协议,允许蓝牙设备之间进行串行数据传输。
        • SDP 层用于发现蓝牙设备提供的服务。
    • HCI(Host Controller Interface)的驱动程序,支持 UART、USB、PCMCIA 以及虚拟设备。
      • HCI 是主机和蓝牙硬件之间的接口,负责传递命令和数据。
      • BlueZ 实现了 HCI 的硬件抽象层,提供了对多种蓝牙硬件的支持。
  • 用户空间组成:
    • bluetoothd 是 BlueZ 的中央守护进程,提供 D-Bus 接口,用于 UI 和其他子系统之间的通信。
    • BlueZ 提供了多种配置和测试程序,如 hcitool、btmon 等。

Bluetooth子系统初始化:

bt_init
  bt_selftest
  debugfs_create_dir--创建/sys/kernel/debug/bluetooth目录。
  bt_leds_init
  bt_sysfs_init--创建bluetooth类bt_class。
  sock_register
  hci_sock_init
    proto_register
    bt_sock_register
    bt_procfs_init
  l2cap_init
    l2cap_init_sockets
    hci_register_cb
    debugfs_create_file
  sco_init
  mgmt_init
    hci_mgmt_chan_register

Bluetooth sysfs初始化:

bt_sysfs_init
  class_create--创建bluetooth类。

Bluetooth设备驱动:

btusb_init
  rtk_btcoex_init
  usb_register--注册Bluetooth USB驱动btusb_driver。

 btusb_driver是USB外设驱动:

static struct usb_driver btusb_driver = {
    .name = "rtk_btusb",
    .probe = btusb_probe,
    .disconnect = btusb_disconnect,
#ifdef CONFIG_PM
    .suspend = btusb_suspend,
    .resume = btusb_resume,
#ifdef RTKBT_SWITCH_PATCH
    .reset_resume = btusb_resume,
#endif
#endif
    .id_table = btusb_table,
    .supports_autosuspend = 1,
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 1)
    .disable_hub_initiated_lpm = 1,
#endif
};

btusb_probe在ID匹配后被调用进行外设初始化:

btusb_probe
  hci_alloc_dev--分配HCI设备。
    hci_request_setup
    hci_init_sysfs--初始化HCI设备。
    discovery_init
  hci_register_dev
    device_add--创建hciX设备。
    rfkill_alloc--分配hci所属的rfkill设备。
    rfkill_register--注册rfkill设备到rfkill子系统。
  rtk_btcoex_probe

USB相关sysfs如下:

tree
.
|-- 2-1.1:1.0
|   |-- bluetooth--蓝牙设备。
|   |   `-- hci0
|   |       |-- device -> ../../../2-1.1:1.0
|   |       |-- hci0:3
|   |       |   |-- device -> ../../hci0
|   |       |-- rfkill2--hci子系统创建的rfkill设备。
|   |-- driver -> ../../../../../../../bus/usb/drivers/rtk_btusb
|   |-- ep_02
|   |   |-- bEndpointAddress
|   |   |-- bInterval
|   |   |-- bLength
|   |   |-- bmAttributes
|   |   |-- direction
|   |   |-- interval
|   |   |-- power
|   |   |-- type--Bulk类型。
|   |   |-- uevent
|   |   `-- wMaxPacketSize
|   |-- ep_81
|   |   |-- bEndpointAddress
|   |   |-- bInterval
|   |   |-- bLength
|   |   |-- bmAttributes
|   |   |-- direction
|   |   |-- interval
|   |   |-- power
|   |   |-- type--Interrupt类型。
|   |   |-- uevent
|   |   `-- wMaxPacketSize
|   |-- ep_82
|   |   |-- bEndpointAddress
|   |   |-- bInterval
|   |   |-- bLength
|   |   |-- bmAttributes
|   |   |-- direction
|   |   |-- interval
|   |   |-- power
|   |   |-- type--Bulk类型。
|   |   |-- uevent
|   |   `-- wMaxPacketSize
|-- 2-1.1:1.1
|   |-- ep_03
|   |   |-- bEndpointAddress
|   |   |-- bInterval
|   |   |-- bLength
|   |   |-- bmAttributes
|   |   |-- direction
|   |   |-- interval
|   |   |-- power
|   |   |-- type--Isoc类型。
|   |   |-- uevent
|   |   `-- wMaxPacketSize
|   |-- ep_83
|   |   |-- bEndpointAddress
|   |   |-- bInterval
|   |   |-- bLength
|   |   |-- bmAttributes
|   |   |-- direction
|   |   |-- interval
|   |   |-- power
|   |   |-- type--Isoc类型。
|   |   |-- uevent
|   |   `-- wMaxPacketSize
|-- 2-1.1:1.2
|-- ep_00
|   |-- bEndpointAddress
|   |-- bInterval
|   |-- bLength
|   |-- bmAttributes
|   |-- direction
|   |-- interval
|   |-- type--Control类型。
|   |-- uevent
|   `-- wMaxPacketSize

4 Bluetooth相关sysfs

HCI设备及其rfkill设备:

/sys/class/bluetooth/hci0
|-- device -> ../../../2-1.1:1.0
|-- power
|   |-- async
|   |-- autosuspend_delay_ms
|   |-- control
|   |-- runtime_active_kids
|   |-- runtime_active_time
|   |-- runtime_enabled
|   |-- runtime_status
|   |-- runtime_suspended_time
|   `-- runtime_usage
|-- rfkill2
|   |-- device -> ../../hci0
|   |-- hard
|   |-- index
|   |-- name
|   |-- persistent
|   |-- power
|   |-- soft
|   |-- state
|   |-- subsystem -> ../../../../../../../../../../class/rfkill
|   |-- type
|   `-- uevent
|-- subsystem -> ../../../../../../../../../class/bluetooth
`-- uevent

5 bluetoothd启动

bluetoothd 用于管理 Bluetooth 设备和协议栈。它是一个核心组件,负责处理 Bluetooth 硬件的低级细节,提供对各种 Bluetooth 配置文件(profiles)的支持,从而允许应用程序通过 Bluetooth 进行通信。

主要作用:

  1. 设备管理:bluetoothd 负责扫描、配对、连接和断开与 Bluetooth 设备的连接。
  2. 协议支持:支持多种 Bluetooth 协议,包括但不限于 A2DP、HFP、HID、PAN 等。
  3. 配置文件管理:管理不同的 Bluetooth 配置文件,这些配置文件定义了设备如何与其他 Bluetooth 设备交互。
  4. 服务发现:允许设备发现其他 Bluetooth 设备提供的服务。

/etc/init.d/S40bluetooth启动bluetoothd:

#!/bin/sh

DAEMON="bluetoothd"
PIDFILE="/var/run/$DAEMON.pid"

BLUETOOTHD_ARGS="-n"

# shellcheck source=/dev/null
[ -r "/etc/default/$DAEMON" ] && . "/etc/default/$DAEMON"

start() {
    printf 'Starting %s: ' "$DAEMON"
    # shellcheck disable=SC2086 # we need the word splitting
    start-stop-daemon -S -q -m -b -p "$PIDFILE" -x "/usr/libexec/bluetooth/$DAEMON" \
        -- $BLUETOOTHD_ARGS
    status=$?
    if [ "$status" -eq 0 ]; then
        echo "OK"
    else
        echo "FAIL"
    fi
    return "$status"
}

stop() {
    printf 'Stopping %s: ' "$DAEMON"
    start-stop-daemon -K -q -p "$PIDFILE"
    status=$?
    if [ "$status" -eq 0 ]; then
        echo "OK"
    else
        echo "FAIL"
    fi
    return "$status"
}

restart() {
    stop
    sleep 1
    start
}

reload() {
    printf 'Reloading %s: ' "$DAEMON"
    start-stop-daemon -K -s HUP -q -p "$PIDFILE"
    status=$?
    if [ "$status" -eq 0 ]; then
        echo "OK"
    else
        echo "FAIL"
    fi
    return "$status"
}

case "$1" in
    start|stop|restart|reload)
        "$1";;
    *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac

6 bluetoothctl

bluetoothctl 是 Linux 系统中用于控制蓝牙设备的一个交互式命令行工具。

以下是 bluetoothctl 的一些关键特性和用途:

  1. 设备发现:使用 bluetoothctl 可以扫描并列出周围可见的蓝牙设备。

  2. 设备配对:它可以与远程蓝牙设备进行配对,建立信任关系。

  3. 连接管理:bluetoothctl 可以用于创建和断开与远程设备的连接。

  4. 设备信息获取:它可以获取并显示远程设备的详细信息,如名称、地址、类、服务等。

  5. 音频管理:用于管理蓝牙音频设备,如耳机和扬声器。

  6. 代理功能:bluetoothctl 可以作为代理来控制其他蓝牙应用程序。

  7. 配置文件管理:它可以加载和卸载配置文件,以改变蓝牙设备的行为。

bluetoothctl ver 5.62
Usage:
        bluetoothctl [--options] [commands]
Options:
        --agent         Register agent handler: <capability>
        --monitor       Enable monitor output
        --timeout       Timeout in seconds for non-interactive mode
        --version       Display version
        --help          Display help
Commands:
        list            List available controllers
        show            Controller information
        select          Select default controller
        devices         List available devices
        paired-devices  List paired devices--显示已经配对的设备列表。
        system-alias    Set controller alias
        reset-alias     Reset controller alias
        power           Set controller power
        pairable        Set controller pairable mode
        discoverable    Set controller discoverable mode
        discoverable-timeout    Set discoverable timeout
        agent           Enable/disable agent with given capability
        default-agent   Set agent as the default one
        advertise       Enable/disable advertising with given type
        set-alias       Set device alias
        scan            Scan for devices
        info            Device information
        pair            Pair with device--进行配对。
        cancel-pairing  Cancel pairing with device
        trust           Trust device
        untrust         Untrust device
        block           Block device
        unblock         Unblock device
        remove          Remove device
        connect         Connect device
        disconnect      Disconnect device

        advertise.:
                uuids           Set/Get advertise uuids
                service         Set/Get advertise service data
                manufacturer    Set/Get advertise manufacturer data
                data            Set/Get advertise data
                discoverable    Set/Get advertise discoverable
                discoverable-timeout    Set/Get advertise discoverable timeout
                tx-power        Show/Enable/Disable TX power to be advertised
                name            Configure local name to be advertised
                appearance      Configure custom appearance to be advertised
                duration        Set/Get advertise duration
                timeout         Set/Get advertise timeout
                secondary       Set/Get advertise secondary channel
                interval        Set/Get advertise interval range
                clear           Clear advertise config

        monitor.:
                set-rssi-threshold      Set RSSI threshold parameter
                set-rssi-timeout        Set RSSI timeout parameter
                set-rssi-sampling-period        Set RSSI sampling period parameter
                add-or-pattern  Register 'or pattern' type monitor with the specified RSSI parameters
                get-pattern     Get advertisement monitor
                remove-pattern  Remove advertisement monitor
                get-supported-info      Get advertisement manager supported features and supported monitor types
                print-usage     Print the command usage

        scan.:
                uuids           Set/Get UUIDs filter
                rssi            Set/Get RSSI filter, and clears pathloss
                pathloss        Set/Get Pathloss filter, and clears RSSI
                transport       Set/Get transport filter
                duplicate-data  Set/Get duplicate data filter
                discoverable    Set/Get discoverable filter
                pattern         Set/Get pattern filter
                clear           Clears discovery filter.
...

启动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

7 hcitool/hciconfig/hcitop/hcidump

7.1 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

7.2 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

7.3 hciconfig

hciconfig 主要用于配置和查看蓝牙设备的 HCI (Host Controller Interface) 设置。这个工具是 BlueZ 蓝牙协议栈的一部分,BlueZ 是 Linux 的官方蓝牙协议栈。

以下是 hciconfig 的一些关键特性和用途:

  1. 查看配置:使用 hciconfig 可以查看当前蓝牙设备的配置,包括设备名称、类、电源管理设置等。

  2. 修改配置:hciconfig 可以用来修改蓝牙设备的运行时配置,例如开启或关闭电源管理,设置设备的类等。

  3. 设备管理:它可以用于启动和停止蓝牙设备,以及设置设备的模式(如连接模式、发现模式等)。

  4. 接口设置:hciconfig 可以设置蓝牙设备的接口,例如更改设备的 MAC 地址或设置接口的 UP/DOWN 状态。

  5. 连接管理:虽然 hciconfig 主要用于配置设备,但它也可以用于查看当前的连接状态。

hciconfig主要用法如下:

hciconfig - HCI device configuration utility
Usage:
        hciconfig
        hciconfig [-a] hciX [command ...]
Commands:
        up                      Open and initialize HCI device
        down                    Close HCI device
        reset                   Reset HCI device
        rstat                   Reset statistic counters
        auth                    Enable Authentication
        noauth                  Disable Authentication
        encrypt                 Enable Encryption
        noencrypt               Disable Encryption
        piscan                  Enable Page and Inquiry scan
        noscan                  Disable scan
        iscan                   Enable Inquiry scan
        pscan                   Enable Page scan
        ptype      [type]       Get/Set default packet type
        lm         [mode]       Get/Set default link mode
        lp         [policy]     Get/Set default link policy
        name       [name]       Get/Set local name
        class      [class]      Get/Set class of device
        voice      [voice]      Get/Set voice setting
        iac        [iac]        Get/Set inquiry access code
        inqtpl     [level]      Get/Set inquiry transmit power level
        inqmode    [mode]       Get/Set inquiry mode
        inqdata    [data]       Get/Set inquiry data
        inqtype    [type]       Get/Set inquiry scan type
        inqparms   [win:int]    Get/Set inquiry scan window and interval
        pageparms  [win:int]    Get/Set page scan window and interval
        pageto     [to]         Get/Set page timeout
        afhmode    [mode]       Get/Set AFH mode
        sspmode    [mode]       Get/Set Simple Pairing Mode
        aclmtu     <mtu:pkt>    Set ACL MTU and number of packets
        scomtu     <mtu:pkt>    Set SCO MTU and number of packets
        delkey     <bdaddr>     Delete link key from the device
        oobdata                 Get local OOB data
        commands                Display supported commands
        features                Display device features
        version                 Display version information
        revision                Display revision information
        block      <bdaddr>     Add a device to the reject list
        unblock    <bdaddr>     Remove a device from the reject list
        lerandaddr <bdaddr>     Set LE Random Address
        leadv      [type]       Enable LE advertising
                        0 - Connectable undirected advertising (default)
                        3 - Non connectable undirected advertising
        noleadv                 Disable LE advertising
        lestates                Display the supported LE states

打开并初始化HCI设备:

hci0:   Type: Primary  Bus: USB
        BD Address: 14:F5:F9:F9:B1:86  ACL MTU: 1021:8  SCO MTU: 255:12
        UP RUNNING PSCAN
        RX bytes:120942 acl:211 sco:0 events:1722 errors:0
        TX bytes:11873 acl:210 sco:0 commands:310 errors:0

获取本地设备名称:

hci0:   Type: Primary  Bus: USB
        BD Address: 14:F5:F9:F9:B1:86  ACL MTU: 1021:8  SCO MTU: 255:12
        Name: 'BlueZ 5.62'

7.4 hcitool

hcitool 用于配置蓝牙设备并与之交互。它是 BlueZ 蓝牙协议栈的一部分,BlueZ 是 Linux 的官方蓝牙协议栈。

以下是 hcitool 的一些关键特性和用途:

  1. 设备发现:hcitool 可以扫描并列出周围可见的蓝牙设备。

  2. 设备信息获取:它能够获取远程蓝牙设备的名称和信息。

  3. 连接管理:hcitool 可以用来创建和断开与远程设备的连接。

  4. 数据传输:它可以发送数据到蓝牙设备,或从蓝牙设备接收数据。

  5. 配置HCI设备:hcitool 能够配置 HCI 设备的参数,比如设置本地名称、类、PIN 码等。

  6. 低功耗扫描:支持对低功耗蓝牙设备(BLE)进行扫描。

hcitool用法如下:

hcitool - HCI Tool ver 5.62
Usage:
        hcitool [options] <command> [command parameters]
Options:
        --help  Display help
        -i dev  HCI device
Commands:
        dev     Display local devices
        inq     Inquire remote devices
        scan    Scan for remote devices--查找蓝牙设备。
        name    Get name from remote device
        info    Get information from remote device
        spinq   Start periodic inquiry
        epinq   Exit periodic inquiry
        cmd     Submit arbitrary HCI commands
        con     Display active connections
        cc      Create connection to remote device
        dc      Disconnect from remote device
        sr      Switch central/peripheral role
        cpt     Change connection packet type
        rssi    Display connection RSSI
        lq      Display link quality
        tpl     Display transmit power level
        afh     Display AFH channel map
        lp      Set/display link policy settings
        lst     Set/display link supervision timeout
        auth    Request authentication
        enc     Set connection encryption
        key     Change connection link key
        clkoff  Read clock offset
        clock   Read local or remote clock
        lescan  Start LE scan
        leinfo  Get LE remote information
        lealadd Add device to LE White List
        lealrm  Remove device from LE White List
        lealsz  Read size of LE White List
        lealclr Clear LE White List
        lewladd Deprecated. Use lealadd instead.
        lewlrm  Deprecated. Use lealrm instead.
        lewlsz  Deprecated. Use lealsz instead.
        lewlclr Deprecated. Use lealclr instead.
        lerladd Add device to LE Resolving List
        lerlrm  Remove device from LE Resolving List
        lerlclr Clear LE Resolving List
        lerlsz  Read size of LE Resolving List
        lerlon  Enable LE Address Resolution
        lerloff Disable LE Address Resolution
        lecc    Create a LE Connection
        ledc    Disconnect a LE Connection
        lecup   LE Connection Update

显示本地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

查看远程设备信息:

hcitool info C8:94:02:FA:48:86
Requesting information ...
        BD Address:  C8:94:02:FA:48:86
        Device Name: LBQ
        LMP Version: 5.2 (0xb) LMP Subversion: 0x2305
        Manufacturer: MediaTek, Inc. (70)
        Features page 0: 0xbf 0x3e 0x8d 0xfe 0xdb 0xff 0x7b 0x87
                <3-slot packets> <5-slot packets> <encryption> <slot offset>
                <timing accuracy> <role switch> <sniff mode> <RSSI>
                <channel quality> <SCO link> <HV2 packets> <HV3 packets>
                <CVSD> <power control> <transparent SCO> <broadcast encrypt>
                <EDR ACL 2 Mbps> <EDR ACL 3 Mbps> <enhanced iscan>
                <interlaced iscan> <interlaced pscan> <inquiry with RSSI>
                <extended SCO> <EV4 packets> <EV5 packets> <AFH cap. perip.>
                <AFH cls. perip.> <LE support> <3-slot EDR ACL>
                <5-slot EDR ACL> <sniff subrating> <pause encryption>
                <AFH cap. central> <AFH cls. central> <EDR eSCO 2 Mbps>
                <EDR eSCO 3 Mbps> <3-slot EDR eSCO> <extended inquiry>
                <LE and BR/EDR> <simple pairing> <encapsulated PDU>
                <err. data report> <non-flush flag> <LSTO> <inquiry TX power>
                <EPC> <extended features>

 

posted on 2024-06-21 23:59  ArnoldLu  阅读(3087)  评论(0)    收藏  举报

导航