linux藍牙初探


剛接觸,不成體系,把遇到的點記錄一下

遇到的困難1

問題描述:在windows的筆記本上,筆記本上有藍牙功能。在windows上裝了個virtual box,在vbox里裝了ubuntu18.04。在windows里開啟了藍牙,在ubuntu里就是無法使用藍牙,在網上查了一大頓,無果,你說氣不氣。昨天迎來了轉機,吼吼,在mac的筆記本上,也裝了個virtual box,發現在添加USB的地方,可以添加藍牙:),然后在mac上的virtual box里的ubuntu就能使用藍牙了(mac的藍牙就不能使用了),真是驚喜。馬上去windows上的virtual box找, 看看有沒有相同的設置,發現沒有:(

但是有下面的選擇項目,嘗試選擇Intel Corp這一項,然后進入ubuntu,發現藍牙好用了(同時windows系統的藍牙就沒了,關閉ubuntu后,windows的藍牙還能出來)。

讀到這里,估計有的童鞋就會問了,怎么知道ubuntu里的藍牙是否好用了呢?沒錯,使用下面的方法:

  • 方法1:在終端里輸入bluetoothctl,如果顯示出一堆【NEW】的東西,就說明檢測到了周圍的藍牙設備,大家的手機基本都有藍牙功能,所以能檢測出老多了。

    $ bluetoothctl
    [NEW] Controller A0:51:0B:4B:2D:4F min [default]
    [NEW] Device 52:1D:57:23:AC:27 52-1D-57-23-AC-27
    [NEW] Device 42:8E:3C:30:31:17 42-8E-3C-30-31-17
    [NEW] Device 50:39:24:E0:25:96 50-39-24-E0-25-96
    [NEW] Device 72:09:00:FF:27:D0 72-09-00-FF-27-D0
    [NEW] Device 57:FD:BC:56:37:B6 57-FD-BC-56-37-B6
    [NEW] Device 55:CA:DC:D0:57:2C 55-CA-DC-D0-57-2C
    [NEW] Device 6C:28:ED:56:55:50 6C-28-ED-56-55-50
    [NEW] Device 6F:01:3D:03:C6:F9 6F-01-3D-03-C6-F9
    [NEW] Device 66:ED:9B:94:37:C7 66-ED-9B-94-37-C7
    [NEW] Device 64:28:42:5F:BA:FF 64-28-42-5F-BA-FF
    [NEW] Device 72:0B:C2:24:F0:4A 72-0B-C2-24-F0-4A
    [NEW] Device 5B:92:B9:21:FA:26 5B-92-B9-21-FA-26
    
  • 方法2:在圖形界面,點擊Bluetooth后,右側能顯示出設備,則說明藍牙ok

遇到的問題2

linux上的藍牙協議棧(bluez)和openobex是什么關系,在ubuntu18.04上有2個daemon:obexd,bluetoothd

  • /usr/lib/bluetooth/obexd
  • /usr/lib/bluetooth/bluetoothd

目前還不知道它們的區別。

遇到的問題3

如何在ubuntu里,使用藍牙連接別的藍牙設備

使用bluetoothctl命令。進入[bluetooth]后,輸入help

[bluetooth]# help
Menu main:
Available commands:
-------------------
advertise                                         Advertise Options Submenu
scan                                              Scan Options Submenu
gatt                                              Generic Attribute Submenu
list                                              List available controllers
show [ctrl]                                       Controller information
select <ctrl>                                     Select default controller
devices                                           List available devices
paired-devices                                    List paired devices
system-alias <name>                               Set controller alias
reset-alias                                       Reset controller alias
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
advertise <on/off/type>                           Enable/disable advertising with given type
set-alias <alias>                                 Set device alias
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
menu <name>                                       Select submenu
version                                           Display version
quit                                              Quit program
exit                                              Quit program
help                                              Display help about this program

頭3個advertise,scan,gatt是子menu的,如何進入子menu呢?使用menu 自menu名字

進入gatt子menu:

[bluetooth]# menu gatt
Menu gatt:
Available commands:
-------------------
list-attributes [dev]                             List attributes
。。。

退回到主menu:back

[bluetooth]# back
Menu main:
Available commands:
-------------------
advertise                                         Advertise Options Submenu
scan                                              Scan Options Submenu
gatt                                              Generic Attribute Submenu

連接別的藍牙設備的步驟:

步驟1 :輸入bluetoothctl

$ bluetoothctl
[bluetooth]#

步驟2 :輸入scan on,掃描周圍的藍牙設備

[bluetooth]#scan on

步驟3:找到你要連接的藍牙設備的mac地址。

[bluetooth]# devices
Device 20:3C:AE:06:F1:F5 my_device

20:3C:AE:06:F1:F5就是設備的mac

步驟4:連接目標設備

[bluetooth]# pair 20:3C:AE:06:F1:F5
Attempting to pair with 20:3C:AE:06:F1:F5

如果目標設備是手機,則在手機上回彈出一個確認的框框,顯示pin的數字,然后在終端輸入yes。

遇到的問題4

想使用工具obex_test給已經連上的設備發東西(文件等),在終端需要輸入目標設備的MAC地址和信道。可以知道目標設備的MAC地址,但是信道(channel)是啥呢?

可以使用sdptool browse 設備MAC地址 查找到信道。

$ sdptool browse 20:3C:AE:06:F1:F5
輸入很長

輸出節選:

GATT service:發現沒有channel

Service Name: GATT
Service RecHandle: 0x4f491801
Service Class ID List:
  "Generic Attribute" (0x1801)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 31
  "ATT" (0x0007)
    uint16: 0x0001
    uint16: 0x0013

MAP service:使用channel 2。所以就可以使用obex_test工具了。

Service Name: MAP MAS-iOS
Service RecHandle: 0x4f491132
Service Class ID List:
  "Message Access - MAS" (0x1132)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 2
  "OBEX" (0x0008)

使用:obex_test。

但是輸入c(連接)后,顯示如下,無法連接上,不知道為什么。

ys@min:~$ obex_test -b 20:3C:AE:06:F1:F5 2
Using Bluetooth RFCOMM transport
OBEX Interactive test client/server.
> c
Made some progress...
Unknown event 05!
Error while doing OBEX_HandleInput()
>

PBAP service:使用channel 8。

Service Name: Phonebook
Service RecHandle: 0x4f49112f
Service Class ID List:
  "Phonebook Access - PSE" (0x112f)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 13
  "OBEX" (0x0008)

連接PBAP service:出現下面的錯誤。

$ obex_test -b 20:3C:AE:06:F1:F5 8
Using Bluetooth RFCOMM transport
OBEX Interactive test client/server.
> c
Made some progress...
Link broken!
Error while doing OBEX_HandleInput()
>

已經被棄用的bluez工具集和代替品

參考:https://wiki.archlinux.org/index.php/Bluetooth

Deprecated tool Most likely replacement
gatttool btgatt-client, D-Bus Gatt API
hciattach btattach
hciconfig btmgmt (and bluetoothctl?)
hcidump btmon (and btsnoop)
hcitool missing, D-Bus Device API available
rfcomm ciptool missing, implement with D-Bus Profile1 API?
sdptool missing, functionality seems to be scattered over different D-Bus objects: ProfileAdvertising, and the UUIDs arrays in device and adapter.

obexd的dbus啟動配置文件編寫

完全不懂!

參考:https://www.cnblogs.com/cxt-janson/p/11457139.html

樹莓派上,連接別的藍牙設備的國外教程

https://mcuoneclipse.com/2016/12/19/tutorial-ble-pairing-the-raspberry-pi-3-model-b-with-hexiwear/

c/c++ 學習互助QQ群:877684253

本人微信:xiaoshitou5854


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM