Chinaunix首页 | 论坛 | 博客
  • 博客访问: 570728
  • 博文数量: 155
  • 博客积分: 7055
  • 博客等级: 少将
  • 技术积分: 1700
  • 用 户 组: 普通用户
  • 注册时间: 2004-11-22 11:40
文章分类

全部博文(155)

文章存档

2016年(1)

2011年(2)

2010年(1)

2009年(2)

2008年(9)

2007年(39)

2006年(58)

2005年(38)

2004年(5)

我的朋友

分类: LINUX

2006-11-12 19:09:19

Rays1.5r2 下蓝牙的使用

直接安装官方源提供的包就可以使用,只是在文件传送时有中文的存在乱码。

设置源:/etc/apt/sources.list
deb stable main contrib non-free

安装bluez-utils,bluez-gnome,python-bluez obexpushd ussp-push
root@hjh:~# apt-get install bluez-utils
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
将会安装下列额外的软件包:
libbluetooth2 libdbus-1-3
建议安装的软件包:
bluez-firmware
推荐安装的软件包:
bluez-passkey-gnome
下列【新】软件包将被安装:
bluez-utils libbluetooth2 libdbus-1-3
共升级了 0 个软件包,新安装了 3 个软件包,要卸载 0 个软件包,有 683 个软件未被升级。
需要下载 561kB 的软件包。
解压缩后会消耗掉 1323kB 的额外空间。
您希望继续执行吗?[Y/n]y
【警告】:下列的软件包不能通过验证!
libbluetooth2 libdbus-1-3 bluez-utils
不经验证就安装这些软件包么?[y/N] y
获取:1 stable/main libbluetooth2 3.7-1 [40.3kB]
获取:2 stable/main libdbus-1-3 1.0.2-1 [269kB]
获取:3 stable/main bluez-utils 3.7-1 [252kB]
下载 561kB,耗时 15s (35.7kB/s)
选中了曾被取消选择的软件包 libbluetooth2。
(正在读取数据库 ... 系统当前总共安装有 69841 个文件和目录。)
正在解压缩 libbluetooth2 (从 .../libbluetooth2_3.7-1_i386.deb) ...
选中了曾被取消选择的软件包 libdbus-1-3。
正在解压缩 libdbus-1-3 (从 .../libdbus-1-3_1.0.2-1_i386.deb) ...
选中了曾被取消选择的软件包 bluez-utils。
正在解压缩 bluez-utils (从 .../bluez-utils_3.7-1_i386.deb) ...
正在设置 libbluetooth2 (3.7-1) ...

正在设置 libdbus-1-3 (1.0.2-1) ...

正在设置 bluez-utils (3.7-1) ...
Creating device nodes ...
udev active, devices will be created in /dev/.static/dev/
Starting bluetooth: hcid sdpd sdp_options rfcomm.


安装后,在/etc/init.d/下有bluetooth脚本,默认就启动了。

hciconfig -a 查看电脑上的蓝牙适配器已开启。
root@hjh:~# hciconfig -a
hci0:   Type: USB
    BD Address: 11:11:11:11:11:11 ACL MTU: 678:8 SCO MTU: 64:10
    UP RUNNING PSCAN
    RX bytes:667 acl:0 sco:0 events:20 errors:0
    TX bytes:326 acl:0 sco:0 commands:20 errors:0
    Features: 0xbf 0xfe 0x8d 0x78 0x08 0x18 0x00 0x00
    Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
    Link policy: RSWITCH HOLD SNIFF PARK
    Link mode: SLAVE ACCEPT
    Name: 'hjh-0'
    Class: 0x3e0100
    Service Classes: Networking, Rendering, Capturing, Object Transfer, Audio
    Device Class: Computer, Uncategorized
    HCI Ver: 1.2 (0x2) HCI Rev: 0x1fe LMP Ver: 1.2 (0x2) LMP Subver: 0x1fe
    Manufacturer: Integrated System Solution Corp. (57)


hcitool scan PC通过蓝牙查找手机(先把手机的蓝牙开启)
root@hjh:~# hcitool scan
Scanning ...
    00:13:E0:2A:99:6A     Nokia 6600

