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

posted @ 2020-07-22 16:45  小石王  阅读(1964)  评论(0编辑  收藏  举报