linux 蓝牙串口 连接android手机调试

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/agave7/article/details/84104785

最近要用到 linux 与 android 蓝牙串口通信,整了两天总算弄通了。
蓝牙模块用的是RealTek 8723B
蓝牙驱动买模块时会提供的,编译安装就OK了。

这里列一下调试串口的过程。

  1. 安装 bluez (包含许多蓝牙管理工具)
  2. hciconfig -a 查看设备
root@debian:~# hciconfig -a
hci0:   Type: BR/EDR  Bus: USB
        BD Address: 00:13:EF:F3:07:18  ACL MTU: 820:8  SCO MTU: 255:16
        DOWN 
        RX bytes:463149 acl:19700 sco:0 events:21660 errors:0
        TX bytes:445871 acl:19835 sco:0 commands:757 errors:0
        Features: 0xff 0xff 0xff 0xfe 0xdb 0xff 0x7b 0x87
        Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 
        Link policy: RSWITCH HOLD SNIFF PARK 
        Link mode: SLAVE ACCEPT 

  1. hciconfig hci0 up
  2. hcitool scan 查找蓝牙设备
root@debian:~# hcitool scan
Scanning ...
        C4:0B:CB:41:56:A0       红米手机
        64:A2:F9:47:34:98       OnePlus 6

  1. 为简化命令行,这里设置变量RED=“C4:0B:CB:41:56:A0” ,表示手机的MAC地址
  2. 手机需要安装蓝牙串口助手,运行打开,这样才能开启SSP服务。
  3. 查找是否有 Serial Port 服务通道,0x1101 表示Serial Port ,这里我们看到对应通道是 Channel: 6 这个通道号很重要,很多时候连不上串口都是因为通道号搞错了。
root@debian:~# sdptool search 0x1101
Class 0x1101
Inquiring ...
Searching for 0x1101 on C4:0B:CB:41:56:A0 ...
Service Name: BlueSPP SPP
Service RecHandle: 0x1000e
Service Class ID List:
  "Serial Port" (0x1101)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 6
  1. rfcomm bind 0 $RED 6
  2. rfcomm connect 0 $RED 6
root@debian:~# rfcomm connect 0 $RED 6
Connected /dev/rfcomm0 to C4:0B:CB:41:56:A0 on channel 6
Press CTRL-C for hangup

看到 Press CTRL-C for hangup 说明连接成功
如果出现Can’t create RFCOMM TTY: Address already in use
则 rfcomm release 0
再 rfcomm connect 0 $RED 6 即可

  1. 打开另一个终端窗口,cat > /dev/rfcomm0
    输入数据
    回车 即可在手机上的串口助手上看见输入的数据。
root@debian:~# cat >/dev/rfcomm0 
12345

^C

猜你喜欢

转载自blog.csdn.net/agave7/article/details/84104785
今日推荐