l2ping `cat 6600_mac` 确认能ping通手机。
root@hjh:~# l2ping `cat 6600_mac `
Ping: 00:13:E0:2A:99:6A from 11:11:11:11:11:11 (data size 44) ...
0 bytes from 00:13:E0:2A:99:6A id 0 time 71.40ms
0 bytes from 00:13:E0:2A:99:6A id 1 time 24.60ms
0 bytes from 00:13:E0:2A:99:6A id 2 time 23.57ms
0 bytes from 00:13:E0:2A:99:6A id 3 time 23.54ms
4 sent, 4 received, 0% loss

系统会自动运行bluetooth-applet,(bluez-gnome包提供)选择“首选项”,设置 Mode of Operation 为
Visible and connectable for other devices

图:



这时手机可以通过蓝牙找到电脑。输入密码后可以配对。

obexpushd 可以接收从手机传送过来的信息。自动放在当前目录。文件名是中文的话,在电脑上变成乱码。

ussp-push 可以从PC上传送文件到手机上。
rfcomm bind 0 `cat 6600_mac ` 1
//6600使用channel 1 用于Dial-up Networking,下面的sdptool browse可以看到。
//rfcomm命令将与手机的蓝牙连接绑定到设备/dev/.static/dev/rfcomm0,如同/dev/ttyS0,可以被终端软件如minicom,kermit使用。
ussp-push `cat 6600_mac`@ 145.htm 145.htm

root@hjh:~# sdptool browse
Inquiring ...
Browsing 00:13:E0:2A:99:6A ...
Service Name: Fax
Service RecHandle: 0x10000
Service Class ID List:
"Fax" (0x1111)
"Generic Telephony" (0x1204)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
  Channel: 1
Language Base Attr List:
code_ISO639: 0x656e
encoding:   0x6a
base_offset: 0x100
Profile Descriptor List:
"Fax" (0x1111)
  Version: 0x0100

Service Name: Dial-up Networking
Service RecHandle: 0x10001
Service Class ID List:
"Dialup Networking" (0x1103)
"Generic Networking" (0x1201)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
  Channel: 1
Language Base Attr List:
code_ISO639: 0x656e
encoding:   0x6a
base_offset: 0x100
Profile Descriptor List:
"Dialup Networking" (0x1103)
  Version: 0x0100

Service Name: Bluetooth Serial Port
Service Description: Bluetooth Serial Port
Service Provider: Symbian Ltd.
Service RecHandle: 0x10002
Service Class ID List:
"Serial Port" (0x1101)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
  Channel: 2
Language Base Attr List:   code_ISO639: 0x656e
encoding:   0x6a
base_offset: 0x100

Service Name: OBEX Object Push
Service RecHandle: 0x10003
Service Class ID List:
"OBEX Object Push" (0x1105)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
  Channel: 9
"OBEX" (0x0008)
Language Base Attr List:
code_ISO639: 0x656e
encoding:   0x6a
base_offset: 0x100
Profile Descriptor List:
"OBEX Object Push" (0x1105)
  Version: 0x0100

Service Name: OBEX File Transfer
Service RecHandle: 0x10006
Service Class ID List:
"OBEX File Transfer" (0x1106)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
  Channel: 10
"OBEX" (0x0008)
Language Base Attr List:
code_ISO639: 0x656e
encoding:   0x6a
base_offset: 0x100
Profile Descriptor List:
"OBEX File Transfer" (0x1106)
  Version: 0x0100

Service Name: Handsfree Audio Gateway
Service RecHandle: 0x10007
Service Class ID List:
"Handfree Audio Gateway" (0x111f)
"Generic Audio" (0x1203)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
  Channel: 3
Language Base Attr List:
code_ISO639: 0x656e
encoding:   0x6a
base_offset: 0x100
Profile Descriptor List:
"Handsfree" (0x111e)
  Version: 0x0101


20070603
-------------

所谓操作,到现在只是通过蓝牙做了二件事:
一、通过虚拟串口连接上手机,执行AT命令操作手机(发短信而已)
二、手机和电脑互传文件

如果使用redhat 9.0,内核版本为2.4.20,要支持bluetooth的话,就得升级内核,
所以还不如更新发行版。如FC5就支持bluetooth,并带了相关的软件。

1, 启动bluetooth服务,查找到手机的MAC是00:13:E0:2A:99:6A,
在Channel 1提供Dail-up Networking,在channel 9提供OBEX Object Push服务

/etc/init.d/bluetooth start
Nov  5 20:43:50 hjh hcid[1766]: Bluetooth HCI daemon
Nov  5 20:43:50 hjh sdpd[1769]: Bluetooth SDP daemon
...
sdptool browse
...

