(self, address, timeout=5,
                address_type=BLEAddressType.public,
                interval_min=60, interval_max=76, supervision_timeout=100,
                latency=0)
Connect directly to a device given the ble address then discovers and
        stores the characteristic and characteristic descriptor handles.

        Requires that the adapter is not connected to a device already.

        address -- a bytearray containing the device mac address.
        timeout -- number of seconds to wait before returning if not connected.
        address_type -- one of BLEAddressType's values, either public or random.

        Raises BGAPIError or NotConnectedError on failure.
Connect directly to a device given the ble address then discovers and
        stores the characteristic and characteristic descriptor handles.
Connect directly to a device given the ble address then discovers and stores the characteristic and characteristic descriptor handles .
def connect(self, address, timeout=5,
                address_type=BLEAddressType.public,
                interval_min=60, interval_max=76, supervision_timeout=100,
                latency=0):
        """
        Connect directly to a device given the ble address then discovers and
        stores the characteristic and characteristic descriptor handles.

        Requires that the adapter is not connected to a device already.

        address -- a bytearray containing the device mac address.
        timeout -- number of seconds to wait before returning if not connected.
        address_type -- one of BLEAddressType's values, either public or random.

        Raises BGAPIError or NotConnectedError on failure.
        """

        address_bytes = bytearray(unhexlify(address.replace(":", "")))
        for device in self._connections.values():
            if device._address == bgapi_address_to_hex(address_bytes):
                return device

        log.info("Connecting to device at address %s (timeout %ds)",
                 address, timeout)
        self.set_bondable(False)

        if address_type == BLEAddressType.public:
            addr_type = constants.ble_address_type['gap_address_type_public']
        else:
            addr_type = constants.ble_address_type['gap_address_type_random']

        self.send_command(
            CommandBuilder.gap_connect_direct(
                address_bytes, addr_type, interval_min, interval_max,
                supervision_timeout, latency))

        try:
            self.expect(ResponsePacketType.gap_connect_direct)
            _, packet = self.expect(EventPacketType.connection_status,
                                    timeout=timeout)
            # TODO what do we do if the status isn't 'connected'? Retry?
            # Raise an exception? Should also check the address matches the
            # expected TODO i'm finding that when reconnecting to the same
            # MAC, we geta conneciotn status of "disconnected" but that is
            # picked up here as "connected", then we don't get anything
            # else.
            if self._connection_status_flag(
                    packet['flags'],
                    constants.connection_status_flag['connected']):
                device = BGAPIBLEDevice(
                    bgapi_address_to_hex(packet['address']),
                    packet['connection_handle'],
                    self)
                if self._connection_status_flag(
                        packet['flags'],
                        constants.connection_status_flag['encrypted']):
                    device.encrypted = True
                self._connections[packet['connection_handle']] = device
                log.info("Connected to %s", address)
                return device
        except ExpectedResponseTimeout:
            # If the connection doesn't occur because the device isn't there
            # then you should manually stop the command.
            #
            # If we never get the connection status it is likely that it
            # didn't occur because the device isn't there. If that is true
            # then we have to manually stop the command.
            self._end_procedure()
            exc = NotConnectedError()
            exc.__cause__ = None
            raise exc
def connect ( self , address , timeout = 5 , address_type = BLEAddressType . public , interval_min = 60 , interval_max = 76 , supervision_timeout = 100 , latency = 0 ) : address_bytes = bytearray ( unhexlify ( address . replace ( ":" , "" ) ) ) for device in self . _connections . values ( ) : if device . _address == bgapi_address_to_hex ( address_bytes ) : return device log . info ( "Connecting to device at address %s (timeout %ds)" , address , timeout ) self . set_bondable ( False ) if address_type == BLEAddressType . public : addr_type = constants . ble_address_type [ 'gap_address_type_public' ] else : addr_type = constants . ble_address_type [ 'gap_address_type_random' ] self . send_command ( CommandBuilder . gap_connect_direct ( address_bytes , addr_type , interval_min , interval_max , supervision_timeout , latency ) ) try : self . expect ( ResponsePacketType . gap_connect_direct ) _ , packet = self . expect ( EventPacketType . connection_status , timeout = timeout ) # TODO what do we do if the status isn't 'connected'? Retry? # Raise an exception? Should also check the address matches the # expected TODO i'm finding that when reconnecting to the same # MAC, we geta conneciotn status of "disconnected" but that is # picked up here as "connected", then we don't get anything # else. if self . _connection_status_flag ( packet [ 'flags' ] , constants . connection_status_flag [ 'connected' ] ) : device = BGAPIBLEDevice ( bgapi_address_to_hex ( packet [ 'address' ] ) , packet [ 'connection_handle' ] , self ) if self . _connection_status_flag ( packet [ 'flags' ] , constants . connection_status_flag [ 'encrypted' ] ) : device . encrypted = True self . _connections [ packet [ 'connection_handle' ] ] = device log . info ( "Connected to %s" , address ) return device except ExpectedResponseTimeout : # If the connection doesn't occur because the device isn't there # then you should manually stop the command. # # If we never get the connection status it is likely that it # didn't occur because the device isn't there. If that is true # then we have to manually stop the command. self . _end_procedure ( ) exc = NotConnectedError ( ) exc . __cause__ = None raise exc