有用bluez做蓝牙配对吗

2019-03-26 12:20发布

本帖最后由 lzwml 于 2018-8-16 10:40 编辑
  • 系统:Linux平台:i386、ARM都试过
  • 工具:bluez工具集(hciconfig、hcitool、bluetoothd、rfcomm)、dbus
  • 软件版本:
    • bluez-4.101.tar.xz(bluez-5编译脚本没生成成功,blue-5有hcidump抓包工具)
    • dbus-1.4.14.tar.gz
bluez官网和源码里居然没任何帮助文档,比如,
  • /var/lib/bluetooth//目录是什么结构,上面需要建立什么文件
  • hcixx和rfcomm是什么关系
  • 运行rfcomm或hcixx之前是否需要启动bluetoothd
  • bluetoothd是否自动启用dbus服务
  • 如果需要手动启动dbus服务该怎么启动,这些配置文件要怎么设置
ls /etc/dbus-1/system.d/ bluetooth.conf ls /usr/local/etc/dbus-1/ session.conf session.d/ system.conf system.d/
  • hciconfig hci0 piscan 可使设备既可见
  • hciconfig -a
  • hciconfig hci0 sspmode disable #关闭简易配对模式
  • hciconfig hci0 sspmode #查看简易配对模式状态 hci0: Type: BR/EDR Bus: UART BD Address: 20:70:02:A0:00:00 ACL MTU: 1021:8 SCO MTU: 64:1 Simple Pairing mode: Disabled
上面的命令都没问题 这个agent与bluez-4里的agent不一样,可能是完全不同的东西 用命令 agent -a hci0 pinCode bd_addr 可对蓝牙终端发起配对请求,其中 pinCode 是 pin 码, 简易配对模式下用不上;关闭简易配对模式时,被动配对方需要输入这个 pin 码才能完成配 对流程。最后面的参数就是要配对的目标蓝牙设备 MAC 地址了,可用扫描功能获得。 agent -a hci0 1234 F4:8B:32:39:D4:0E Pincode request for device /org/bluez/1319/hci0/dev_F4_8B_32_39_D4_0E Agent has been released bluetoothd启动失败,貌似要打开D-bus,或者D-bus配置文件需要修改(我没做任何修改) [root@TRK200 nfs]# bluetoothd -n -d bluetoothd[1562]: Bluetooth daemon 4.37 bluetoothd[1562]: Enabling debug information bluetoothd[1562]: parsing main.conf bluetoothd[1562]: discovto=0 bluetoothd[1562]: pairto=0 bluetoothd[1562]: pageto=8192 bluetoothd[1562]: name=%h-%d <------ bluetoothd[1562]: class=0x000100 bluetoothd[1562]: inqmode=0 bluetoothd[1562]: Key file does not have key 'DeviceID' bluetoothd[1562]: Unable to get on D-Bus [root@TRK200 nfs]# 配置文件应该是这个 [root@TRK200 nfs]# cat /etc/bluetooth/main.conf [General] # List of plugins that should not be loaded on bluetoothd startup #DisablePlugins = network,input # Default adaper name # %h - substituted for hostname # %d - substituted for adapter id Name = %h-%d <------ # Default device class. Only the major and minor device class bits are # considered. Class = 0x000100 # How long to stay in discoverable mode before going back to non-discoverable # The value is in seconds. Default is 180, i.e. 3 minutes. # 0 = disable timer, i.e. stay discoverable forever DiscoverableTimeout = 0 # How long to stay in pairable mode before going back to non-discoverable # The value is in seconds. Default is 0. # 0 = disable timer, i.e. stay pairable forever PairableTimeout = 0 # Use some other page timeout than the controller default one # which is 16384 (10 seconds). PageTimeout = 8192 # Discover scheduler interval used in Adapter.DiscoverDevices # The value is in seconds. Defaults is 0 to use controller scheduler. DiscoverSchedulerInterval = 0 # What value should be assumed for the adapter Powered property when # SetProperty(Powered, ...) hasn't been called yet. Defaults to true InitiallyPowered = true # Remember the previously stored Powered state when initializing adapters RememberPowered = true # Use vendor, product and version information for DID profile support. # The values are separated by ":" and VID, PID and version. #DeviceID = 1234:5678:abcd # Do reverse service discovery for previously unknown devices that connect to # us. This option is really only needed for qualification since the BITE tester # doesn't like us doing reverse SDP for some test cases (though there could in # theory be other useful purposes for this too). Defaults to true. ReverseServiceDiscovery = true 一段日志 [root@TRK200 /]# hciconfig hci0 up [root@TRK200 /]# hcitool scan Scanning ... C0:14:3D:C1:3C:72 WIN-QEGCEAT2FS3 78:02:F8:D2:CA:2E QCOM-BTD 2C:57:31:D8:A2:05 nimei <---- 我的手机 [root@TRK200 /]# l2ping 2C:57:31:D8:A2:05 Ping: 2C:57:31:D8:A2:05 from 00:1A:7D:DA:71:11 (data size 44) ... 44 bytes from 2C:57:31:D8:A2:05 id 0 time 32.16ms 44 bytes from 2C:57:31:D8:A2:05 id 1 time 30.25ms ^C2 sent, 2 received, 0% loss [root@TRK200 /]# cat /etc/bluetooth/rfcomm.conf # # RFCOMM configuration file. # rfcomm0 { # Automatically bind the device at startup bind yes; # Bluetooth address of the device device 2C:57:31:D8:A2:05; <------- 修改成我的手机 # RFCOMM channel for the connection channel 1; # Description of the connection comment "Example Bluetooth device"; } [root@TRK200 /]# rfcomm bind /dev/rfcomm0 <--- 绑定并创建设备节点 [root@TRK200 /]# hcitool cc 2C:57:31:D8:A2:05 <---- 要尝试连接几次才能成功 [root@TRK200 /]# hcitool con Connections: [root@TRK200 /]# hcitool cc 2C:57:31:D8:A2:05 [root@TRK200 /]# hcitool con <------- 得到一次连接成功的过程,很短暂,如果不配对几秒后消失 Connections: < ACL 2C:57:31:D8:A2:05 handle 70 state 1 lm SLAVE [root@TRK200 /]# cat /dev/rfcomm0 <------- 据说如果成功,手机端会提示输入pin码,但我没有 cat: can't open '/dev/rfcomm0': Connection refused 此帖出自Linux与安卓论坛
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。