2, 生成虚拟串口连接设备:
[root@hjh ~]# rfcomm bind 0 `cat ~/6600_mac` 1
[root@hjh ~]# ll /dev/rfcomm0
crw------- 1 root root 216, 0 Nov  5 20:49 /dev/rfcomm0

3,
试一试能不能和手机连接:
cat < /dev/rfcomm0
有时出现:
Operation already in progress,可能得重启了。具体原因不明。

这时就可以使用终端软件连接上手机,然后使用AT命令操作手机,
可以使用minicom,但有些时候有些问题,表现在: 手机提示输入pin,但很快电脑显示minicom: cannot open /dev/rfcomm0: No such file or directory
不知如何解决,而用kermit无啥问题:
set line /dev/rfcomm0
set speed 9600
connect -> 输入AT命令。如:
ati3
Nokia 6600 就可以使用发短信的AT命令了
退出:Ctrl-\  再按 C

4,
FC5里除了obex_test,还加了一个obex_push,用于电脑和蓝牙设备(手机)之间互传文件
* 使用obex_push传送文件给手机:
obex_push 9 `cat ~/6600_mac ` /mnt/d/mig-29.jpg
* 接收文件(默认存在/tmp)
obex_push

参考:

 linux unwired 这本书里具体有一章讲bluetooth.

bluetooth里有一些命令是以hci打头的,such as hciconfig/hciscan 它的意思是:

The prefix "hci" derives from the name of the interface between the computer and the Bluetooth device, the Host Controller Interface.

To see which services the system's SDP daemon is advertising, use the special Bluetooth address FF:FF:FF:00:00:00, which refers to the local Bluetooth device: sdptool browse FF:FF:FF:00:00:00.

======

20090321
如果在连接时无法配对(pair),又没有提示有用的错误信息。可以试一试下面的方法
运行 python passagent2.py &
这样当手机主动与电脑配对时,电脑会弹出一个对话框提示输入PIN。

passagent2.py的内容如下:
import dbus
import dbus.glib
import dbus.service
import gobject
import sys
import gtk

def pin_entry(address):
        # build dialog
        dialog = gtk.Dialog("New bluetooth connection", None, gtk.DIALOG_MODAL,
                        (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
        dialog.set_default_response(gtk.RESPONSE_ACCEPT)
        dialog.set_position(gtk.WIN_POS_CENTER)

        # tabulka
        table = gtk.Table(2, 2, False)
        dialog.vbox.pack_start(table)
        table.show()

        # entry
        entry = gtk.Entry()
        entry.set_activates_default(True)
        table.attach(entry, 1, 2, 1, 2, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 10, 2)
        entry.show()

        # icon
        icon = gtk.Image()
        icon.set_from_stock(gtk.STOCK_DIALOG_AUTHENTICATION, gtk.ICON_SIZE_DIALOG)
        table.attach(icon, 0, 1, 0, 2, gtk.FILL, gtk.FILL, 12, 12)
        icon.show()

        # label
        label = gtk.Label("New bluetooth connection to " + address + "\nPlease enter PIN:")
        label.set_alignment(0, 1)
        table.attach(label, 1, 2, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 10, 2)
        label.show()
       
        # run dialog
        if dialog.run() == gtk.RESPONSE_ACCEPT:
                pin = entry.get_text()
        else:
                pin = ""
        dialog.destroy()
        return pin


class PasskeyAgent(dbus.service.Object):
        def __init__(self, path):
                dbus.service.Object.__init__(self, dbus.SystemBus(), path)

        @dbus.service.method(dbus_interface='org.bluez.PasskeyAgent',
                                                 in_signature='ssb', out_signature='s')

        def Request(self, path, address, numeric):
                try:
                        pin = pin_entry(address)
                        return pin
                except:
                        return ""

if __name__ == "__main__":
        PATH = '/my/PasskeyAgent'
        bus = dbus.SystemBus();
        handler = PasskeyAgent(PATH)
        adapter = bus.get_object('org.bluez', '/org/bluez/hci0')
        sec = dbus.Interface(adapter, 'org.bluez.Security')
        sec.RegisterDefaultPasskeyAgent(PATH)
        main_loop = gobject.MainLoop()
        main_loop.run()

阅读(2181) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~