Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26001] Android custom Module : Unable to connect WIFI of Android 8 devices

GitHub Issuen/a
TypeBug
PriorityNone
StatusClosed
ResolutionInvalid
Resolution Date2018-06-13T18:36:57.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsmodule
ReporterAminul Islam
AssigneeJoshua Quick
Created2018-05-01T17:05:18.000+0000
Updated2018-06-13T18:36:57.000+0000

Description

We are using below code to connect to Android device Wifi programatically. This code is working fine Android devices 7 and below. Same code is not working in Android 8. We have developed sample app in Android studio where we are able to connect to WIFI using wifiManager manager class. Due to this we are suspecting this issue might exists only if we using Appcelerator module. Details are mentioned after the code. Code used to connect :
List scanResultList = getScanningResults();
int res =0;
for (ScanResult result : scanResultList) {

if (result.SSID.equals(networkSSID)) {

try
{
WifiConfiguration wifiConfiguration = createAPConfiguration(
networkSSID, networkPasskey, securityMode);
this.wifiManager.setWifiEnabled(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
wifiConfiguration.SSID = networkSSID;
// int pri = 99;
//wifiConfiguration.priority = wifiConfiguration.priority + 1;
} else {
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.priority = 40;
}
while(!this.wifiManager.pingSupplicant())
{
Thread.sleep(1000);
}
res = this.wifiManager.addNetwork(wifiConfiguration);
Log.i("WIFI_Connect", "# addNetwork returned " + res);
if (res == -1) {
// Get existed network id if it is already added to WiFi network
this.wifiManager.updateNetwork(wifiConfiguration);
res = getExistingNetworkId(networkSSID);
Log.d("WIFI_Connect", "getExistingNetworkId: " + res);
}
this.wifiManager.disconnect();
this.wifiManager.enableNetwork(res, true);
//this.wifiManager.saveConfiguration();
this.wifiManager.reconnect();
return res;
}catch(Exception ex)
{
Log.i("", "AddDynamicWifi--savewifi--Exception--" + ex);
return 1;
}
}
}

return -1;


private WifiConfiguration createAPConfiguration(String networkSSID,
String networkPass, String capabilities) {
this.wifiConfig.allowedKeyManagement.clear();
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.allowedKeyManagement.clear();
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
//wifiConfiguration.priority = 40;
wifiConfiguration.SSID = String.format("\"%s\"",
new Object[] { networkSSID });

Log.i("networkSSID", "=======" + wifiConfiguration.SSID);

if (capabilities.toUpperCase().contains("WEP")) {
Log.i("rht", "Configuring WEP");
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.RSN);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.WPA);
wifiConfiguration.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConfiguration.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP40);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP104);

/* if (networkPass.matches("^[0-9a-fA-F]+$")) {
wifiConfiguration.wepKeys[0] = networkPass;
} else {
wifiConfiguration.wepKeys[0] = "\"".concat(networkPass).concat(
"\"");
}*/
wifiConfiguration.wepKeys[0] = String.format("\"%s\"",
new Object[] { networkPass });
Log.i("networkSSID", "=======" + wifiConfiguration.wepKeys[0]);
wifiConfiguration.wepTxKeyIndex = 0;

} else if (capabilities.toUpperCase().contains("WPA")) {
Log.i("rht", "Configuring WPA");
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.WPA);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.WPA);
wifiConfiguration.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConfiguration.allowedKeyManagement
.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP40);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP104);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfiguration.preSharedKey = String.format("\"%s\"",
new Object[] { networkPass });
Log.i("network password WPA", "=======" + wifiConfiguration.preSharedKey);
//wifiConfiguration.preSharedKey = "\"" + networkPass + "\"";

} else if (capabilities.toUpperCase().contains("PSK")) {
Log.i("rht", "Configuring PSK");
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfiguration.preSharedKey = String.format("\"%s\"",
new Object[] { networkPass });
Log.i("network password PSK", "=======" + wifiConfiguration.preSharedKey);
wifiConfiguration.hiddenSSID = true;
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedKeyManagement
.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.RSN);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.WPA);

} else {

Log.i("rht", "Configuring OPEN network");
wifiConfiguration.allowedKeyManagement
.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.RSN);
wifiConfiguration.allowedProtocols
.set(WifiConfiguration.Protocol.WPA);
wifiConfiguration.allowedAuthAlgorithms.clear();
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP40);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP104);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.TKIP);
}

return wifiConfiguration;

}

Please take a look on attached zip file. Note Our App which is consuming this module is targeted for Titanium SDK 5.5.1.GA. Let me know if you need more information.

Attachments

FileDateSize
NetworkStatusTest.js2018-05-17T23:31:40.000+00002811
Screenshot_20180518-235114.png2018-05-18T17:52:25.000+000073867
wificonnect.zip2018-05-01T17:04:01.000+00004684583

Comments

  1. Joshua Quick 2018-05-01

    Titanium 5.5.1 targets API Level 23 (aka: Android 6.0). I suggest in Android Studio for you to target API Level 23 as well to see if you can reproduce this issue. (I suspect you are using a higher API Level.) Other than that, I don't see how Titanium could cause this issue. You may be running into breaking changes or issues with this API on Android 8.0 as can be seen by other devs here... https://www.google.com/search?q=android+wifimanager+android+8+not+working+site:stackoverflow.com
  2. Joshua Quick 2018-05-17

    I just tested Titanium's "Ti.Network" JavaScript APIs via the attached [^NetworkStatusTest.js] and our "online" and "networkType" properties are definitely working correctly. I've tested it on the following real devices: - Pixel XL (1st generation) running Android 8.0 - Pixel 2 running Android 8.1 I've swiped down the status bar and disconnected/reconnected the device from WIFI by tapping the WIFI symbol. The app's network status labels were updated appropriately. The "online" property was set true when "networkTypeName" was set to "WIFI". I've tested this in Titanium 7.0.0 and 6.0.2. It works. If you're having problems in Titanium 5.x.x, then you may want to double check that the "android.permission.ACCESS_NETWORK_STATE" permission is being added to the AndroidManifest.xml file since it's required to acquire this information.
  3. Joshua Quick 2018-05-18

    From looking at our docs, I can see that description of Ti.Network.online is not correct. It states that indicates the device has Internet access. This is not true. It indicates the device is connected to the network. So, if you're connected to a private network without Internet access, this property will be set true as well. This is the correct behavior and how it work natively on both Android and iOS. We'll be correcting the documentation in the near future. See: [TIDOC-3190]

JSON Source