1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
"""
Shows how to run a RFCOMM server socket.
"""
import lightblue
# create and set up server socket
sock = lightblue.socket()
sock.bind(("", 0)) # bind to 0 to bind to a dynamically assigned channel
sock.listen(1)
lightblue.advertise("EchoService", sock, lightblue.RFCOMM)
print "Advertised and listening on channel %d..." % sock.getsockname()[1]
conn, addr = sock.accept()
print "Connected by", addr
data = conn.recv(1024)
print "Echoing received data:", data
conn.send(data)
# sometimes the data isn't sent if the connection is closed immediately after
# the call to send(), so wait a second
import time
time.sleep(1)
conn.close()
sock.close()
|