Bluetooth (简体中文)
蓝牙是一个短距离无线通信标准,适用于手机、计算机和其他电子设备之间的通信。在 Linux 中,通常使用的蓝牙协议栈实现是 BlueZ.
安装
为了使用蓝牙(Blutooth),必须要安装官方仓库中的 bluez 软件包。
bluez会使用dbus
服务读取设置和进行pin(个人识别码 personal identification number)配对。蓝牙(Bluetooth)协议需要bluetooth服务来支撑:
# systemctl enable bluetooth.service # systemctl start bluetooth.service
图形化前端
下面的软件包提供了一个图形化的界面来自定义蓝牙(Bluetooth)。
Blueman
Blueman是一个全能的基于 GTK+ 开发的蓝牙(Bluetooth)管理器,因此,被GNOME和Xfce所推荐。你可以安装官方仓库中的blueman。
在确认bluetooth
服务像上文描述的那样正在运行之后,执行blueman-applet
。为了确保这个applet在登录之后运行,将blueman-applet
添加到System -> Preferences -> Startup Applications (GNOME) 或 Xfce Menu -> Settings -> Session and Startup (Xfce)
要用 Blueman 添加或管理设备,用户需要位于 'lp' 组,参见 /etc/dbus-1/system.d/bluetooth.conf
中对 'lp' 组与 Bluetooth daemon 交互的说明。
要接收文件,请点击 Blueman tray icon -> Local Services -> Transfer -> File Receiving" 并选中 "Enabled".
Thunar 脚本工具
如果你没有使用nautilus(例如thunar)你会发现这个很有用:
obex_thunar.sh
#!/bin/bash fusermount -u ~/bluetooth obexfs -b $1 ~/bluetooth thunar ~/bluetooth
移动脚本到一个合适的位置(例如:/usr/bin
),之后将它设为可执行:
chmod +x /usr/bin/obex_thunar.sh
最后更改Blueman tray icon -> Local Services -> Transfer -> Advanced 这一行,改成 obex_thunar.sh %d
。
gnome-bluetooth
gnome-bluetooth 是旧的bluez-gnome 的一个分支,专注于跟GNOME桌面环境相集成。GNOME Bluetooth 是 gnome-shell 需要的,因此如果你在运行 GNOME 3 你应该已经安装了它。 否则,可以用 gnome-bluetooth 这个包安装。
运行bluetooth-applet
来打开一个漂亮的蓝牙(bluetooth)小程序。现在你应该可以能够通过在这个蓝牙(bluetooth)图标来设置设备并且传输文件了。为了保证这个小程序随登录启动,把它添加到System -> Preferences -> Startup Applications。
如果想要在Thunar的文件属性菜单的发送到里添加一个蓝牙(Bluetooth)入口,参考这里。
bluedevil
KDE 的蓝牙工具是bluedevil,可以用 bluedevil 包安装,它在官方仓库中。
确认bluetooth
守护进程在运行。你可以在dolphin和系统通知区域里得到一个蓝牙图标,你可以通过点击这个图标来设置bluedevil、检测蓝牙设备等等。你也可以通过KDE的系统设置来配置bluedevil。
Fluxbox, openbox, 其他的窗口管理器
你当然可以用前面提到过的程序即便你没有用GNOME、Xfce或KDE作为你的桌面管理器。 这个列表能够帮助你明白每个程序的功能:
- bluetooth-applet -- 能够实现设置、配对向导和已知设备管理的系统通知区域图标。
- /usr/lib/gnome-user-share/gnome-user-share -- 如果你想要通过obexBT来从已配对的设备接收文件的话,它应当处于运行状态。
如果你在文件传输过程中收到了一个错误,并且/或没有收到文件,把下面的代码添加到
/etc/dbus-1/system.d/bluetooth.conf
<policy user="your_user_id"> <allow own="org.bluez"/> <allow send_destination="org.bluez"/> <allow send_interface="org.bluez.Agent"/> </policy>
- bluetooth-wizard -- 负责新设备的配对工作。
- bluetooth-properties -- 也能够通过Bluetooth-applet图标打开。
- gnome-file-share-properties -- 对通过蓝牙(Bluetooth)传输文件授权。
- bluez-sendto -- 提供向远端设备传送文件的图形用户接口。
手工配置
你需要编辑 /etc/bluetooth
下的配置文件来实现人工配置Bluez。它们是:
audio.conf input.conf main.conf network.conf rfcomm.conf
默认的配置文件在绝大多数情况下都能都正常工作。绝大多数的配置选项在她们的文件里都有丰富的文档注释,所以自定义成为了一个简单到阅读配置选项的描述和注释的工作。全局设置从main.conf
开始
音频流
如果你想启用从你的设备到计算机的音频流,你必须更改 audio.conf
然后添加这个到 [General]
部分:
Enable=Socket
配对
许多蓝牙(Bluetooth)设备要求在使用时配对。 实际上的过程取决于配对涉及到的设备和这些设备的输入功能。 在移动电话上的配对过程可能会像下面那样:
- 计算机向移动电话发送一个配对请求
- 移动电话上会提示输入一个由计算机给出的PIN(个人识别码)码
- 必须在计算机上重新输入同一个序列(指上文中的PIN)。
通过执行一下命令来搜索外部设备
$ hcitool scan
在不使用gnome-bluez包的情况下配对设备,你需要使用一个包含在bluez包里的,叫做bluez-simple-agent的工具。你需要从常用的软件仓库里获取几个跟python相关的工具来使这种方法可用:dbus-python和pygobject。 如果你准备好了,你可以以root身份执行这个脚本:
# bluez-simple-agent
如果它工作正常,你应当会在控制台得到消息:"Agent registered" 。
现在你可以开始从你的移动电话开始配对工作,这个脚本会在控制台询问你PIN码,你输入进去然后按回车键确认,这样就完成了配对工作。接下来可以通过^C-c终止这个脚本的执行,只需要在配对的时候使用这个脚本,而连接设备时不需要用到它。
需要查看例子.请向下翻页到示例部分。
使用 Obex 发送接收文件
除了KDE和Gnome Bluetooth软件包之外,Obexfs是另一个选择,它可以允许你挂载你的移动电话作为文件系统的一部分。注意,如果要使用Obex,你需要一个能够提供Obex FTP服务的设备。 安装Obex;
# pacman -S obexfs
然后你可以以root权限运行如下命令来挂载你的移动电话
# obexfs -b <devices mac address> /mountpoint
单看更多挂载选项,参阅http://dev.zuckschwerdt.org/openobex/wiki/ObexFs
示例
西门子 S55
这是我链接我的S55的过程。(我还没搞明白怎么样通过电话来初始化连接。)
- 根据安装步骤
$> hcitool scan Scanning ... XX:XX:XX:XX:XX:XX NAME $> B=XX:XX:XX:XX:XX:XX
在另一个终端启动simple-agent
$> su -c bluez-simple-agent Password: Agent registered
回到上一个终端
$> obexftp -b $B -l "Address book" # Phone ask for pin, I enter it and answer yes when asked if I want to save the device ... <file name="5F07.adr" size="78712" modified="20030101T001858" user-perm="WD" group-perm="" /> ... $> obexftp -b 00:01:E3:6B:FF:D7 -g "Address book/5F07.adr" Browsing 00:01:E3:6B:FF:D7 ... Channel: 5 Connecting...done Receiving "Address book/5F07.adr"... Sending "Address book"... done Disconnecting...done $> obexftp -b 00:01:E3:6B:FF:D7 -p a ... Sending "a"... done Disconnecting...done
罗技鼠标 MX Laser / M555b
快速测试连接
$> hidd --connect XX:XX:XX:XX:XX:XX
通过桌面想到设置鼠标一实现自动重连。 如果你的桌面环境不提供对此任务的支持,参阅Bluetooth mouse manual configuration向导.
摩托罗拉 V900
在安装blueman并且运行blueman-applet之后,在摩托罗拉设备的菜单connections -> bluetooth下单击"find me"。在blueman-applet搜索设备,找到摩托罗拉,然后单击blueman-applet中的“添加。单击“bond”,输入pin码,根据摩托罗达上的提示输入相同的pin码,在终端执行:
cd ~/ mkdir bluetooth-temp obexfs -n xx:yy:zz:... ~/bluetooth-temp cd ~/bluetooth-temp
然后浏览... 这样操作时只能访问音频、视频和图片。
摩托罗拉 RAZ
> pacman -S obextool obexfs obexftp openobex bluez
> lsusb Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 002 Device 002: ID 03f0:171d Hewlett-Packard Wireless (Bluetooth + WLAN) Interface [Integrated Module] Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
> hciconfig hci0 up
> hciconfig hci0: Type: BR/EDR Bus: USB BD Address: 00:16:41:97:BA:5E ACL MTU: 1017:8 SCO MTU: 64:8 UP RUNNING RX bytes:348 acl:0 sco:0 events:11 errors:0 TX bytes:38 acl:0 sco:0 commands:11 errors:0
> hcitool dev Devices: hci0 00:16:41:97:BA:5E
注意:确保手机上的蓝牙处于打开状态并且是可见的
> hcitool scan Scanning ... 00:1A:1B:82:9B:6D [quirxi]
> hcitool inq Inquiring ... 00:1A:1B:82:9B:6D clock offset: 0x1ee4 class: 0x522204
> l2ping 00:1A:1B:82:9B:6D Ping: 00:1A:1B:82:9B:6D from 00:16:41:97:BA:5E (data size 44) ... 44 bytes from 00:1A:1B:82:9B:6D id 0 time 23.94ms 44 bytes from 00:1A:1B:82:9B:6D id 1 time 18.85ms 44 bytes from 00:1A:1B:82:9B:6D id 2 time 30.88ms 44 bytes from 00:1A:1B:82:9B:6D id 3 time 18.88ms 44 bytes from 00:1A:1B:82:9B:6D id 4 time 17.88ms 44 bytes from 00:1A:1B:82:9B:6D id 5 time 17.88ms 6 sent, 6 received, 0% loss
> hcitool name 00:1A:1B:82:9B:6D [quirxi]
# hciconfig -a hci0 hci0: Type: BR/EDR Bus: USB BD Address: 00:16:41:97:BA:5E ACL MTU: 1017:8 SCO MTU: 64:8 UP RUNNING RX bytes:9740 acl:122 sco:0 events:170 errors:0 TX bytes:2920 acl:125 sco:0 commands:53 errors:0 Features: 0xff 0xff 0x8d 0xfe 0x9b 0xf9 0x00 0x80 Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 Link policy: Link mode: SLAVE ACCEPT Name: 'BCM2045' Class: 0x000000 Service Classes: Unspecified Device Class: Miscellaneous, HCI Version: 2.0 (0x3) Revision: 0x204a LMP Version: 2.0 (0x3) Subversion: 0x4176 Manufacturer: Broadcoml / Corporation (15)
> hcitool info 00:1A:1B:82:9B:6D Requesting information ... BD Address: 00:1A:1B:82:9B:6D Device Name: [quirxi] LMP Version: 1.2 (0x2) LMP Subversion: 0x309 Manufacturer: Broadcom Corporation (15) Features: 0xff 0xfe 0x0d 0x00 0x08 0x08 0x00 0x00 <3-slot packets> <5-slot packets> <encryption> <slot offset> <timing accuracy> <role switch> <hold mode> <sniff mode> <RSSI> <channel quality> <SCO link> <HV2 packets> <HV3 packets> <A-law log> <CVSD> <power control> <transparent SCO> <AFH cap. slave> <AFH cap. master>
编辑你的main.conf并且输入对应与你手机的class( Class = 0x100100 ):
> vim /etc/bluetooth/main.conf
# Default device class. Only the major and minor device class bits are # considered. #Class = 0x000100 Class = 0x100100
> /etc/rc.d/dbus start :: Starting D-BUS system messagebus [DONE]
> /etc/rc.d/bluetooth start :: Stopping bluetooth subsystem: pand dund rfcomm hidd bluetoothd [DONE] :: Starting bluetooth subsystem: bluetoothd
通过bluez-simple-agent配对只需要做一次。当你的手机要求输入pin码时,输入0000
> /usr/bin/bluez-simple-agent hci0 00:1A:1B:82:9B:6D RequestPinCode (/org/bluez/10768/hci0/dev_00_1A_1B_82_9B_6D) Enter PIN Code: 0000 Release New device (/org/bluez/10768/hci0/dev_00_1A_1B_82_9B_6D)
现在你可以动过ObexFTP来访问你手机上的文件了:
> obexftp -v -b 00:1A:1B:82:9B:6D -B 9 -l Connecting..\done Tried to connect for 448ms Receiving "(null)"...-<?xml version="1.0" ?> <!DOCTYPE folder-listing SYSTEM "obex-folder-listing.dtd"> <folder-listing> <parent-folder /> <folder name="audio" size="0" type="folder" modified="20101010T132323Z" user-perm="RW" /> <folder name="video" size="0" type="folder" modified="20101010T132323Z" user-perm="RW" /> <folder name="picture" size="0" type="folder" modified="20101010T132323Z" user-perm="RW" /> </folder-listing> done Disconnecting..\done
你也可以把你的手机作为文件系统的一部分挂载在你的计算机上:
> groupadd bluetooth > mkdir /mnt/bluetooth > chown root:bluetooth /mnt/bluetooth > chmod 775 /mnt/bluetooth > usermod -a -G bluetooth arno
> obexfs -b 00:1A:1B:82:9B:6D /mnt/bluetooth/ > l /mnt/bluetooth/ total 6 drwxr-xr-x 1 root root 0 10. Okt 13:25 . drwxr-xr-x 5 root root 4096 10. Okt 10:08 .. drwxr-xr-x 1 root root 0 10. Okt 2010 audio drwxr-xr-x 1 root root 0 10. Okt 2010 picture drwxr-xr-x 1 root root 0 10. Okt 2010 video
使用bluez-simple-agent来跟iPhone配对
假设有一个叫做hci0的蓝牙设备和一个在hcitool里显示为'00:00:DE:AD:BE:EF'的iPhone:
# bluez-simple-agent hci0 00:00:DE:AD:BE:EF Passcode:
疑难解答
passkey-agent
$> passkey-agent --default 1234 Can't register passkey agent The name org.bluez was not provided by any .service files
你可能在启动/etc/rc.d/dbus之前启动了 /etc/rc.d/bluetooth
$> hciconfig dev # (no listing)
尝试运行 hciconfig hc0 up
Blueman
如果blueman-applet启动失败,试着删除整个/var/lib/bluetooth目录然后重新启动电脑(或者仅仅重启hal、dbus和bluetooth这几个服务)。
# rm -rf /var/lib/bluetooth # reboot
gnome-bluetooth
如果你通过在蓝牙属性里接受文件时受到如下消息:
Bluetooth OBEX start failed: Invalid path Bluetooth FTP start failed: Invalid path
那么运行
# pacman -S xdg-user-dirs $ xdg-user-dirs-update
你可以通过下列方式修改路径:
$ vi ~/.config/user-dirs.dirs
Bluetooth USB 适配器
如果你在使用USB适配器,你应当确认你的适配器被正确识别。你可以在插入适配器时通过查看/var/log/messages.log,这应当会出现类似于下面所示的信息:
# tail -f /var/log/messages.log May 2 23:36:40 tatooine usb 4-1: new full speed USB device using uhci_hcd and address 9 May 2 23:36:40 tatooine usb 4-1: configuration #1 chosen from 1 choice May 2 23:36:41 tatooine hcid[8109]: HCI dev 0 registered May 2 23:36:41 tatooine hcid[8109]: HCI dev 0 up May 2 23:36:41 tatooine hcid[8109]: Device hci0 has been added May 2 23:36:41 tatooine hcid[8109]: Starting security manager 0 May 2 23:36:41 tatooine hcid[8109]: Device hci0 has been activated
查看支持的硬件列表,请参考本页的resources。
如果你只得到了前面两行,说明了电脑发现了这个设备,但是你需要手动启动它。 例如:
hciconfig -a hci0 hci0: Type: USB BD Address: 00:00:00:00:00:00 ACL MTU: 0:0 SCO MTU: 0:0 DOWN RX bytes:0 acl:0 sco:0 events:0 errors:0 TX bytes:0 acl:0 sco:0 commands:0 errors: sudo hciconfig hci0 up hciconfig -a hci0 hci0: Type: USB BD Address: 00:02:72:C4:7C:06 ACL MTU: 377:10 SCO MTU: 64:8 UP RUNNING RX bytes:348 acl:0 sco:0 events:11 errors:0 TX bytes:38 acl:0 sco:0 commands:11 errors:0
要验证这个设备被正确检测,你可以使用bluez-utils当中的hcitool。你可以通过下面方式获得受支持的设备、它们的识别码和硬件地址:
$ hcitool dev Devices: hci0 00:1B:DC:0F:DB:40
可以通过hciconfig获得关于这个设备更详细的信息。
$ hciconfig -a hci0 hci0: Type: USB BD Address: 00:1B:DC:0F:DB:40 ACL MTU: 310:10 SCO MTU: 64:8 UP RUNNING PSCAN ISCAN RX bytes:1226 acl:0 sco:0 events:27 errors:0 TX bytes:351 acl:0 sco:0 commands:26 errors:0 Features: 0xff 0xff 0x8f 0xfe 0x9b 0xf9 0x00 0x80 Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 Link policy: RSWITCH HOLD SNIFF PARK Link mode: SLAVE ACCEPT Name: 'BlueZ (0)' Class: 0x000100 Service Classes: Unspecified Device Class: Computer, Uncategorized HCI Ver: 2.0 (0x3) HCI Rev: 0xc5c LMP Ver: 2.0 (0x3) LMP Subver: 0xc5c Manufacturer: Cambridge Silicon Radio (10)
hcitool scan: 没找到设备
- 在某些Dell的笔记本电脑(比如 Studio 15)上,你必须把蓝牙模式由HID改成HCI
# hid2hci
- 有时候这个简单的命令也会有用:
# hciconfig hci0 up
我的电脑不可见
如果不能从你的移动电话上找到你的电脑,打开PSCAN和ISCAN:
# 打开 PSCAN 和 ISCAN $ hciconfig hci0 piscan # 确认操作有效 $ hciconfig hci0: Type: USB BD Address: 00:12:34:56:78:9A ACL MTU: 192:8 SCO MTU: 64:8 UP RUNNING PSCAN ISCAN RX bytes:20425 acl:115 sco:0 events:526 errors:0 TX bytes:5543 acl:84 sco:0 commands:340 errors:0
试着在 /etc/bluetooth/main.conf 改变设备的class
# Default device class. Only the major and minor device class bits are # considered. #Class = 0x000100 (from default config) Class = 0x100100
This was the only solution to make my computer visible for my phone.
Nautilus cannot browse files
If nautilus doesn't open and show this error:
Nautilus cannot handle obex: locations. Couldn't display "obex://[XX:XX:XX:XX:XX:XX]/".
Install gvfs-obexftp package:
# pacman -S gvfs-obexftp