diff options
Diffstat (limited to 'pybluez/simple')
| -rw-r--r-- | pybluez/simple/asynchronous-inquiry.py | 65 | ||||
| -rw-r--r-- | pybluez/simple/inquiry.py | 17 | ||||
| -rw-r--r-- | pybluez/simple/l2capclient.py | 28 | ||||
| -rw-r--r-- | pybluez/simple/l2capserver.py | 32 | ||||
| -rw-r--r-- | pybluez/simple/rfcomm-client.py | 45 | ||||
| -rw-r--r-- | pybluez/simple/rfcomm-server.py | 41 | ||||
| -rw-r--r-- | pybluez/simple/sdp-browse.py | 35 |
7 files changed, 263 insertions, 0 deletions
diff --git a/pybluez/simple/asynchronous-inquiry.py b/pybluez/simple/asynchronous-inquiry.py new file mode 100644 index 0000000..abbd70c --- /dev/null +++ b/pybluez/simple/asynchronous-inquiry.py @@ -0,0 +1,65 @@ +# file: asynchronous-inquiry.py +# auth: Albert Huang <albert@csail.mit.edu> +# desc: demonstration of how to do asynchronous device discovery by subclassing +# the DeviceDiscoverer class +# $Id: asynchronous-inquiry.py 405 2006-05-06 00:39:50Z albert $ +# +# XXX Linux only (5/5/2006) + +import bluetooth +import select + +class MyDiscoverer(bluetooth.DeviceDiscoverer): + + def pre_inquiry(self): + self.done = False + + def device_discovered(self, address, device_class, name): + print "%s - %s" % (address, name) + + # get some information out of the device class and display it. + # voodoo magic specified at: + # + # https://www.bluetooth.org/foundry/assignnumb/document/baseband + major_classes = ( "Miscellaneous", + "Computer", + "Phone", + "LAN/Network Access point", + "Audio/Video", + "Peripheral", + "Imaging" ) + major_class = (device_class >> 8) & 0xf + if major_class < 7: + print " %s" % major_classes[major_class] + else: + print " Uncategorized" + + print " services:" + service_classes = ( (16, "positioning"), + (17, "networking"), + (18, "rendering"), + (19, "capturing"), + (20, "object transfer"), + (21, "audio"), + (22, "telephony"), + (23, "information")) + + for bitpos, classname in service_classes: + if device_class & (1 << (bitpos-1)): + print " %s" % classname + + def inquiry_complete(self): + self.done = True + +d = MyDiscoverer() +d.find_devices(lookup_names = True) + +readfiles = [ d, ] + +while True: + rfds = select.select( readfiles, [], [] )[0] + + if d in rfds: + d.process_event() + + if d.done: break diff --git a/pybluez/simple/inquiry.py b/pybluez/simple/inquiry.py new file mode 100644 index 0000000..8f77887 --- /dev/null +++ b/pybluez/simple/inquiry.py @@ -0,0 +1,17 @@ +# file: inquiry.py +# auth: Albert Huang <albert@csail.mit.edu> +# desc: performs a simple device inquiry followed by a remote name request of +# each discovered device +# $Id: inquiry.py 401 2006-05-05 19:07:48Z albert $ +# + +import bluetooth + +print "performing inquiry..." + +nearby_devices = bluetooth.discover_devices(lookup_names = True) + +print "found %d devices" % len(nearby_devices) + +for addr, name in nearby_devices: + print " %s - %s" % (addr, name) diff --git a/pybluez/simple/l2capclient.py b/pybluez/simple/l2capclient.py new file mode 100644 index 0000000..930e431 --- /dev/null +++ b/pybluez/simple/l2capclient.py @@ -0,0 +1,28 @@ +# file: l2capclient.py +# desc: Demo L2CAP client for bluetooth module. +# $Id: l2capclient.py 524 2007-08-15 04:04:52Z albert $ + +import sys +import bluetooth + +sock=bluetooth.BluetoothSocket(bluetooth.L2CAP) + +if len(sys.argv) < 2: + print "usage: l2capclient.py <addr>" + sys.exit(2) + +bt_addr=sys.argv[1] +port = 0x1001 + +print "trying to connect to %s on PSM 0x%X" % (bt_addr, port) + +sock.connect((bt_addr, port)) + +print "connected. type stuff" +while True: + data = raw_input() + if(len(data) == 0): break + sock.send(data) + +sock.close() + diff --git a/pybluez/simple/l2capserver.py b/pybluez/simple/l2capserver.py new file mode 100644 index 0000000..40ad7d6 --- /dev/null +++ b/pybluez/simple/l2capserver.py @@ -0,0 +1,32 @@ +# file: l2capclient.py +# desc: Demo L2CAP server for pybluez. +# $Id: l2capserver.py 524 2007-08-15 04:04:52Z albert $ + +import bluetooth + +server_sock=bluetooth.BluetoothSocket( bluetooth.L2CAP ) + +port = 0x1001 + +server_sock.bind(("",port)) +server_sock.listen(1) + +#uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ef" +#bluetooth.advertise_service( server_sock, "SampleServerL2CAP", +# service_id = uuid, +# service_classes = [ uuid ] +# ) + +client_sock,address = server_sock.accept() +print "Accepted connection from ",address + +data = client_sock.recv(1024) +print "Data received:", data + +while data: + client_sock.send('Echo =>' + data) + data = client_sock.recv(1024) + print "Data received:",data + +client_sock.close() +server_sock.close() diff --git a/pybluez/simple/rfcomm-client.py b/pybluez/simple/rfcomm-client.py new file mode 100644 index 0000000..4da12af --- /dev/null +++ b/pybluez/simple/rfcomm-client.py @@ -0,0 +1,45 @@ +# file: rfcomm-client.py +# auth: Albert Huang <albert@csail.mit.edu> +# desc: simple demonstration of a client application that uses RFCOMM sockets +# intended for use with rfcomm-server +# +# $Id: rfcomm-client.py 424 2006-08-24 03:35:54Z albert $ + +from bluetooth import * +import sys + +addr = None + +if len(sys.argv) < 2: + print "no device specified. Searching all nearby bluetooth devices for" + print "the SampleServer service" +else: + addr = sys.argv[1] + print "Searching for SampleServer on %s" % addr + +# search for the SampleServer service +uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" +service_matches = find_service( uuid = uuid, address = addr ) + +if len(service_matches) == 0: + print "couldn't find the SampleServer service =(" + sys.exit(0) + +first_match = service_matches[0] +port = first_match["port"] +name = first_match["name"] +host = first_match["host"] + +print "connecting to \"%s\" on %s" % (name, host) + +# Create the client socket +sock=BluetoothSocket( RFCOMM ) +sock.connect((host, port)) + +print "connected. type stuff" +while True: + data = raw_input() + if len(data) == 0: break + sock.send(data) + +sock.close() diff --git a/pybluez/simple/rfcomm-server.py b/pybluez/simple/rfcomm-server.py new file mode 100644 index 0000000..1fb555f --- /dev/null +++ b/pybluez/simple/rfcomm-server.py @@ -0,0 +1,41 @@ +# file: rfcomm-server.py +# auth: Albert Huang <albert@csail.mit.edu> +# desc: simple demonstration of a server application that uses RFCOMM sockets +# +# $Id: rfcomm-server.py 518 2007-08-10 07:20:07Z albert $ + +from bluetooth import * + +server_sock=BluetoothSocket( RFCOMM ) +server_sock.bind(("",PORT_ANY)) +server_sock.listen(1) + +port = server_sock.getsockname()[1] + +uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" + +advertise_service( server_sock, "SampleServer", + service_id = uuid, + service_classes = [ uuid, SERIAL_PORT_CLASS ], + profiles = [ SERIAL_PORT_PROFILE ], +# protocols = [ OBEX_UUID ] + ) + +print "Waiting for connection on RFCOMM channel %d" % port + +client_sock, client_info = server_sock.accept() +print "Accepted connection from ", client_info + +try: + while True: + data = client_sock.recv(1024) + if len(data) == 0: break + print "received [%s]" % data +except IOError: + pass + +print "disconnected" + +client_sock.close() +server_sock.close() +print "all done" diff --git a/pybluez/simple/sdp-browse.py b/pybluez/simple/sdp-browse.py new file mode 100644 index 0000000..9d24b7a --- /dev/null +++ b/pybluez/simple/sdp-browse.py @@ -0,0 +1,35 @@ +# file: sdp-browse.py +# auth: Albert Huang <albert@csail.mit.edu> +# desc: displays services being advertised on a specified bluetooth device +# $Id: sdp-browse.py 393 2006-02-24 20:30:15Z albert $ + +import sys +import bluetooth + +if len(sys.argv) < 2: + print "usage: sdp-browse <addr>" + print " addr can be a bluetooth address, \"localhost\", or \"all\"" + sys.exit(2) + +target = sys.argv[1] +if target == "all": target = None + +services = bluetooth.find_service(address=target) + +if len(services) > 0: + print "found %d services on %s" % (len(services), sys.argv[1]) + print +else: + print "no services found" + +for svc in services: + print "Service Name: %s" % svc["name"] + print " Host: %s" % svc["host"] + print " Description: %s" % svc["description"] + print " Provided By: %s" % svc["provider"] + print " Protocol: %s" % svc["protocol"] + print " channel/PSM: %s" % svc["port"] + print " svc classes: %s "% svc["service-classes"] + print " profiles: %s "% svc["profiles"] + print " service id: %s "% svc["service-id"] + print |
