[RaspberryPi] Bluetooth?*

樹(shù)莓派官方發(fā)布的最新的操作系統(tǒng)中,都繼承了BlueZ藍(lán)牙模組庫(kù),所以只要樹(shù)莓派上有藍(lán)牙適配器(不管是自帶的還是外接的),都可以使用藍(lán)牙功能。
其他操作系統(tǒng)我們這里不討論,因?yàn)槔鏦indows 10 IOT和Android things他們封裝的藍(lán)牙模組庫(kù)的用法可能會(huì)完全不同,所以我們討論起來(lái)就天長(zhǎng)地久白頭偕老了~

BlueZ


我們就先看看在樹(shù)莓派的Linux下的終端(命令行)里怎么操作藍(lán)牙模組庫(kù)BlueZ。

  • 獲取BlueZ版本
    bluetoothd -v
  • 查看BlueZ運(yùn)行狀況
    systemctl status bluetooth
  • 查看藍(lán)牙適配器信息
    hciconfig -a
  • 啟動(dòng)藍(lán)牙服務(wù)
    systemctl start bluetooth
  • 關(guān)閉藍(lán)牙服務(wù)
    systemctl stop bluetooth
  • 啟用藍(lán)牙服務(wù)
    systemctl enable bluetooth
  • 禁用藍(lán)牙服務(wù)
    systemctl disable bluetooth
  • 進(jìn)入藍(lán)牙控制臺(tái)
    bluetoothctl
  • 藍(lán)牙控制臺(tái)列出藍(lán)牙適配器
    list
  • 藍(lán)牙控制臺(tái)顯示藍(lán)牙適配器的信息
    show [ctrl]
  • 藍(lán)牙控制臺(tái)設(shè)置默認(rèn)的藍(lán)牙適配器
    select <ctrl>
  • 藍(lán)牙控制臺(tái)列出可連接的藍(lán)牙設(shè)備
    devices
  • 藍(lán)牙控制臺(tái)列出已配對(duì)的藍(lán)牙設(shè)備
    paired-devices
  • 藍(lán)牙控制臺(tái)設(shè)置藍(lán)牙適配器電源
    power <on/off>
  • 藍(lán)牙控制臺(tái)設(shè)置藍(lán)牙適配器是否可配對(duì)
    pairable <on/off>
  • 藍(lán)牙控制臺(tái)設(shè)置藍(lán)牙適配器是否可被法線
    discoverable <on/off>
  • 藍(lán)牙控制臺(tái)設(shè)置藍(lán)牙適配器是否可被法線
    discoverable <on/off>
  • 藍(lán)牙控制臺(tái)啟動(dòng)藍(lán)牙設(shè)備掃描
    scan on
  • 藍(lán)牙控制臺(tái)停止藍(lán)牙設(shè)備掃描
    scan off

systemctl


systemctl [OPTIONS...] {COMMAND} ...

Query or send control commands to the systemd manager.

  -h --help           Show this help
     --version        Show package version
     --system         Connect to system manager
     --user           Connect to user service manager
  -H --host=[USER@]HOST
                      Operate on remote host
  -M --machine=CONTAINER
                      Operate on local container
  -t --type=TYPE      List only units of a particular type
     --state=STATE    List only units with particular LOAD or SUB or ACTIVE stat
  -p --property=NAME  Show only properties by this name
  -a --all            Show all loaded units/properties, including dead/empty
                      ones. To list all units installed on the system, use
                      the 'list-unit-files' command instead.
  -l --full           Don't ellipsize unit names on output
  -r --recursive      Show unit list of host and local containers
     --reverse        Show reverse dependencies with 'list-dependencies'
     --job-mode=MODE  Specify how to deal with already queued jobs, when
                      queueing a new job
     --show-types     When showing sockets, explicitly show their type
  -i --ignore-inhibitors
                      When shutting down or sleeping, ignore inhibitors
     --kill-who=WHO   Who to send signal to
  -s --signal=SIGNAL  Which signal to send
  -q --quiet          Suppress output
     --no-block       Do not wait until operation finished
     --no-wall        Don't send wall message before halt/power-off/reboot
     --no-reload      When enabling/disabling unit files, don't reload daemon
                      configuration
     --no-legend      Do not print a legend (column headers and hints)
     --no-pager       Do not pipe output into a pager
     --no-ask-password
                      Do not ask for system passwords
     --global         Enable/disable unit files globally
     --runtime        Enable unit files only temporarily until next reboot
  -f --force          When enabling unit files, override existing symlinks
                      When shutting down, execute action immediately
     --preset-mode=   Specifies whether fully apply presets, or only enable,
                      or only disable
     --root=PATH      Enable unit files in the specified root directory
  -n --lines=INTEGER  Number of journal entries to show
  -o --output=STRING  Change journal output mode (short, short-monotonic,
                      verbose, export, json, json-pretty, json-sse, cat)
     --plain          Print unit dependencies as a list instead of a tree

