Ejemplo n.º 1
0
def btaddrtochars(addr):
    """
    Takes a bluetooth address and returns a tuple with the corresponding 
    char values. This can then be used to construct a 
    IOBluetoothDevice object, providing the signature of the withAddress: 
    selector has been set (as in _setpyobjcsignatures() in this module).
    
    For example:
        >>> chars = btaddrtochars("00:0e:0a:00:a2:00")
        >>> chars
        (0, 14, 10, 0, 162, 0)
        >>> device = _IOBluetooth.IOBluetoothDevice.withAddress_(chars)
        >>> type(device)
        <objective-c class IOBluetoothDevice at 0xa4024988>
        >>> device.getAddressString()
        u'00-0e-0a-00-a2-00'
    """
    if not _lightbluecommon._isbtaddr(addr):
        raise TypeError("address %s not valid bluetooth address" % str(addr))
    if addr.find(":") == -1:
        addr = addr.replace("-", ":")  # consider alternative addr separator

    # unhexlify gives binary value like '\x0e', then ord to get the char value.
    # unhexlify throws TypeError if value is not a hex pair.
    import binascii
    chars = [ord(binascii.unhexlify(part)) for part in addr.split(":")]
    return tuple(chars)
Ejemplo n.º 2
0
def sendfile(address, channel, source):
    if not _lightbluecommon._isbtaddr(address):
        raise TypeError("address '%s' is not a valid bluetooth address" \
            % address)
    if not isinstance(channel, int):
        raise TypeError("channel must be int, was %s" % type(channel))
    if not isinstance(source, types.StringTypes) and \
            not hasattr(source, "read"):
        raise TypeError("source must be string or file-like object with read() method")

    if isinstance(source, types.StringTypes):
        headers = {"name": source}
        fileobj = file(source, "rb")
        closefileobj = True
    else:
        if hasattr(source, "name"):
            headers = {"name": source.name}
        fileobj = source
        closefileobj = False

    client = OBEXClient(address, channel)
    client.connect()

    try:
        resp = client.put(headers, fileobj)
    finally:
        if closefileobj:
            fileobj.close()
        try:
            client.disconnect()
        except:
            pass    # always ignore disconnection errors

    if resp.code != _obexcommon.OK:
        raise OBEXError("server denied the Put request")
Ejemplo n.º 3
0
def btaddrtochars(addr):
    """
    Takes a bluetooth address and returns a tuple with the corresponding
    char values. This can then be used to construct a
    IOBluetoothDevice object, providing the signature of the withAddress:
    selector has been set (as in _setpyobjcsignatures() in this module).

    For example:
        >>> chars = btaddrtochars("00:0e:0a:00:a2:00")
        >>> chars
        (0, 14, 10, 0, 162, 0)
        >>> device = _IOBluetooth.IOBluetoothDevice.withAddress_(chars)
        >>> type(device)
        <objective-c class IOBluetoothDevice at 0xa4024988>
        >>> device.getAddressString()
        u'00-0e-0a-00-a2-00'
    """
    if not _lightbluecommon._isbtaddr(addr):
        raise TypeError("address %s not valid bluetooth address" % str(addr))
    if addr.find(":") == -1:
        addr = addr.replace("-", ":")   # consider alternative addr separator

    # unhexlify gives binary value like '\x0e', then ord to get the char value.
    # unhexlify throws TypeError if value is not a hex pair.
    import binascii
    chars = [ord(binascii.unhexlify(part)) for part in addr.split(":")]
    return tuple(chars)
Ejemplo n.º 4
0
def sendfile(address, channel, source):
    if not _lightbluecommon._isbtaddr(address):  
        raise TypeError("address '%s' is not a valid bluetooth address" %
                address)
    if not isinstance(channel, int):
        raise TypeError("channel must be int, was %s" % type(channel))
    if not isinstance(source, types.StringTypes) and \
            not hasattr(source, "read"):
        raise TypeError("source must be string or file-like object with read() method")
              
    if isinstance(source, types.StringTypes):
        headers = {"name": source}
        fileobj = file(source, "rb")
        closefileobj = True                    
    else:
        if hasattr(source, "name"):
            headers = {"name": source.name}
        fileobj = source
        closefileobj = False                
        
    client = OBEXClient(address, channel)
    client.connect()        
    
    try:
        resp = client.put(headers, fileobj)
    finally:
        if closefileobj:
            fileobj.close()
        try:
            client.disconnect()
        except:
            pass    # always ignore disconnection errors                

    if resp.code != _obexcommon.OK:
        raise OBEXError("server denied the Put request")
Ejemplo n.º 5
0
    def __init__(self, address, channel):
        if not _lightbluecommon._isbtaddr(address):
            raise TypeError("address '%s' is not a valid bluetooth address" %
                            address)
        if not type(channel) == int:
            raise TypeError("channel must be int, was %s" % type(channel))
        if channel < 0:
            raise ValueError("channel cannot be negative")

        self.__serveraddr = (address, channel)
        self.__busy = False
        self.__client = None
        self.__obexsession = None  # for testing
