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
36 manages objects and operations for bluetooth connection.
37 Subclass of GerericConnectionManager.
41 GenericConnectionManager.__init__(self)
45 self.client_sock = None
46 self.client_address = None
48 # fast way to create a simple server
49 def create_server(self, protocol, port):
50 self.create_socket(protocol)
56 # fast way to create a simple client
57 def create_client(self, protocol, address, port):
58 self.create_socket(protocol)
59 self.set_address(address)
63 # search for all devices
64 def find_devices(self, time=8):
65 list_devices = bluetooth.discover_devices(lookup_names = True, duration=time)
69 raise BluetoothConnectionError, "Device were not found."
71 # search the device port
72 def find_port(self, addr):
76 services = bluetooth.find_service(address=addr)
77 for i in range(len(services)):
78 port = services[i]['port']
83 raise BluetoothConnectionError, "Port not found."
86 raise BluetoothConnectionError, "Invalid address."
88 # search device services
89 def find_services(self, service=None, addr=None):
90 if service == None and addr == None:
91 list = bluetooth.find_service()
93 elif service != None and addr == None:
94 list = bluetooth.find_service(name=service)
98 raise BluetoothConnectionError, "Name of the service does not exist."
99 elif service == None and addr != None:
100 number = addr.split(":")
101 if(len(number) == 6):
102 list = bluetooth.find_service(address=addr)
106 raise BluetoothConnectionError, "Services not found."
108 raise BluetoothConnectionError, "Invalid address."
109 elif service != None and addr != None:
110 number = addr.split(":")
111 if(len(number) == 6):
112 list = bluetooth.find_service(name=service, address=addr)
116 raise BluetoothConnectionError, "Services not found."
118 raise BluetoothConnectionError, "Invalid address."
121 # search the device indicated by name
122 def find_device_address_by_name(self, device_name):
123 list = bluetooth.discover_devices()
127 if device_name == bluetooth.lookup_name(address):
133 raise BluetoothConnectionError, "Device name not found."
135 # search only device names
136 def find_devices_only_names(self):
137 list = self.find_devices()
139 for address, names in list:
140 list_names += [names]
145 raise BluetoothConnectionError, "Devices were not found."
147 # get the client address
148 def get_client_address(self):
149 return self.client_address
151 # set the port to communicate
152 def set_port(self, port):
155 # get the port to communicate
159 # set the device address
160 def set_address(self, address):
161 aux = address.split(":")
163 self.address = address
165 raise BluetoothConnectionError, "Invalid address."
167 # get the device address
168 def get_address(self):
171 # create a socket with a determinated protocol
172 def create_socket(self, protocol=None):
173 if protocol == 'rfcomm' or protocol == 'RFCOMM':
174 self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
175 elif protocol == 'l2cap' or protocol == 'L2CAP':
176 self.sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
178 raise BluetoothConnectionError, "Undefined Protocol."
180 # bind the communication
182 self.sock.bind(("", self.get_port()))
184 # just listen the tube, only to server
188 # accept the client communication
190 self.client_sock, self.client_address = self.sock.accept()
194 self.sock.connect((self.get_address(), self.get_port()))
196 # send string message
197 def send_message(self, msg=None):
200 # receive string message
201 def received_message(self):
202 return self.client_sock.recv(1024)
206 if self.sock != None and self.client_sock != None:
207 self.client_sock.close()
209 elif self.sock != None and self.client_sock == None:
212 self.client_sock.close()