termux / termux-api

Termux add-on app which exposes device functionality as API to command line programs.
https://f-droid.org/en/packages/com.termux.api/
2.25k stars 449 forks source link

The command 'termux-wifi-enable true' fails when the screen is locked. #674

Closed rayventi closed 5 months ago

rayventi commented 5 months ago

Problem description The command 'termux-wifi-enable true' fails when the screen is locked.

Steps to reproduce Below is the code for testing 'termux-wifi-enable':

import os, json, time

def get_ip():
    try:
        wifi_info = os.popen("termux-wifi-connectioninfo").read()
        if json.loads(wifi_info):
            return json.loads(wifi_info).get("ip")
        else:
            return None
    except json.JSONDecodeError:
        return None

while True:
    os.system("termux-wifi-enable true")
    time.sleep(10)
    ip = get_ip()
    if ip == "10.43.252.31":
        print(f"'termux-wifi-enable true' success\n ip : {ip}")
        os.system("termux-wifi-enable false")
        time.sleep(10)
        ip = get_ip()
        if ip == "0.0.0.0":
            print(f"'termux-wifi-enable false' success\n ip : {ip}")
        else:
            print(f"'termux-wifi-enable false' failed\n ip : {ip}")
    else:
        print(f"'termux-wifi-enable true' failed\n ip : {ip}")
    time.sleep(10)

image

My device is a Redmi 4 Pro, running Lineage OS with Android version 9. The termux-wake-lock is already held, and both Termux and Termux:API are exempt from battery optimization. The termux:api now has permission to modify the system settings. Expected behavior

In my Python code, I aim to toggle the Wi-Fi mode using the 'termux-wifi-enable' command when the phone screen is locked.

Additional information