首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> Symbian >> 网络 >> 文章正文
S60 Python 编程指南——蓝牙连接( 手机到PC)
作者:朱珠    文章来源:http://fred.webcan.cn/weblog/    更新时间:2007-2-5 10:19:02

创建一个应用程序能够让手机通过蓝牙连接到你PC。这个程序能够在手机上书写,并能在PC上接收到。
1、你需要使用 socket module :
1)让蓝牙扫描发现PC上的BT地址和服务,并连接上PC
2)创建一个能够触发一个文本输入框并能发送输入内容从蓝牙到PC终端的函数
示例代码:
# Copyright (c) 2005 Jurgen Scheible
# script that connects to the serial port of the PC
# and lets you send characters to the PC

import appuifw
import socket
import e32

def bt_connect():
    global sock
    sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
    target=''
    if not target:
        address,services=socket.bt_discover()
        print "Discovered: %s, %s"%(address,services)
        if len(services)>1:
            import appuifw
            choices=services.keys()
            choices.sort()
            choice=appuifw.popup_menu([unicode(services[x])+": "+x
                                        for x in choices],u'Choose port:')
            target=(address,services[choices[choice]])
        else:
            target=(address,services.values()[0])
    print "Connecting to "+str(target)
    sock.connect(target)
    print "OK."

    bt_typetext()

def bt_typetext():
    global sock
    test = appuifw.query(u"Type words", "text", u"")
    if test == None:
        exit_key_handler()
    else:
        sock.send(test)
        bt_typetext()

def exit_key_handler():
    script_lock.signal()
    appuifw.app.set_exit()

appuifw.app.title = u"bt mob to PC"

script_lock = e32.Ao_lock()

appuifw.app.exit_key_handler = exit_key_handler()

bt_connect()

script_lock.wait()
说明文档:
# Copyright (c) 2005 Jurgen Scheible
# script that connects to the serial port of the PC
# and lets you send characters to the PC

import appuifw
# import the module socket
import socket
import e32

# function that handles the bluetooth connection:
def bt_connect():
    global sock
    # create a bluetooth socket
    sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
    target=''# here you can give the bt address of the other mobile if you know it
    if not target:
        # scan for bluetooth devices
        address,services=socket.bt_discover()
        print "Discovered: %s, %s"%(address,services)
        if len(services)>1:
            choices=services.keys()
            choices.sort()
            # bring up a popup menu and show the available bt devices for selection
            choice=appuifw.popup_menu([unicode(services[x])+": "+x
                                        for x in choices],u'Choose port:')
            target=(address,services[choices[choice]])
        else:
            target=(address,services.values()[0])
    print "Connecting to "+str(target)
    # connect to the serial port of the PC
    sock.connect(target)
    print "OK."

    # call the text input field function  
    bt_typetext()
       
# define the textinput function
def bt_typetext():
    global sock
    # create the text input field
    test = appuifw.query(u"Type words", "text", u"")
    # if cancel has been pressed, then quit the application otherwise send the character over bluetooth
    if test == None:
        exit_key_handler()
    else:
        # send the typed in characters over bluetooth to the PC
        sock.send(test)
        # call again the textinput field function to show the text input again
        bt_typetext()

def exit_key_handler():
    script_lock.signal()
    appuifw.app.set_exit()

appuifw.app.title = u"bt mob to PC"

script_lock = e32.Ao_lock()

appuifw.app.exit_key_handler = exit_key_handler()

# call the function that handles the bluetooth connection
bt_connect()

script_lock.wait()

注意:
确认在你的PC蓝牙连接软件上进行了正确的设置:

  • 鼠标右键点击蓝牙图标 -> 通信 -> 超级终端
  • 选择选项标签,确定”Turn discovery on”被选择
  • 选择 COM 端口标签,确定COM端口“连入”为可用,如果没有,创建一个新的COM端口

Create a Hyper terminal:

  • got to Accessories -> Communications -> Hyperterminal
  • give a name and choose an icon, press ok, then choose in the “connect using” field the COM port of your “incoming port”, press ok
  • make the Hyper teminal active by clicking the “call” button.
  • start now the python script on the phone.
相关文章:
Symbian中同步socket用法
Symbian函数或类与头文件和LIB库对照表
Symbian OS:活动对象与活动调度器(6)
Symbian OS:活动对象与活动调度器(5)
Socket 引擎的设计与实现9
Socket 引擎的设计与实现8
Socket 引擎的设计与实现7
Socket 引擎的设计与实现6