| | 7 | |
| | 8 | class BtServerSocket( bluetooth.BluetoothSocket ): |
| | 9 | def __init__(self, aMac="", aPort=bluetooth.PORT_ANY): |
| | 10 | bluetooth.BluetoothSocket.__init__( self, bluetooth.RFCOMM ) |
| | 11 | self.bind( (aMac, aPort) ) |
| | 12 | self.listen(1) |
| | 13 | |
| | 14 | if aPort == bluetooth.PORT_ANY: |
| | 15 | bluetooth.port = self.getsockname()[1] |
| | 16 | |
| | 17 | uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" |
| | 18 | |
| | 19 | bluetooth.advertise_service( self, "myMRC", |
| | 20 | service_id = uuid, |
| | 21 | service_classes = [ uuid, bluetooth.SERIAL_PORT_CLASS ], |
| | 22 | profiles = [ bluetooth.SERIAL_PORT_PROFILE ] ) |
| | 23 | |
| | 24 | self._iLock = threading.Semaphore() |
| | 25 | self._iClientStack = [] |
| | 26 | |
| | 27 | printd("Bt interface: %s"%(repr(self.getsockname()))) |
| | 28 | |
| | 29 | ## Add instance to the array. |
| | 30 | # @param self: The object pointer. |
| | 31 | # @param aChannel: Channel to had the the array. |
| | 32 | def _addToStack( self, aClient ): |
| | 33 | self._iLock.acquire( ) |
| | 34 | self._iClientStack.append( aClient ) |
| | 35 | self._iLock.release( ) |
| | 36 | printd( "Client added to stack") |
| | 37 | |
| | 38 | ## Handle incomming connection request. |
| | 39 | # @param self: The object pointer. |
| | 40 | def _connectionHandler( self ): |
| | 41 | printd("Wait for incoming connection") |
| | 42 | while True: |
| | 43 | try: |
| | 44 | client = Client(self.accept()) |
| | 45 | self._addToStack( client ) |
| | 46 | except Exception, e: |
| | 47 | printd("BT interface busy: %s", repr(e)) |
| | 48 | |
| | 49 | |
| | 50 | ## Listen interface for incomming connection. |
| | 51 | # @param self The object pointer. |
| | 52 | def start( self ): |
| | 53 | thread.start_new_thread(self._connectionHandler, () ) |
| | 54 | |
| | 55 | def __del__(self): |
| | 56 | for client in self._iClientStack: |
| | 57 | self._removeFromStack(client) |
| | 58 | del client |