Dave McLaughlin
Dave McLaughlin

Reputation: 71

Android. How to bring up WiFi

I have an embedded module running Android and the OS disconnects WiFi after about 5 mins of inactivity. If I then run any application that uses network connectivity, the WiFi remains off and the icon does not show in the status bar. I have to go into settings and WiFi settings to get the WiFi to come back up.

I have an application that has to send data periodically over the WiFi connection. If I start the programme with the WiFi active and send data every 2-3 mins the WiFi remains active and never disconnects. If I let the WiFi disconnect and then try to send, it never comes back up. In code, the call to:

    wifi.isWifiEnabled()

always returns true but the icon remains hidden meaning that the WiFi is not connected. I have tried to call reconnect() from the code as a test with a button, but the WiFi never comes up.

From the terminal debug port I can type IFCONFIG WLAN0 UP and the WiFi connects to the AP.

Is there any call I can make that will cause the WiFi to reconnect and is there any way I can actually detect that the WiFi is disconnected other than detecting that the TCP/IP connection failed?

IN RESPONSE TO A REQUEST BELOW, I HAVE ADDED THE FOLLOWING CODE SNIPPETS AND LOGCAT OUTPUT

    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    wifiLock = wifi.createWifiLock(WifiManager.WIFI_MODE_FULL, "Mini210-Lock");
    if(!wifiLock.isHeld())
    {
        wifi.setWifiEnabled(true);
        wifi.reconnect();
        // wifi.reassociate(); // Also try this!!
        wifiLock.acquire();    // Keep system alive
        Log.i("Alarm System", "WiFi Lock Acquired");
    }

Logcat shows the following when the above code is run.

    09-10 22:43:25.273: I/Alarm System(611): WiFi Lock Acquired
    09-10 22:43:25.417: I/ActivityManager(91): Display axon.alarm.system/.AlarmSystemActivity: +15s198ms (total +4h23m59s645ms)
    09-10 22:43:31.636: W/wpa_supplicant(133): Failed to initiate AP scan.
    09-10 22:43:41.636: W/wpa_supplicant(133): Failed to initiate AP scan.

When I enter ifconfig wlan0 up in the debug monitor port, the following appears in logcat (some duplicate output has been deleted)

    09-10 22:56:12.726: I/wpa_supplicant(133): CTRL-EVENT-SCAN-RESULTS  Ready
    09-10 22:56:12.726: I/wpa_supplicant(133): Trying to associate with 00:22:75:32:a9:47 (SSID='kpk_mobile' freq=2412 MHz)
    09-10 22:56:20.976: D/dhcpcd(623): checking 192.168.1.123 is available on attached networks
    09-10 22:56:20.976: D/dhcpcd(623): sending ARP probe (1 of 3), next in 1.73 seconds
    09-10 22:56:22.710: D/dhcpcd(623): sending ARP probe (2 of 3), next in 1.00 seconds
    09-10 22:56:23.714: D/dhcpcd(623): sending ARP probe (3 of 3), next in 2.00 seconds
    09-10 22:56:25.718: D/dhcpcd(623): leased 192.168.1.123 for 86400 seconds
    09-10 22:56:25.718: D/dhcpcd(623): adding IP address 192.168.1.123/24
    09-10 22:56:25.718: D/dhcpcd(623): adding route to 0.0.0.0/0 via 192.168.1.1
    09-10 22:56:25.730: D/dhcpcd(623): executing `/system/etc/dhcpcd/dhcpcd-run-hooks', reason BOUND
    09-10 22:56:25.773: D/dhcpcd(623): forking to background
    09-10 22:56:25.804: V/WifiStateTracker(91): DhcpHandler: DHCP request succeeded
    09-10 22:56:25.804: V/WifiStateTracker(91): IP configuration: ipaddr 192.168.1.123  gateway 192.168.1.1 netmask 255.255.255.0 dns1 192.168.1.1 dns2 0.0.0.0 DHCP server 192.168.1.1 lease 86400 seconds
    09-10 22:56:25.816: I/TelephonyRegistry(91): notifyDataConnection: state=0 isDataConnectivityPossible=false reason=dataDisabled interfaceName=null networkType=0
    09-10 22:56:25.828: D/WifiWatchdogService(91): (android.server.ServerThread) kpk_mobile (00:22:75:32:a9:47) does not require the watchdog
    09-10 22:56:25.843: V/FTPDroid(356): EventsReceiver: NETWORK_STATE_CHANGED_ACTION (exists: true, connected: true)
    09-10 22:56:25.863: D/Tethering(91): MasterInitialState.processMessage what=3
    09-10 22:56:25.902: I/GTalkService(278): [ServiceAutoStarter] --- connectivity changed
    09-10 22:56:25.902: I/GTalkService(278): [ServiceAutoStarter] --- start GTalk   service ---
    09-10 22:56:27.789: D/dhcpcd(647): sending ARP announce (1 of 2), next in 2.00 seconds
    09-10 22:56:29.789: D/dhcpcd(647): sending ARP announce (2 of 2)
    09-10 22:56:29.789: D/dhcpcd(647): renew in 43196 seconds

Upvotes: 1

Views: 2433

Answers (2)

Aditya Nikhade
Aditya Nikhade

Reputation: 1373

Use this permissions in your manifest

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

Set its state to ON using below:

wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
wifiManager.setWifiEnabled(true);

Upvotes: 0

Tom
Tom

Reputation: 4652

WifiManager wifiManager = (WifiManager) this.getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);

Try this, don't know if working.

Upvotes: 1

Related Questions