Branch hangs on WifiManager.enableNetwork ()

I see that my calling thread hangs in its own code when calling WifiManager.enableNetwork (). So far, I was able to reproduce this hang on a Motorola Xoom tablet running Android 3.2.1. I tested on several other phones and tablets (all of them were either Froyo or Gingerbread) and did not see the problem. Xoom is the only dual-core device I need to test (and I reproduced the problem on two different Xooms), so it seems to me that I am facing some very subtle Android stream requirements when interacting with WifiManager. The stack trace in which my calling thread hangs:

BinderProxy.transact(int, Parcel, Parcel, int) line: not available [native method] IWifiManager$Stub$Proxy.enableNetwork(int, boolean) line: 513 WifiManager.enableNetwork(int, boolean) line: 587 

My application is trying to connect to a known Wi-Fi access point, perform some tests, and then reconnect the device to its original access point (if it was previously connected). Before establishing the connection, we already checked that Wi-Fi is turned on, and we performed a check to check if our SSID for the access point was found. This connection setup code runs in AsyncTask and looks something like this:

 ... private WifiManager mWifiManager; private List<WifiConfiguration> mConfiguredNets = new ArrayList<WifiConfiguration>(); private Object mConnectMonitor = new Object(); private NetworkInfo.State mNetworkState = State.UNKNOWN; private final BroadcastReceiver mConnectionStateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context inContext, final Intent inIntent) { final String action = inIntent.getAction(); if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { NetworkInfo ni = (NetworkInfo)inIntent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); State state = ni.getState(); if (state == State.CONNECTED) { synchronized (mConnectMonitor) { mNetworkState = state; mConnectMonitor.notify(); } } } } }; public void runninInAsyncTask(Context activityContext, int networkID) { mWifiManager = (WifiManager)activityContext.getSystemService(Context.WIFI_SERVICE); // Register our broadcast receiver to get network state change events IntentFilter ifilter = new IntentFilter(); ifilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); activityContext.registerReceiver(mConnectionStateReceiver, ifilter); // Get a list of our currently configured networks so we can re-enable // them after connecting to the desired network mConfiguredNets = mWifiManager.getConfiguredNetworks(); // Enable our network and disable all others mWifiManager.enableNetwork(networkId, true); // Start the reconnection process to connect to our desired network synchronized (mConnectMonitor) { mWifiManager.reconnect(); mConnectMonitor.wait(60000); if (mNetworkState != State.CONNECTED) { Log.e(TAG, "Problems connecting to desired network!"); } else { Log.e(TAG, "Successfully connected to desired network!"); } } // Re-enable all of our previously configured networks for (WifiConfiguration wifiConfig : mConfiguredNets) { if (wifiConfig.status != Status.ENABLED) { mWifiManager.enableNetwork(wifiConfig.networkId, false); } } } ... 

This code was based on the Wifi settings menu code in the open source Android Gingerbread. Is there anything missing about calling WifiManager.enableNetwork () that I am missing? Do I need to run it on a specific thread? I tried to make sure enableNetwork () is called in the user interface thread (by moving the logic to the broadcast receiver). It seemed to help a bit, but I was still able to reproduce the hang. Maybe this is something special for Honeycomb? Right now, these 2 Xooms are the only Honeycomb devices I have for testing, so they are the only data points that I have.

FROM

+6
source share
1 answer

This is really a firmware issue related to 3. * (it seems).

I saw how this happened on the Asus Transformer TF101 and Sony Tablet S (both with 3. *, which was a while ago).

Starting with version 3.0, there are new APIs for connecting to WiFi that do not require the use of enableNetwork in packets (to include all networks except the current one).

More on these APIs that I could compile from source 4.0:

  • They are marked with "@hide"
  • They are used by the Settings app.
  • They are still not documented since 4.1
  • They changed slightly between 3. * and 4. * runtime

My recommendation is to try using these APIs through reflection. Since they are used in the Settings app, they work.

+1
source

Source: https://habr.com/ru/post/900452/


All Articles