| |
- BluetoothSocket
- DeviceDiscoverer
- exceptions.IOError(exceptions.EnvironmentError)
-
- BluetoothError
class BluetoothSocket |
|
BluetoothSocket(proto=RFCOMM) -> bluetooth socket object
Open a socket of the given protocol. proto must be one of
HCI, L2CAP, RFCOMM, or SCO. SCO sockets have
not been tested at all yet.
A BluetoothSocket object represents one endpoint of a bluetooth connection.
Methods of BluetoothSocket objects (keyword arguments not allowed):
accept() -- accept a connection, returning new socket and client address
bind(addr) -- bind the socket to a local address
close() -- close the socket
connect(addr) -- connect the socket to a remote address
connect_ex(addr) -- connect, return an error code instead of an exception
dup() -- return a new socket object identical to the current one
fileno() -- return underlying file descriptor
getpeername() -- return remote address
getsockname() -- return local address
getsockopt(level, optname[, buflen]) -- get socket options
gettimeout() -- return timeout or None
listen(n) -- start listening for incoming connections
makefile([mode, [bufsize]]) -- return a file object for the socket
recv(buflen[, flags]) -- receive data
recvfrom(buflen[, flags]) -- receive data and sender's address
sendall(data[, flags]) -- send all data
send(data[, flags]) -- send data, may not send all of it
sendto(data[, flags], addr) -- send data to a given address
setblocking(0 | 1) -- set or clear the blocking I/O flag
setsockopt(level, optname, value) -- set socket options
settimeout(None | float) -- set or clear the timeout
shutdown(how) -- shut down traffic in one or both directions |
|
Methods defined here:
- __init__(self, proto=3, _sock=None)
- accept(self)
- accept() -> (socket object, address info)
Wait for an incoming connection. Return a new socket representing the
connection, and the address of the client. For L2CAP sockets, the address
is a (host, psm) tuple. For RFCOMM sockets, the address is a (host, channel)
tuple. For SCO sockets, the address is just a host.
- bind(self, *args, **kwargs)
- bind(address)
Bind the socket to a local address. address must always be a tuple.
HCI sockets: ( device number, )
device number should be 0, 1, 2, etc.
L2CAP sockets: ( host, psm )
host should be an address e.g. "01:23:45:67:89:ab"
psm should be an unsigned integer
RFCOMM sockets: ( host, channel )
SCO sockets: ( host )
- close(self, *args, **kwargs)
- close()
Close the socket. It cannot be used after this call.
- connect(self, *args, **kwargs)
- connect(address)
Connect the socket to a remote address. For L2CAP sockets, the address is a
(host,psm) tuple. For RFCOMM sockets, the address is a (host,channel) tuple.
For SCO sockets, the address is just the host.
- connect_ex(self, *args, **kwargs)
- connect_ex(address) -> errno
This is like connect(address), but returns an error code (the errno value)
instead of raising an exception when an error occurs.
- dup(self, *args, **kwargs)
- dup() -> socket object
Return a new socket object connected to the same system resource.
- fileno(self, *args, **kwargs)
- fileno() -> integer
Return the integer file descriptor of the socket.
- getpeername(self, *args, **kwargs)
- getpeername() -> address info
Return the address of the remote endpoint. For HCI sockets, the address is a
device number (0, 1, 2, etc). For L2CAP sockets, the address is a
(host,psm) tuple. For RFCOMM sockets, the address is a (host,channel) tuple.
For SCO sockets, the address is just the host.
- getsockname(self, *args, **kwargs)
- getsockname() -> address info
Return the address of the local endpoint.
- getsockopt(self, *args, **kwargs)
- getsockopt(level, option[, buffersize]) -> value
Get a socket option. See the Unix manual for level and option.
If a nonzero buffersize argument is given, the return value is a
string of that length; otherwise it is an integer.
- gettimeout(self, *args, **kwargs)
- gettimeout() -> timeout
Returns the timeout in floating seconds associated with socket
operations. A timeout of None indicates that timeouts on socket
operations are disabled.
- listen(self, *args, **kwargs)
- listen(backlog)
Enable a server to accept connections. The backlog argument must be at
least 1; it specifies the number of unaccepted connection that the system
will allow before refusing new connections.
- makefile(self, *args, **kwargs)
- makefile([mode[, buffersize]]) -> file object
Return a regular file object corresponding to the socket.
The mode and buffersize arguments are as for the built-in open() function.
- recv(self, *args, **kwargs)
- recv(buffersize[, flags]) -> data
Receive up to buffersize bytes from the socket. For the optional flags
argument, see the Unix manual. When no data is available, block until
at least one byte is available or until the remote end is closed. When
the remote end is closed and all data is read, return the empty string.
- recvfrom(self, *args, **kwargs)
- recvfrom(buffersize[, flags]) -> (data, address info)
Like recv(buffersize, flags) but also return the sender's address info.
- send(self, *args, **kwargs)
- send(data[, flags]) -> count
Send a data string to the socket. For the optional flags
argument, see the Unix manual. Return the number of bytes
sent; this may be less than len(data) if the network is busy.
- sendall(self, *args, **kwargs)
- sendall(data[, flags])
Send a data string to the socket. For the optional flags
argument, see the Unix manual. This calls send() repeatedly
until all data is sent. If an error occurs, it's impossible
to tell how much data has been sent.
- sendto(self, *args, **kwargs)
- sendto(data[, flags], address) -> count
Like send(data, flags) but allows specifying the destination address.
For IP sockets, the address is a pair (hostaddr, port).
- setblocking(self, *args, **kwargs)
- setblocking(flag)
Set the socket to blocking (flag is true) or non-blocking (false).
setblocking(True) is equivalent to settimeout(None);
setblocking(False) is equivalent to settimeout(0.0).
- setsockopt(self, *args, **kwargs)
- setsockopt(level, option, value)
Set a socket option. See the Unix manual for level and option.
The value argument can either be an integer or a string.
- settimeout(self, *args, **kwargs)
- settimeout(timeout)
Set a timeout on socket operations. 'timeout' can be a float,
giving in seconds, or None. Setting a timeout of None disables
the timeout feature and is equivalent to setblocking(1).
Setting a timeout of zero is the same as setblocking(0).
- shutdown(self, *args, **kwargs)
- shutdown(flag)
Shut down the reading side of the socket (flag == 0), the writing side
of the socket (flag == 1), or both ends (flag == 2).
|
class DeviceDiscoverer |
|
Skeleton class for finer control of the device discovery process.
To implement asynchronous device discovery (e.g. if you want to do
something *as soon as* a device is discovered), subclass DeviceDiscoverer
and override device_discovered() and inquiry_complete() |
|
Methods defined here:
- __init__(self)
- cancel_inquiry(self)
- Call this method to cancel an inquiry in process. inquiry_complete
will still be called.
- device_discovered(self, address, device_class, name)
- Called when a bluetooth device is discovered.
address is the bluetooth address of the device
device_class is the Class of Device, as specified in [1]
passed in as a 3-byte string
name is the user-friendly name of the device if lookup_names was set
when the inquiry was started. otherwise None
This method exists to be overriden.
[1] https://www.bluetooth.org/foundry/assignnumb/document/baseband
- fileno(self)
- find_devices(self, lookup_names=True, duration=8, flush_cache=True)
- start_inquiry( lookup_names=True, service_name=None,
duration=8, flush_cache=True )
Call this method to initiate the device discovery process
lookup_names - set to True if you want to lookup the user-friendly
names for each device found.
service_name - set to the name of a service you're looking for.
only devices with a service of this name will be
returned in device_discovered() NOT YET IMPLEMENTED
ADVANCED PARAMETERS: (don't change these unless you know what
you're doing)
duration - the number of 1.2 second units to spend searching for
bluetooth devices. If lookup_names is True, then the
inquiry process can take a lot longer.
flush_cache - return devices discovered in previous inquiries
- inquiry_complete(self)
- Called when an inquiry started by start_inquiry has completed.
- pre_inquiry(self)
- Called just after start_inquiry is invoked, but just before the inquiry
is started.
This method exists to be overriden
- process_event(self)
- Waits for one event to happen, and proceses it. The event will be
either a device discovery, or an inquiry completion.
- process_inquiry(self)
- Repeatedly calls process_event() until the device inquiry has
completed.
| |