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
23 # Description : BluetoothConnectionManager
24 # ============================================================================
28 from exceptions import *
29 from genericconnectionmanager import *
31 class BluetoothConnectionError(Exception):
34 class BluetoothConnectionManager(GenericConnectionManager):
37 GenericConnectionManager.__init__(self)
38 print "BluetoothConnectionManager iniciado."
39 # globals data variables
43 self.client_sock = None
44 self.client_address = None
46 # fast way to create a simple server
47 def create_server(self, protocol, port):
48 self.create_socket(protocol)
54 # fast way to create a simple client
55 def create_client(self, protocol, address, port):
56 self.create_socket(protocol)
57 self.set_address(address)
61 # search for all devices
62 def find_devices(self, time=8):
63 list_devices = bluetooth.discover_devices(lookup_names = True, duration=time)
67 raise BluetoothConnectionError, "Device were not found."
69 # search the device port
70 def find_port(self, addr):
74 services = bluetooth.find_service(address=addr)
75 for i in range(len(services)):
76 port = services[i]['port']
81 raise BluetoothConnectionError, "Port not found."
84 raise BluetoothConnectionError, "Invalid address."
86 # search device services
87 def find_services(self, service=None, addr=None):
88 if service == None and addr == None:
89 list = bluetooth.find_service()
90 # returns all services
92 elif service != None and addr == None:
93 list = bluetooth.find_service(name=service)
94 # returns only the device services indicated by name
98 raise BluetoothConnectionError, "Name of the service does not exist."
99 elif service == None and addr != None:
100 number = addr.split(":")
101 # returns only the device services indicated by address
102 if(len(number) == 6):
103 list = bluetooth.find_service(address=addr)
107 raise BluetoothConnectionError, "Services not found."
109 raise BluetoothConnectionError, "Invalid address."
110 elif service != None and addr != None:
111 number = addr.split(":")
112 # returns only the device service indicated by address
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."
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
193 self.client_sock, self.client_address = self.sock.accept()
197 self.sock.connect((self.get_address(), self.get_port()))
199 # send string message
200 def send_message(self, msg=None):
203 # receive string message
204 def received_message(self):
205 return self.client_sock.recv(1024)
209 if self.sock != None and self.client_sock != None:
210 self.client_sock.close()
212 elif self.sock != None and self.client_sock == None:
215 self.client_sock.close()