Ejemplo n.º 6
0
 def __init__(self, address, channel):
     if not _lightbluecommon._isbtaddr(address):  
         raise TypeError("address '%s' is not a valid bluetooth address"
             % address)
     if not type(channel) == int:
         raise TypeError("channel must be int, was %s" % type(channel))
     if channel < 0:
         raise ValueError("channel cannot be negative")
 
     self.__serveraddr = (address, channel)
     self.__busy = False    
     self.__client = None
     self.__obexsession = None   # for testing
Ejemplo n.º 7
0
def finddevicename(address, usecache=True):
    if not _lightbluecommon._isbtaddr(address):
        raise ValueError("%s is not a valid bluetooth address" % str(address))
        
    if address == gethostaddr():
        return _gethostname()
    
    try:
        # lookupName() expects address without colon separators
        import _lightblueutil        
        address_no_sep = address.replace(":", "").replace("-", "")
        name = _lightblueutil.lookupName(address_no_sep, (not usecache))
    except SymbianError, e:
        raise _lightbluecommon.BluetoothError(
            "Cannot find device name for %s: %s" % (address, str(e)))
Ejemplo n.º 8
0
def finddevicename(address, usecache=True):
    if not _lightbluecommon._isbtaddr(address):
        raise ValueError("%s is not a valid bluetooth address" % str(address))

    if address == gethostaddr():
        return _gethostname()

    if usecache:
        name = _devicenames.get(address)
        if name is not None:
            return name

    name = bluetooth.lookup_name(address)
    if name is None:
        raise _lightbluecommon.BluetoothError(
            "Could not find device name for %s" % address)
    _devicenames[address] = name
    return name
Ejemplo n.º 9
0
def finddevicename(address, usecache=True):
    if not _lightbluecommon._isbtaddr(address):
        raise TypeError("%s is not a valid bluetooth address" % str(address))

    if address == gethostaddr():
        return _gethostname()

    device = _IOBluetooth.IOBluetoothDevice.withAddress_(_macutil.createbtdevaddr(address))
    if usecache:
        name = device.getName()
        if name is not None:
            return name

    # do name request with timeout of 10 seconds
    result = device.remoteNameRequest_withPageTimeout_(None, 10000)
    if result == _macutil.kIOReturnSuccess:
        return device.getName()
    raise _lightbluecommon.BluetoothError("Could not find device name for %s" % address)
Ejemplo n.º 10
0
def finddevicename(address, usecache=True):
    if not _lightbluecommon._isbtaddr(address):
        raise ValueError("%s is not a valid bluetooth address" % str(address))

    if address == gethostaddr():
        return _gethostname()

    if usecache:
        name = _devicenames.get(address)
        if name is not None:
            return name

    name = bluetooth.lookup_name(address)
    if name is None:
        raise _lightbluecommon.BluetoothError(
            "Could not find device name for %s" % address)
    _devicenames[address] = name
    return name
Ejemplo n.º 11
0
def finddevicename(address, usecache=True):
    if not _lightbluecommon._isbtaddr(address):
        raise TypeError("%s is not a valid bluetooth address" % str(address))

    if address == gethostaddr():
        return _gethostname()

    device = _IOBluetooth.IOBluetoothDevice.withAddressString_(address)
    if usecache:
        name = device.getName()
        if name is not None:
            return name
    
    # do name request with timeout of 10 seconds    
    result = device.remoteNameRequest_withPageTimeout_(None, 10000)
    if result == _macutil.kIOReturnSuccess:
        return device.getName()
    raise _lightbluecommon.BluetoothError(
        "Could not find device name for %s" % address)      
Ejemplo n.º 12
0
def _checkaddrpair(address, checkbtaddr=True):
    # will want checkbtaddr=False if the address might be empty string
    # (for binding to a server address)

    if not isinstance(address, tuple):
        raise TypeError("address must be (address, port) tuple, was %s" % \
            type(address))

    if len(address) != 2:
        raise TypeError("address tuple must have 2 items (has %d)" % \
            len(address))

    if not isinstance(address[0], types.StringTypes):
        raise TypeError("address host value must be string, was %s" % \
            type(address[0]))

    if checkbtaddr:
        if not _lightbluecommon._isbtaddr(address[0]):
            raise TypeError("address '%s' is not a bluetooth address" % \
                address[0])

    if not isinstance(address[1], int):
        raise TypeError("address port value must be int, was %s" % \
            type(address[1]))
Ejemplo n.º 13
0
def _checkaddrpair(address, checkbtaddr=True):
    # will want checkbtaddr=False if the address might be empty string 
    # (for binding to a server address)
    
    if not isinstance(address, tuple):
        raise TypeError("address must be (address, port) tuple, was %s" % \
            type(address))
    
    if len(address) != 2:
        raise TypeError("address tuple must have 2 items (has %d)" % \
            len(address))
            
    if not isinstance(address[0], types.StringTypes):
        raise TypeError("address host value must be string, was %s" % \
            type(address[0]))
        
    if checkbtaddr:
        if not _lightbluecommon._isbtaddr(address[0]):
            raise TypeError("address '%s' is not a bluetooth address" % \
                address[0])
    
    if not isinstance(address[1], int):
        raise TypeError("address port value must be int, was %s" % \
            type(address[1]))              
Ejemplo n.º 14
0
# Copyright (c) 2009 Bea Lam. All rights reserved.
Ejemplo n.º 15
0
# Copyright (c) 2009 Bea Lam. All rights reserved.