Example #1
0
    def on_gap_evt_adv_report(self, ble_driver, conn_handle, peer_addr, rssi,
                              adv_type, adv_data):
        dev_name_list = []
        if BLEAdvData.Types.complete_local_name in adv_data.records:
            dev_name_list = adv_data.records[
                BLEAdvData.Types.complete_local_name]

        elif BLEAdvData.Types.short_local_name in adv_data.records:
            dev_name_list = adv_data.records[BLEAdvData.Types.short_local_name]

        dev_name = "".join(chr(e) for e in dev_name_list)
        address_string = "".join("{0:02X}".format(b) for b in peer_addr.addr)
        logger.debug(
            'Received advertisement report, address: 0x{}, device_name: {}'.
            format(address_string, dev_name))

        if (dev_name
                == self.target_device_name) or (address_string
                                                == self.target_device_addr):
            conn_params = BLEGapConnParams(min_conn_interval_ms=15,
                                           max_conn_interval_ms=30,
                                           conn_sup_timeout_ms=4000,
                                           slave_latency=0)
            logger.info(
                'BLE: Found target advertiser, address: 0x{}, name: {}'.format(
                    address_string, dev_name))
            logger.info('BLE: Connecting to 0x{}'.format(address_string))
            self.adapter.connect(address=peer_addr, conn_params=conn_params)
            # store the name and address for subsequent connections
            self.target_device_name = dev_name
            self.target_device_addr = address_string
Example #2
0
    def on_gap_evt_adv_report(self, ble_driver, conn_handle, peer_addr, rssi, adv_type, adv_data):
        dev_name_list = []
        if BLEAdvData.Types.complete_local_name in adv_data.records:
            dev_name_list = adv_data.records[BLEAdvData.Types.complete_local_name]

        elif BLEAdvData.Types.short_local_name in adv_data.records:
            dev_name_list = adv_data.records[BLEAdvData.Types.short_local_name]

        dev_name        = "".join(chr(e) for e in dev_name_list)
        address_string  = "".join("{0:02X}".format(b) for b in peer_addr.addr)
        logger.info('Received advertisement report, address: 0x{}, device_name: {}'.format(address_string, dev_name))

        if (dev_name == self.target_device_name) or (address_string == self.target_device_addr):
            self.conn_params = BLEGapConnParams(min_conn_interval_ms = 7.5,
                                                max_conn_interval_ms = 30,
                                                conn_sup_timeout_ms  = 4000,
                                                slave_latency        = 0)
            logger.info('BLE: Found target advertiser, address: 0x{}, name: {}'.format(address_string, dev_name))
            logger.info('BLE: Connecting to 0x{}'.format(address_string))
            # Connect must specify tag=1 to enable the settings
            # set with BLEConfigConnGatt (that implictly operates
            # on connections with tag 1) to allow for larger MTU.
            self.adapter.connect(address=peer_addr,
                                 conn_params=self.conn_params,
                                 tag=1)
            # store the address for subsequent connections
            self.target_device_addr = address_string
            self.target_device_addr_type = peer_addr
 def __init__(self, adapter, uuids, address):
     super(Collector, self).__init__()
     self.adapter = adapter
     self.conn_q = Queue.Queue()
     self.adapter.observer_register(self)
     self.adapter.driver.observer_register(self)
     self.uuids = uuids
     self.target_address = address
     self.conn_params = BLEGapConnParams(min_conn_interval_ms=100,
                                         max_conn_interval_ms=100,
                                         conn_sup_timeout_ms=4000,
                                         slave_latency=0)
    def test_conn_params_update(self):
        self.peripheral.start(self.adv_name)

        min_conn_int = 7.5
        max_conn_int = 15
        conn_sup_timeout = 500
        slave_latency = 5

        conn_params = BLEGapConnParams(
            min_conn_int,
            max_conn_int,
            conn_sup_timeout,
            slave_latency
        )

        result = self.central.start(self.adv_name, conn_params)

        self.assertTrue(type(result) == BLEGapConnParams)
        self.assertTrue(result.min_conn_interval_ms >= min_conn_int)
        self.assertTrue(result.max_conn_interval_ms <= max_conn_int)
        self.assertEqual(result.conn_sup_timeout_ms, conn_sup_timeout)
        self.assertEqual(result.slave_latency, slave_latency)

        self.central.stop()