Unit Commands:
  list-units [PATTERN...]         List loaded units
  list-sockets [PATTERN...]       List loaded sockets ordered by address
  list-timers [PATTERN...]        List loaded timers ordered by next elapse
  start NAME...                   Start (activate) one or more units
  stop NAME...                    Stop (deactivate) one or more units
  reload NAME...                  Reload one or more units
  restart NAME...                 Start or restart one or more units
  try-restart NAME...             Restart one or more units if active
  reload-or-restart NAME...       Reload one or more units if possible,
                                  otherwise start or restart
  reload-or-try-restart NAME...   Reload one or more units if possible,
                                  otherwise restart if active
  isolate NAME                    Start one unit and stop all others
  kill NAME...                    Send signal to processes of a unit
  is-active PATTERN...            Check whether units are active
  is-failed PATTERN...            Check whether units are failed
  status [PATTERN...|PID...]      Show runtime status of one or more units
  show [PATTERN...|JOB...]        Show properties of one or more
                                  units/jobs or the manager
  cat PATTERN...                  Show files and drop-ins of one or more units
  set-property NAME ASSIGNMENT... Sets one or more properties of a unit
  help PATTERN...|PID...          Show manual for one or more units
  reset-failed [PATTERN...]       Reset failed state for all, one, or more
                                  units
  list-dependencies [NAME]        Recursively show units which are required
                                  or wanted by this unit or by which this
                                  unit is required or wanted

Unit File Commands:
  list-unit-files [PATTERN...]    List installed unit files
  enable NAME...                  Enable one or more unit files
  disable NAME...                 Disable one or more unit files
  reenable NAME...                Reenable one or more unit files
  preset NAME...                  Enable/disable one or more unit files
                                  based on preset configuration
  preset-all                      Enable/disable all unit files based on
                                  preset configuration
  is-enabled NAME...              Check whether unit files are enabled

  mask NAME...                    Mask one or more units
  unmask NAME...                  Unmask one or more units
  link PATH...                    Link one or more units files into
                                  the search path
  get-default                     Get the name of the default target
  set-default NAME                Set the default target

Machine Commands:
  list-machines [PATTERN...]      List local containers and host

Job Commands:
  list-jobs [PATTERN...]          List jobs
  cancel [JOB...]                 Cancel all, one, or more jobs

Snapshot Commands:
  snapshot [NAME]                 Create a snapshot
  delete NAME...                  Remove one or more snapshots

Environment Commands:
  show-environment                Dump environment
  set-environment NAME=VALUE...   Set one or more environment variables
  unset-environment NAME...       Unset one or more environment variables
  import-environment NAME...      Import all, one or more environment variables

Manager Lifecycle Commands:
  daemon-reload                   Reload systemd manager configuration
  daemon-reexec                   Reexecute systemd manager

System Commands:
  is-system-running               Check whether system is fully running
  default                         Enter system default mode
  rescue                          Enter system rescue mode
  emergency                       Enter system emergency mode
  halt                            Shut down and halt the system
  poweroff                        Shut down and power-off the system
  reboot [ARG]                    Shut down and reboot the system
  kexec                           Shut down and reboot the system with kexec
  exit                            Request user instance exit
  switch-root ROOT [INIT]         Change to a different root file system
  suspend                         Suspend the system
  hibernate                       Hibernate the system
  hybrid-sleep                    Hibernate and suspend the system

bluetoothd


Usage:
  bluetoothd [OPTION...]

Help Options:
  -h, --help                  Show help options

Application Options:
  -d, --debug=DEBUG           Specify debug options to enable
  -p, --plugin=NAME,..,       Specify plugins to load
  -P, --noplugin=NAME,...     Specify plugins not to load
  -C, --compat                Provide deprecated command line interfaces
  -E, --experimental          Enable experimental interfaces
  -n, --nodetach              Run with logging in foreground
  -v, --version               Show version information and exit

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 blacklist
        unblock    <bdaddr>     Remove a device from the blacklist
        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

hcitool


hcitool - HCI Tool ver 5.23
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 master/slave 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
        lewladd Add device to LE White List
        lewlrm  Remove device from LE White List
        lewlsz  Read size of LE White List
        lewlclr Clear LE White list
        lecc    Create a LE Connection
        ledc    Disconnect a LE Connection
        lecup   LE Connection Update

For more information on the usage of each command use:
        hcitool <command> --help

hciattach


hciattach - HCI UART driver initialization utility
Usage:
        hciattach [-n] [-p] [-b] [-r] [-t timeout] [-s initial_speed] <tty> <type | id> [speed] [flow|noflow] [bdaddr]
        hciattach -l

bluetoothctl


Available commands:
  list                       List available controllers
  show [ctrl]                Controller information
  select <ctrl>              Select default controller
  devices                    List available devices
  paired-devices             List paired devices
  power <on/off>             Set controller power
  pairable <on/off>          Set controller pairable mode
  discoverable <on/off>      Set controller discoverable mode
  agent <on/off/capability>  Enable/disable agent with given capability
  default-agent              Set agent as the default one
  scan <on/off>              Scan for devices
  info <dev>                 Device information
  pair <dev>                 Pair with device
  trust <dev>                Trust device
  untrust <dev>              Untrust device
  block <dev>                Block device
  unblock <dev>              Unblock device
  remove <dev>               Remove device
  connect <dev>              Connect device
  disconnect <dev>           Disconnect device
  version                    Display version
  quit                       Quit program
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容