Module btpycom :: Class BTClient
[hide private]
[frames] | no frames]

Class BTClient

source code

Class that represents a Bluetooth socket based client. To connect a client via Bluetooth, the Bluetooth MAC address (12 hex characters) and the Bluetooth port (channel) of the server must be known (server info). All other server information is irrelevant (Bluetooth friendly name, Bluetooth service name), but the server info may be retrieved by a Bluetooth search from the server name or the service name.

Instance Methods [hide private]
 
__init__(self, stateChanged, isVerbose=False)
Creates a Bluetooth client prepared for a connection with a BTServer.
source code
 
sendMessage(self, msg)
Sends the information msg to the server (as String, the character (ASCII 0) serves as end of string indicator, it is transparently added and removed).
source code
 
connect(self, serverInfo, timeout)
Performs a connection trial to the server with given serverInfo.
source code
 
_connect(self, serverInfo) source code
 
findServer(self, serverName, timeout)
Perform a device inquiry for the given server name.
source code
 
_inquireMAC(self) source code
 
findService(self, serviceName, timeout)
Perform a service inquiry for the given service name.
source code
 
_inquireService(self) source code
 
disconnect(self, endOfTransmission="")
Closes the connection with the server.
source code
 
isConnected(self)
Returns True of client is connnected to the server.
source code
 
getMacAddress(self)
Returns the Bluetooth MAC address in format "nn:nn:nn:nn:nn::nn"
source code
 
getChannel(self)
Returns the Bluetooth channel.
source code
Static Methods [hide private]
 
debug(msg) source code
 
getVersion()
Returns the library version.
source code
Class Variables [hide private]
  isVerbose = False
  CONNECTING = "CONNECTING"
  CONNECTION_FAILED = "CONNECTION_FAILED"
  CONNECTED = "CONNECTED"
  DISCONNECTED = "DISCONNECTED"
  MESSAGE = "MESSAGE"
Method Details [hide private]

__init__(self, stateChanged, isVerbose=False)
(Constructor)

source code 

Creates a Bluetooth client prepared for a connection with a BTServer. The client uses an internal handler thread to detect incoming messages. State changes invoke the callback onStateChanged(state, msg) where state is one of the following strings: "CONNECTING", "CONNECTION_FAILED", "DISCONNECTED", "MESSAGE". msg is a string with additional information: CONNECTING: serverInfo, CONNECTED: serverInfo, CONNECTION_FAILED: serverInfo, DISCONNECTED: empty, MESSAGE: message received

Parameters:
  • stateChanged - the callback function to register
  • isVerbose - if true, debug messages are written to System.out

sendMessage(self, msg)

source code 

Sends the information msg to the server (as String, the character (ASCII 0) serves as end of string indicator, it is transparently added and removed).

Parameters:
  • msg - the message to send

connect(self, serverInfo, timeout)

source code 

Performs a connection trial to the server with given serverInfo. If the connection trial fails, it is repeated until the timeout is reached.

Parameters:
  • serverInfo - a tuple ("nn:nn:nn:nn:nn:nn", channel) with the Bluetooth MAC address string (12 hex characters, upper or lower case, separated by :) and Bluetooth channel (integer), e.g. ("B8:27:EB:04:A6:7E", 1)

findServer(self, serverName, timeout)

source code 

Perform a device inquiry for the given server name. The Bluetooth channel is not inquired, always assumed to be 1.

Parameters:
  • timeout - the maximum time in seconds to search
Returns:
a tuple with the Bluetooth MAC address and the Bluetooth channel 1; None if inquiry failed

findService(self, serviceName, timeout)

source code 

Perform a service inquiry for the given service name.

Parameters:
  • timeout - the maximum time in seconds to search
Returns:
a tuple with the Bluetooth MAC address and the Bluetooth channel; None if inquiry failed

disconnect(self, endOfTransmission="")

source code 

Closes the connection with the server. The endOfTransmission message is sent to the server to notify disconnection.

Parameters:
  • endOfTransmission - the message sent to the server (appended by ) Default: "", None: no notification sent

isConnected(self)

source code 

Returns True of client is connnected to the server.

Returns:
True, if the connection is established

getVersion()
Static Method

source code 

Returns the library version.

Returns:
the current version of the library