예제 #1
0
    def connect(self, peer_address, connection_params=None):
        """
        Initiates a connection to a peripheral peer with the specified connection parameters, or uses the default
        connection parameters if not specified. The connection will not be complete until the returned waitable
        either times out or reports the newly connected peer

        :param peer_address: The address of the peer to connect to
        :type peer_address: peer.PeerAddress
        :param connection_params: Optional connection parameters to use. If not specified, uses the set default
        :type connection_params: peer.ConnectionParameters
        :return: A Waitable which can be used to wait until the connection is successful or times out. Waitable returns
                 a peer.Peripheral object
        :rtype: PeripheralConnectionWaitable
        """
        if peer_address in self.connected_peripherals.keys():
            raise exceptions.InvalidStateException(
                "Already connected to {}".format(peer_address))
        if self.connecting_peripheral is not None:
            raise exceptions.InvalidStateException(
                "Cannot initiate a new connection while connecting to another")

        if not connection_params:
            connection_params = self._default_conn_params

        self.connecting_peripheral = peer.Peripheral(self, peer_address,
                                                     connection_params)
        periph_connection_waitable = PeripheralConnectionWaitable(
            self, self.connecting_peripheral)
        self.ble_driver.ble_gap_connect(peer_address)
        return periph_connection_waitable
예제 #2
0
    def connect(self, peer_address, connection_params=None) -> PeripheralConnectionWaitable:
        """
        Initiates a connection to a peripheral peer with the specified connection parameters, or uses the default
        connection parameters if not specified. The connection will not be complete until the returned waitable
        either times out or reports the newly connected peer

        :param peer_address: The address of the peer to connect to
        :type peer_address: peer.PeerAddress
        :param connection_params: Optional connection parameters to use. If not specified, uses the set default
        :type connection_params: peer.ConnectionParameters
        :return: A Waitable which can be used to wait until the connection is successful or times out. Waitable returns
                 a peer.Peripheral object
        """
        if peer_address in self.connected_peripherals.keys():
            raise exceptions.InvalidStateException("Already connected to {}".format(peer_address))
        if self.connecting_peripheral is not None:
            raise exceptions.InvalidStateException("Cannot initiate a new connection while connecting to another")

        # Try finding the peer's name in the scan report
        name = ""
        scan_report = self.scanner.scan_report.get_report_for_peer(peer_address)
        if scan_report:
            name = scan_report.advertise_data.local_name

        if not connection_params:
            connection_params = self._default_conn_params

        self.connecting_peripheral = peer.Peripheral(self, peer_address, connection_params, self._default_security_params, name,
                                                     self._default_conn_config.write_cmd_tx_queue_size)
        periph_connection_waitable = PeripheralConnectionWaitable(self, self.connecting_peripheral)
        self.ble_driver.ble_gap_connect(peer_address, conn_params=connection_params,
                                        conn_cfg_tag=self._default_conn_config.conn_tag)
        return periph_connection_waitable
예제 #3
0
    def configure(self,
                  vendor_specific_uuid_count=10,
                  service_changed=False,
                  max_connected_peripherals=1,
                  max_connected_clients=1,
                  max_secured_peripherals=1,
                  attribute_table_size=nrf_types.driver.
                  BLE_GATTS_ATTR_TAB_SIZE_DEFAULT,
                  att_mtu_max_size=MTU_SIZE_FOR_MAX_DLE):
        """
        Configures the BLE Device with the given settings.

        .. note:: Configuration must be set before opening the device

        :param vendor_specific_uuid_count: The Nordic hardware limits number of 128-bit Base UUIDs
                                           that the device can know about. This normally equals the number of custom services
                                           that are to be supported, since characteristic UUIDs are usually derived from the service base UUID.
        :param service_changed: Whether or not the Service Changed characteristic is exposed in the GAP service
        :param max_connected_peripherals: The maximum number of concurrent connections with peripheral devices
        :param max_connected_clients: The maximum number of concurrent connections with client devices (NOTE: blatann currently only supports 1)
        :param max_secured_peripherals: The maximum number of concurrent peripheral connections that will need security (bonding/pairing) enabled
        :param attribute_table_size: The maximum size of the attribute table.
                                     Increase this number if there's a lot of services/characteristics in your GATT database.
        :param att_mtu_max_size: The maximum ATT MTU size supported. The default supports an MTU which will fit into
                                 a single transmission if Data Length Extensions is set to its max (251)
        """
        if self.ble_driver.is_open:
            raise exceptions.InvalidStateException(
                "Cannot configure the BLE device after it has been opened")

        self._ble_configuration = nrf_types.BleEnableConfig(
            vendor_specific_uuid_count, max_connected_clients,
            max_connected_peripherals, max_secured_peripherals,
            service_changed, attribute_table_size)
        self._default_conn_config.max_att_mtu = att_mtu_max_size
예제 #4
0
파일: device.py 프로젝트: notmikeb/blatann
    def configure(self, vendor_specific_uuid_count=10, service_changed=False, max_connected_peripherals=1,
                  max_connected_clients=1, max_secured_peripherals=1,
                  attribute_table_size=nrf_types.driver.BLE_GATTS_ATTR_TAB_SIZE_DEFAULT,
                  att_mtu_max_size=MTU_SIZE_FOR_MAX_DLE, device_name=""):
        if self.ble_driver.is_open:
            raise exceptions.InvalidStateException("Cannot configure the BLE device after it has been opened")
        if device_name and isinstance(device_name, str):
            device_name = device_name.encode("utf8")

        self._ble_configuration = nrf_types.BleEnableConfig(vendor_specific_uuid_count, max_connected_clients,
                                                            max_connected_peripherals, max_secured_peripherals,
                                                            service_changed, attribute_table_size, device_name)
        self._default_conn_config.max_att_mtu = att_mtu_max_size
예제 #5
0
    def configure(self,
                  vendor_specific_uuid_count=10,
                  service_changed=False,
                  max_connected_peripherals=1,
                  max_connected_clients=1,
                  max_secured_peripherals=1,
                  attribute_table_size=nrf_types.driver.
                  BLE_GATTS_ATTR_TAB_SIZE_DEFAULT,
                  att_mtu_max_size=MTU_SIZE_FOR_MAX_DLE):
        if self.ble_driver.is_open:
            raise exceptions.InvalidStateException(
                "Cannot configure the BLE device after it has been opened")

        self._ble_configuration = nrf_types.BLEEnableParams(
            vendor_specific_uuid_count, service_changed, max_connected_clients,
            max_connected_peripherals, max_secured_peripherals,
            attribute_table_size, att_mtu_max_size)