/bluetooth/__init__.py

http://pybluez.googlecode.com/ · Python · 149 lines · 103 code · 10 blank · 36 comment · 17 complexity · 70b734c4340e46ef4d9a58c35d53c567 MD5 · raw file

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