(self, timeout=10, scan_interval=75, scan_window=50, active=True,
             discover_mode=constants.gap_discover_mode['observation'],
             scan_cb=None, **kwargs)
return devices
Perform a scan to discover BLE devices.

        timeout -- the number of seconds this scan should last.
        scan_interval -- the number of milliseconds until scanning is restarted.
        scan_window -- the number of milliseconds the scanner will listen on one
                     frequency for advertisement packets.
        active -- True --> ask sender for scan response data. False --> don't.
        discover_mode -- one of the gap_discover_mode constants.
        scan_cb -- This callback function is called whenever a new BLE
                   advertising packet is received.
                   The function takes three parameters:
                       devices, addr, packet_type
                   If the function returns True, the scan is aborted
Perform a scan to discover BLE devices.
Perform a scan to discover BLE devices .
def scan(self, timeout=10, scan_interval=75, scan_window=50, active=True,
             discover_mode=constants.gap_discover_mode['observation'],
             scan_cb=None, **kwargs):
        """
        Perform a scan to discover BLE devices.

        timeout -- the number of seconds this scan should last.
        scan_interval -- the number of milliseconds until scanning is restarted.
        scan_window -- the number of milliseconds the scanner will listen on one
                     frequency for advertisement packets.
        active -- True --> ask sender for scan response data. False --> don't.
        discover_mode -- one of the gap_discover_mode constants.
        scan_cb -- This callback function is called whenever a new BLE
                   advertising packet is received.
                   The function takes three parameters:
                       devices, addr, packet_type
                   If the function returns True, the scan is aborted
        """
        self._scan_cb = scan_cb
        parameters = 1 if active else 0
        # NOTE: the documentation seems to say that the times are in units of
        # 625us but the ranges it gives correspond to units of 1ms....
        self.send_command(
            CommandBuilder.gap_set_scan_parameters(
                scan_interval, scan_window, parameters
            ))

        self.expect(ResponsePacketType.gap_set_scan_parameters)

        log.info("Starting an %s scan", "active" if active else "passive")
        self.send_command(CommandBuilder.gap_discover(discover_mode))

        self.expect(ResponsePacketType.gap_discover)

        log.info("Pausing for maximum %ds to allow scan to complete", timeout)

        self._evt.set()
        start_time = time.time()

        while self._evt.is_set():
            try:
                self.expect(EventPacketType.gap_scan_response,
                            timeout=timeout)
            except ExpectedResponseTimeout:
                pass
            if _timed_out(start_time, timeout):
                break

        log.info("Stopping scan")
        self.send_command(CommandBuilder.gap_end_procedure())
        self.expect(ResponsePacketType.gap_end_procedure)

        devices = []
        for address, info in self._devices_discovered.items():
            devices.append({
                'address': address,
                'name': info.name,
                'rssi': info.rssi,
                'packet_data': info.packet_data
            })
        log.info("Discovered %d devices: %s", len(devices), devices)
        self._devices_discovered = {}
        return devices
def scan ( self , timeout = 10 , scan_interval = 75 , scan_window = 50 , active = True , discover_mode = constants . gap_discover_mode [ 'observation' ] , scan_cb = None , * * kwargs ) : self . _scan_cb = scan_cb parameters = 1 if active else 0 # NOTE: the documentation seems to say that the times are in units of # 625us but the ranges it gives correspond to units of 1ms.... self . send_command ( CommandBuilder . gap_set_scan_parameters ( scan_interval , scan_window , parameters ) ) self . expect ( ResponsePacketType . gap_set_scan_parameters ) log . info ( "Starting an %s scan" , "active" if active else "passive" ) self . send_command ( CommandBuilder . gap_discover ( discover_mode ) ) self . expect ( ResponsePacketType . gap_discover ) log . info ( "Pausing for maximum %ds to allow scan to complete" , timeout ) self . _evt . set ( ) start_time = time . time ( ) while self . _evt . is_set ( ) : try : self . expect ( EventPacketType . gap_scan_response , timeout = timeout ) except ExpectedResponseTimeout : pass if _timed_out ( start_time , timeout ) : break log . info ( "Stopping scan" ) self . send_command ( CommandBuilder . gap_end_procedure ( ) ) self . expect ( ResponsePacketType . gap_end_procedure ) devices = [ ] for address , info in self . _devices_discovered . items ( ) : devices . append ( { 'address' : address , 'name' : info . name , 'rssi' : info . rssi , 'packet_data' : info . packet_data } ) log . info ( "Discovered %d devices: %s" , len ( devices ) , devices ) self . _devices_discovered = { } return devices