def connect(self, address): if self.__isbound(): raise _socket.error("Can't connect, socket has been bound") elif self.__isconnected(): raise _socket.error("Socket is already connected") _checkaddrpair(address) # open a connection to device self.__remotedevice = _IOBluetooth.IOBluetoothDevice.withAddress_( _macutil.createbtdevaddr(address[0])) if not self.__remotedevice.isConnected(): if self.__timeout is None: result = self.__remotedevice.openConnection() else: result = self.__remotedevice.openConnection_withPageTimeout_authenticationRequired_( None, self.__timeout * 1000, False) if result != _macutil.kIOReturnSuccess: if result == _macutil.kBluetoothHCIErrorPageTimeout: if self.__timeout == 0: raise _socket.error( errno.EAGAIN, "Resource temporarily unavailable") else: raise _socket.timeout("connect timed out") else: raise _socket.error(result, "Cannot connect to %s, can't open connection." \ % str(address[0])) # open RFCOMM or L2CAP channel self.__eventlistener = self.__createlistener() result = self.__conn.connect( self.__remotedevice, address[1], self.__eventlistener) # pass listener as cocoa delegate if result != _macutil.kIOReturnSuccess: self.__remotedevice.closeConnection() self.__stopevents() self.__eventlistener = None raise _socket.error( result, "Cannot connect to %d on %s" % (address[1], address[0])) return # if you don't run the event loop a little here, it's likely you won't # be able to reconnect to the same remote device later _macutil.waitfor(0.5)
def connect(self, address): if self.__isbound(): raise _socket.error("Can't connect, socket has been bound") elif self.__isconnected(): raise _socket.error("Socket is already connected") _checkaddrpair(address) # open a connection to device self.__remotedevice = _IOBluetooth.IOBluetoothDevice.withAddressString_(address[0]) if not self.__remotedevice.isConnected(): if self.__timeout is None: result = self.__remotedevice.openConnection() else: result = self.__remotedevice.openConnection_withPageTimeout_authenticationRequired_( None, self.__timeout*1000, False) if result != _macutil.kIOReturnSuccess: if result == _macutil.kBluetoothHCIErrorPageTimeout: if self.__timeout == 0: raise _socket.error(errno.EAGAIN, "Resource temporarily unavailable") else: raise _socket.timeout("connect timed out") else: raise _socket.error(result, "Cannot connect to %s, can't open connection." \ % str(address[0])) # open RFCOMM or L2CAP channel self.__eventlistener = self.__createlistener() result = self.__conn.connect(self.__remotedevice, address[1], self.__eventlistener) # pass listener as cocoa delegate if result != _macutil.kIOReturnSuccess: self.__remotedevice.closeConnection() self.__stopevents() self.__eventlistener = None raise _socket.error(result, "Cannot connect to %d on %s" % (address[1], address[0])) return # if you don't run the event loop a little here, it's likely you won't # be able to reconnect to the same remote device later _macutil.waitfor(0.5)
def close(self): wasconnected = self.__isconnected() or self.__isbound() self.__stopevents() if self.__conn is not None: if self.__isbound(): self._boundports[self.__conn.proto].discard(self.__port) else: if self.__conn.channel is not None: self.__conn.channel.setDelegate_(None) self.__conn.channel.closeChannel() # disconnect the baseband connection. # This will fail if other RFCOMM channels to the remote device are # still open (which is what we want, cos we don't know if another # process is talking to the device) if self.__remotedevice is not None: self.__remotedevice.closeConnection() # returns err code # if you don't run the event loop a little here, it's likely you won't # be able to reconnect to the same remote device later if wasconnected: _macutil.waitfor(0.5)
# Copyright (c) 2009 Bea Lam. All rights reserved.