PageRenderTime 19ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/simple/rfcomm-server.py

http://pybluez.googlecode.com/
Python | 41 lines | 34 code | 2 blank | 5 comment | 1 complexity | 85ad9b2b3ab1a8d19c0b424eb580e502 MD5 | raw file
 1# file: rfcomm-server.py
 2# auth: Albert Huang <albert@csail.mit.edu>
 3# desc: simple demonstration of a server application that uses RFCOMM sockets
 4#
 5# $Id: rfcomm-server.py 518 2007-08-10 07:20:07Z albert $
 6
 7from bluetooth import *
 8
 9server_sock=BluetoothSocket( RFCOMM )
10server_sock.bind(("",PORT_ANY))
11server_sock.listen(1)
12
13port = server_sock.getsockname()[1]
14
15uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
16
17advertise_service( server_sock, "SampleServer",
18                   service_id = uuid,
19                   service_classes = [ uuid, SERIAL_PORT_CLASS ],
20                   profiles = [ SERIAL_PORT_PROFILE ], 
21#                   protocols = [ OBEX_UUID ] 
22                    )
23                   
24print("Waiting for connection on RFCOMM channel %d" % port)
25
26client_sock, client_info = server_sock.accept()
27print("Accepted connection from ", client_info)
28
29try:
30    while True:
31        data = client_sock.recv(1024)
32        if len(data) == 0: break
33        print("received [%s]" % data)
34except IOError:
35    pass
36
37print("disconnected")
38
39client_sock.close()
40server_sock.close()
41print("all done")