PageRenderTime 19ms CodeModel.GetById 11ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/bluetooth/__init__.py

http://pybluez.googlecode.com/
Python | 149 lines | 116 code | 8 blank | 25 comment | 10 complexity | 70b734c4340e46ef4d9a58c35d53c567 MD5 | raw file
  1import sys
  2import os
  3if sys.version < '3':
  4    from .btcommon import *
  5else:
  6    from bluetooth.btcommon import *
  7
  8__version__ = 0.19
  9
 10def _dbg(*args):
 11    return
 12    sys.stderr.write(*args)
 13    sys.stderr.write("\n")
 14
 15if sys.platform == "win32":
 16    _dbg("trying widcomm")
 17    have_widcomm = False
 18    dll = "wbtapi.dll"
 19    sysroot = os.getenv ("SystemRoot")
 20    if os.path.exists (dll) or \
 21       os.path.exists (os.path.join (sysroot, "system32", dll)) or \
 22       os.path.exists (os.path.join (sysroot, dll)):
 23        try:
 24            from . import widcomm
 25            if widcomm.inquirer.is_device_ready ():
 26                # if the Widcomm stack is active and a Bluetooth device on that
 27                # stack is detected, then use the Widcomm stack
 28                from .widcomm import *
 29                have_widcomm = True
 30        except ImportError: 
 31            pass
 32
 33    if not have_widcomm:
 34        # otherwise, fall back to the Microsoft stack
 35        _dbg("Widcomm not ready. falling back to MS stack")
 36        if sys.version < '3':
 37            from .msbt import *
 38        else:
 39            from bluetooth.msbt import *
 40
 41elif sys.platform.startswith("linux"):
 42    if sys.version < '3':
 43        from .bluez import *
 44    else:
 45        from bluetooth.bluez import *
 46elif sys.platform == "darwin":
 47    from .osx import *
 48else:
 49    raise Exception("This platform (%s) is currently not supported by pybluez." % sys.platform)
 50
 51discover_devices.__doc__ = \
 52    """
 53    performs a bluetooth device discovery using the first available bluetooth
 54    resource.
 55
 56    if lookup_names is False, returns a list of bluetooth addresses.
 57    if lookup_names is True, returns a list of (address, name) tuples
 58
 59    lookup_names=False
 60        if set to True, then discover_devices also attempts to lookup the
 61        display name of each detected device.
 62
 63    if lookup_class is True, the class of the device is added to the tuple
 64    """
 65
 66lookup_name.__doc__ = \
 67    """
 68    Tries to determine the friendly name (human readable) of the device with
 69    the specified bluetooth address.  Returns the name on success, and None
 70    on failure.
 71    """
 72
 73advertise_service.__doc__ = \
 74    """
 75    Advertises a service with the local SDP server.  sock must be a bound,
 76    listening socket.  name should be the name of the service, and service_id 
 77    (if specified) should be a string of the form 
 78    "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", where each 'X' is a hexadecimal
 79    digit.
 80
 81    service_classes is a list of service classes whose this service belongs to.
 82    Each class service is a 16-bit UUID in the form "XXXX", where each 'X' is a
 83    hexadecimal digit, or a 128-bit UUID in the form
 84    "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX". There are some constants for
 85    standard services, e.g. SERIAL_PORT_CLASS that equals to "1101". Some class
 86    constants:
 87
 88    SERIAL_PORT_CLASS        LAN_ACCESS_CLASS         DIALUP_NET_CLASS 
 89    HEADSET_CLASS            CORDLESS_TELEPHONY_CLASS AUDIO_SOURCE_CLASS
 90    AUDIO_SINK_CLASS         PANU_CLASS               NAP_CLASS
 91    GN_CLASS
 92
 93    profiles is a list of service profiles that thie service fulfills. Each
 94    profile is a tuple with ( uuid, version). Most standard profiles use
 95    standard classes as UUIDs. PyBluez offers a list of standard profiles,
 96    for example SERIAL_PORT_PROFILE. All standard profiles have the same
 97    name as the classes, except that _CLASS suffix is replaced by _PROFILE.
 98
 99    provider is a text string specifying the provider of the service
100
101    description is a text string describing the service
102
103    A note on working with Symbian smartphones:
104        bt_discover in Python for Series 60 will only detect service records
105        with service class SERIAL_PORT_CLASS and profile SERIAL_PORT_PROFILE
106
107    """
108
109stop_advertising.__doc__ = \
110    """
111    Instructs the local SDP server to stop advertising the service associated
112    with sock.  You should typically call this right before you close sock.
113    """
114
115find_service.__doc__ = \
116    """
117    find_service (name = None, uuid = None, address = None)
118
119    Searches for SDP services that match the specified criteria and returns
120    the search results.  If no criteria are specified, then returns a list of
121    all nearby services detected.  If more than one is specified, then
122    the search results will match all the criteria specified.  If uuid is
123    specified, it must be either a 16-bit UUID in the form "XXXX", where each
124    'X' is a hexadecimal digit, or as a 128-bit UUID in the form
125    "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".  A special case of address is
126    "localhost", which will search for services on the local machine.
127
128    The search results will be a list of dictionaries.  Each dictionary
129    represents a search match and will have the following key/value pairs:
130
131      host          - the bluetooth address of the device advertising the
132                      service
133      name          - the name of the service being advertised
134      description   - a description of the service being advertised
135      provider      - the name of the person/organization providing the service
136      protocol      - either 'RFCOMM', 'L2CAP', None if the protocol was not
137                      specified, or 'UNKNOWN' if the protocol was specified but
138                      unrecognized
139      port          - the L2CAP PSM # if the protocol is 'L2CAP', the RFCOMM
140                      channel # if the protocol is 'RFCOMM', or None if it
141                      wasn't specified
142      service-classes - a list of service class IDs (UUID strings).  possibly
143                        empty
144      profiles        - a list of profiles - (UUID, version) pairs - the
145                        service claims to support.  possibly empty.
146      service-id      - the Service ID of the service.  None if it wasn't set
147                        See the Bluetooth spec for the difference between
148                        Service ID and Service Class ID List
149    """