1 # -*- coding: utf-8 -*-
3 # ****************************************************************************
4 # Copyright (c) 2008 INdT/Fucapi.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # ============================================================================
19 # Project Name : PC Remote
20 # Author : Jônatas Isvi
21 # Email : jonatas.nona@gmail.com
25 # Package : connection
26 # Description : BluetoothConnectionManager
27 # ============================================================================
30 from exceptions import *
31 from genericconnectionmanager import *
33 class BluetoothConnectionManager(GenericConnectionManager):
35 """ BluetoothConnectionManager
37 manages objects and operations for bluetooth connection.
38 Subclass of GerericConnectionManager.
42 GenericConnectionManager.__init__(self)
46 self.client_sock = None
47 self.client_address = None
49 # fast way to create a simple server
50 def create_server(self, protocol, port):
51 self.create_socket(protocol)
57 # fast way to create a simple client
58 def create_client(self, protocol, address, port):
59 self.create_socket(protocol)
60 self.set_address(address)
64 # search for all devices
65 def find_devices(self, time=8):
66 list_devices = bluetooth.discover_devices(lookup_names = True, duration=time)
71 raise BluetoothConnectionError, "Device were not found."
73 # search the device port
74 def find_port(self, addr):
78 services = bluetooth.find_service(address=addr)
79 for i in range(len(services)):
80 port = services[i]['port']
85 raise BluetoothConnectionError, "Port not found."
88 raise BluetoothConnectionError, "Invalid address."
90 # search device services
91 def find_services(self, service=None, addr=None):
92 if service == None and addr == None:
93 list = bluetooth.find_service()
95 elif service != None and addr == None:
96 list = bluetooth.find_service(name=service)
100 raise BluetoothConnectionError, "Name of the service does not exist."
101 elif service == None and addr != None:
102 number = addr.split(":")
103 if(len(number) == 6):
104 list = bluetooth.find_service(address=addr)
108 raise BluetoothConnectionError, "Services not found."
110 raise BluetoothConnectionError, "Invalid address."
111 elif service != None and addr != None:
112 number = addr.split(":")
113 if(len(number) == 6):
114 list = bluetooth.find_service(name=service, address=addr)
118 raise BluetoothConnectionError, "Services not found."
120 raise BluetoothConnectionError, "Invalid address."
123 # search the device indicated by name
124 def find_device_address_by_name(self, device_name):
125 list = bluetooth.discover_devices()
129 if device_name == bluetooth.lookup_name(address):
135 raise BluetoothConnectionError, "Device name not found."
137 # search only device names
138 def find_devices_only_names(self):
139 list = self.find_devices()
141 for address, names in list:
142 list_names += [names]
147 raise BluetoothConnectionError, "Devices were not found."
149 # get the client address
150 def get_client_address(self):
151 return self.client_address
153 # set the port to communicate
154 def set_port(self, port):
157 # get the port to communicate
161 # set the device address
162 def set_address(self, address):
163 aux = address.split(":")
165 self.address = address
167 raise BluetoothConnectionError, "Invalid address."
169 # get the device address
170 def get_address(self):
173 # create a socket with a determinated protocol
174 def create_socket(self, protocol=None):
175 if protocol == 'rfcomm' or protocol == 'RFCOMM':
176 self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
177 elif protocol == 'l2cap' or protocol == 'L2CAP':
178 self.sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
180 raise BluetoothConnectionError, "Undefined Protocol."
182 # bind the communication
184 self.sock.bind(("", self.get_port()))
186 # just listen the tube, only to server
190 # accept the client communication
192 self.client_sock, self.client_address = self.sock.accept()
196 self.sock.connect((self.get_address(), self.get_port()))
198 # send string message
199 def send_message(self, msg=None):
202 # receive string message
203 def received_message(self):
204 return self.client_sock.recv(1024)
208 if self.sock != None and self.client_sock != None:
209 self.client_sock.close()
211 elif self.sock != None and self.client_sock == None:
214 self.client_sock.close()