J2ME之家

匿名投稿 投稿指南 RSS订阅 J2ME通告:
搜索: 您的位置主页>Symbian>网络编程>

S60 Python 编程指南——蓝牙连接( 手机到计算机)

2008-8-21 00:21:07 来源:j2me之家整理 责任编辑:本站整理 【 点击: 我要投稿 [进入论坛]

[J2ME之家-Symbian网络编程]

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

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 计算机"

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 计算机
# and lets you send characters to the 计算机

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 计算机
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 计算机

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 计算机"

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()

注意:
确认在你的计算机蓝牙连接软件上进行了 准确的设置:

鼠标右键点击蓝牙图标 -> 通信 -> 超级终端 选择选项标签,确定”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.

Tag:连接 手机 计算机 蓝牙 指南 编程 the to target services
责任编辑:本站整理
  • 用户名: (新注册)密码: 匿名:  请文明参与讨论,禁止漫骂攻击。
    评论总数: [ 查看全部 ] 网友评论
    关于我们 - 联系我们 - 广告服务 - 法律声明 - RSS订阅 - 网站地图 - 返回顶部