(self, timeout=10, run_as_root=False)
return []
By default, scanning with gatttool requires root privileges.
        If you don't want to require root, you must add a few
        'capabilities' to your system. If you have libcap installed, run this to
        enable normal users to perform LE scanning:
            setcap 'cap_net_raw,cap_net_admin+eip' `which hcitool`

        If you do use root, the hcitool subprocess becomes more difficult to
        terminate cleanly, and may leave your Bluetooth adapter in a bad state.
By default, scanning with gatttool requires root privileges.
        If you don't want to require root, you must add a few
        'capabilities' to your system. If you have libcap installed, run this to
        enable normal users to perform LE scanning:
            setcap 'cap_net_raw,cap_net_admin+eip' `which hcitool`
By default scanning with gatttool requires root privileges . If you don t want to require root you must add a few capabilities to your system . If you have libcap installed run this to enable normal users to perform LE scanning : setcap cap_net_raw cap_net_admin + eip which hcitool
def scan(self, timeout=10, run_as_root=False):
        """
        By default, scanning with gatttool requires root privileges.
        If you don't want to require root, you must add a few
        'capabilities' to your system. If you have libcap installed, run this to
        enable normal users to perform LE scanning:
            setcap 'cap_net_raw,cap_net_admin+eip' `which hcitool`

        If you do use root, the hcitool subprocess becomes more difficult to
        terminate cleanly, and may leave your Bluetooth adapter in a bad state.
        """

        cmd = 'hcitool -i %s lescan' % self._hci_device
        if run_as_root:
            cmd = 'sudo %s' % cmd

        log.info("Starting BLE scan")
        # "lescan" doesn't exit, so we're forcing a timeout
        self._scan = scan = pexpect.spawn(cmd, timeout=timeout)
        try:
            scan.expect('foooooo')
        except pexpect.EOF:
            before_eof = scan.before.decode('utf-8', 'replace')
            if "No such device" in before_eof:
                message = "No BLE adapter found"
            elif "Set scan parameters failed: Input/output error" in before_eof:
                message = ("BLE adapter requires reset after a scan as root"
                           "- call adapter.reset()")
            else:
                message = "Unexpected error when scanning: %s" % before_eof
            log.error(message)
            raise BLEError(message)
        except pexpect.TIMEOUT:
            devices = {}
            for line in scan.before.decode('utf-8', 'replace').split('\r\n'):
                if 'sudo' in line:
                    raise BLEError("Enable passwordless sudo for 'hcitool' "
                                   "before scanning")
                match = re.match(
                    r'(([0-9A-Fa-f][0-9A-Fa-f]:?){6}) (\(?.+\)?)', line)

                if match is not None:
                    address = match.group(1)
                    name = match.group(3)
                    if name == "(unknown)":
                        name = None

                    if address in devices:
                        if (devices[address]['name'] is None) and (name is not
                                                                   None):
                            log.info("Discovered name of %s as %s",
                                     address, name)
                            devices[address]['name'] = name
                    else:
                        log.info("Discovered %s (%s)", address, name)
                        devices[address] = {
                            'address': address,
                            'name': name
                        }
            log.info("Found %d BLE devices", len(devices))
            return [device for device in devices.values()]
        finally:
            self.kill()
        return []
def scan ( self , timeout = 10 , run_as_root = False ) : cmd = 'hcitool -i %s lescan' % self . _hci_device if run_as_root : cmd = 'sudo %s' % cmd log . info ( "Starting BLE scan" ) # "lescan" doesn't exit, so we're forcing a timeout self . _scan = scan = pexpect . spawn ( cmd , timeout = timeout ) try : scan . expect ( 'foooooo' ) except pexpect . EOF : before_eof = scan . before . decode ( 'utf-8' , 'replace' ) if "No such device" in before_eof : message = "No BLE adapter found" elif "Set scan parameters failed: Input/output error" in before_eof : message = ( "BLE adapter requires reset after a scan as root" "- call adapter.reset()" ) else : message = "Unexpected error when scanning: %s" % before_eof log . error ( message ) raise BLEError ( message ) except pexpect . TIMEOUT : devices = { } for line in scan . before . decode ( 'utf-8' , 'replace' ) . split ( '\r\n' ) : if 'sudo' in line : raise BLEError ( "Enable passwordless sudo for 'hcitool' " "before scanning" ) match = re . match ( r'(([0-9A-Fa-f][0-9A-Fa-f]:?){6}) (\(?.+\)?)' , line ) if match is not None : address = match . group ( 1 ) name = match . group ( 3 ) if name == "(unknown)" : name = None if address in devices : if ( devices [ address ] [ 'name' ] is None ) and ( name is not None ) : log . info ( "Discovered name of %s as %s" , address , name ) devices [ address ] [ 'name' ] = name else : log . info ( "Discovered %s (%s)" , address , name ) devices [ address ] = { 'address' : address , 'name' : name } log . info ( "Found %d BLE devices" , len ( devices ) ) return [ device for device in devices . values ( ) ] finally : self . kill ( ) return [ ]