PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/simple/rfcomm-client.py

http://pybluez.googlecode.com/
Python | 48 lines | 29 code | 11 blank | 8 comment | 5 complexity | ebf333ffc94aef1fd670a1cb95f98134 MD5 | raw file
 1# file: rfcomm-client.py
 2# auth: Albert Huang <albert@csail.mit.edu>
 3# desc: simple demonstration of a client application that uses RFCOMM sockets
 4#       intended for use with rfcomm-server
 5#
 6# $Id: rfcomm-client.py 424 2006-08-24 03:35:54Z albert $
 7
 8from bluetooth import *
 9import sys
10
11if sys.version < '3':
12    input = raw_input
13
14addr = None
15
16if len(sys.argv) < 2:
17    print("no device specified.  Searching all nearby bluetooth devices for")
18    print("the SampleServer service")
19else:
20    addr = sys.argv[1]
21    print("Searching for SampleServer on %s" % addr)
22
23# search for the SampleServer service
24uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
25service_matches = find_service( uuid = uuid, address = addr )
26
27if len(service_matches) == 0:
28    print("couldn't find the SampleServer service =(")
29    sys.exit(0)
30
31first_match = service_matches[0]
32port = first_match["port"]
33name = first_match["name"]
34host = first_match["host"]
35
36print("connecting to \"%s\" on %s" % (name, host))
37
38# Create the client socket
39sock=BluetoothSocket( RFCOMM )
40sock.connect((host, port))
41
42print("connected.  type stuff")
43while True:
44    data = input()
45    if len(data) == 0: break
46    sock.send(data)
47
48sock.close()