创建一个应用程序能够让手机通过蓝牙连接到你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.
|