1. gao shuai
  2. wd_pys60

Source

wd_pys60 / wd_rfcomm_listenr.py

import socket, appuifw

def chat_server():
    server = socket.socket(socket.AF_BT, socket.SOCK_STREAM)
    channel = socket.bt_rfcomm_get_available_server_channel(server)
    server.bind(("457807c0-4897-11df-9879-0800200c9a66", channel))
    server.listen(1)
    socket.bt_advertise_service(u"btchat", server, True, socket.RFCOMM)
    socket.set_security(server, socket.AUTH | socket.AUTHOR)
    print "Waiting for clients..."
    conn, client_addr = server.accept()
    print "Client connected!"
    talk(None, conn)


def receive_msg(fd):
        print "Waiting for message.."
        reply = fd.readline()
        print "Received: " + reply
        appuifw.note(unicode(reply), "info")


def talk(client, server):
        try:
                if server:
                        fd = server.makefile("rw", 0)
                        fd.write("hello\n")
                while True:
                        receive_msg(fd)
        except Exception,t:
                appuifw.note(u"Connection lost", "info")
                print t
                if server: server.close()
                print "Bye!"

chat_server()