Search code examples
python-3.xsl4a

Can't find wifi networks


I am working on python sl4a script to scan wifi networks around me, search for specific SSID by partial match (network name is HOME1234 and script should find all networks which contain HOME), retrieve full network SSID from list by index and connect to it.

Problem is that function Android().wifiGetScanResults() returns variable of type sl4a.result. What i need it to do is to output this as list so algorithm can search trough every element and return its index.

This is algorithm: number=[my_list.index(i) for i in my_list if "HOME" in i]

my_list: List to search trough

HOME: Name of network to search for

This is code

from sl4a import * 
from time import * 

a=Android()

def find_net(): 
    try:
        print("Scanning")
        a.wifiLockAcquireFull() 
        a.wifiStartScan()
        sleep(5)
        print("Scan completed")
        aps=list(a.wifiGetScanResults())
        print("Got results")
        try:
            number=[aps.index(i) for i in aps if "HOM" in i]
        except:
            print("Nothing found")
            return number

    except:
        print("Scan Failed")

 print(find_net())

This code gives this result

Scanning
Scan completed
Got results
Nothing found
Scan Failed
None

#[QPython] Press enter to exit

Please note that this is runned in qpython3 on android


Solution

  • Printing out everything is good! Every sl4a call will return something like Result(id=1, result=None, error=None), so I ran this:

    import sl4a
    droid=sl4a.Android()
    print(droid.wifiLockAcquireFull())
    print(droid.wifiStartScan())
    print(droid.wifiGetScanResults())
    

    And I got:

    Result(id=1, result=None, error=None)
    java.lang.NullPointerException: throw with null exception
    Result(id=2, result=None, error='java.lang.NullPointerException: throw with null exception')
    Result(id=3, result=[a list of dictionaries, each one is a wifi], error=None)
    

    Looks like something went wrong when running wifiStartScan, I can't control it since it's in sl4a. Also, only calling wifiGetScanResults can still get the results since the documentation said wifiGetScanResults returns the list of access points found during "the most recent" Wifi scan and Android itself performs the scan frequently.

    The results are a list of dictionaries, each one contains these keys: "capabilities", "frequency", "ssid", "bssid", "level". So what you want may be:

    import sl4a,time
    droid = sl4a.Android()
    
    ### keep these if your phone can run wifiStartScan without errors ###
    print("Scanning...")
    droid.wifiLockAcquireFull()
    droid.wifiStartScan()
    time.sleep(5)
    print("Scan completed")
    ### keep these if your phone can run wifiStartScan without errors ###
    
    aps = droid.wifiGetScanResults().result
    numbers = [i for i in range(len(aps)) if "HOME" in aps[i]["ssid"] ]
    print(numbers)