1

I'm working on an Android app that connects to custom hardware via networking.

If you set it up to use a wireless access point, it works fine. If you set it up to use an access point on the device (aka tethering), it works fine. If you set it up to use an ad-hoc network, it doesn't work. Android fails at the "obtaining IP address" step.

our work-around was to use a static IP address, and low and behold it worked! If you connect the tablet to our custom hardware and force a custom subnet mask and IP address, it connects instantly.

However, that's too confusing. DHCP or GTFO. Our solution uses a custom network manager, and if it's an ad-hoc network force a static IP address automatically. This should work.

I've followed the directions at this link (with some modifications) and it works perfectly-ish. You can select the network, assign it a static IP address, and that's it. The android tablet will never connect to the network. However, if you look at the network under the "real" wifi manager and select "modify settings", you can see where all of the static IP address information is saved. It just won't connect.

Adding to my misery, if you remove the network, and go to the android wifi manager and enter the information in manually, it works. but not if you do the same steps programatically.

Does anyone know why this is, or how to fix it? This is on a rooted nexus 7.

Thanks in advance.

public void connectToWiFiNetwork(String networkSSID, String networkPass, String type)
{
    //Make sure a password is set
    if(networkPass != null && networkPass.length() == 0)
        return;

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain ssid in quotes
    conf.status = WifiConfiguration.Status.ENABLED;
    int id = -1;

    if(type.contains("WPA")) {

        conf.preSharedKey = "\""+ networkPass +"\"";
        //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        System.out.println("Contains WPA");

    } else if (type.contains("WEP")) {

        conf.wepKeys[0] = "\"" + networkPass + "\"";
        conf.wepTxKeyIndex = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        System.out.println("Contains WEP");

    } else {
        //open network?
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        System.out.println("Open WiFi");
    }



    if(type.contains("IBSS"))
    {
        //Adhoc network. Provide an IP address to prevent... stuff.

        //For Android 2.x
        //final ContentResolver cr = this.context.getContentResolver();
        //Settings.System.putInt(cr, Settings.System.WIFI_USE_STATIC_IP, 1);
        //Settings.System.putString(cr, Settings.System.WIFI_STATIC_IP, "169.254.1.1");
        //Settings.System.putString(cr, Settings.System.WIFI_STATIC_NETMASK, "255.255.0.0");



        try{
            //Some code here isn't used... 
            WifiConfiguration wifiConf = null;
            WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);

            WifiInfo connectionInfo = wifiManager.getConnectionInfo();
            List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
            for (WifiConfiguration conf2 : configuredNetworks){
                if (conf2.networkId == connectionInfo.getNetworkId()){
                    wifiConf = conf2;
                    break;
                }
            }

            wifiConf = conf;

            setIpAssignment("STATIC", wifiConf); //or "DHCP" for dynamic setting
            Log.d("CONNECTION", "Connecting now using a static IP address");
            setIpAddress(InetAddress.getByName("169.254.1.1"), 24, wifiConf);
            Log.d("CONNECTION", "Connecting now using a static IP address");
            setGateway(InetAddress.getByName("255.255.0.0"), wifiConf);
            Log.d("CONNECTION", "Connecting now using a static IP address");
            setDNS(InetAddress.getByName("8.8.8.8"), wifiConf);
            Log.d("CONNECTION", "Connecting now using a static IP address");
            //wifiManager.updateNetwork(wifiConf); //apply the setting
            Log.d("CONNECTION", "Connecting now using a static IP address");

        }catch(Exception e){
            e.printStackTrace();
        }
    }

    id = wifi.addNetwork(conf);
    wifi.saveConfiguration();

    //Add the network

    //And enable it

    List<WifiConfiguration> list = wifi.getConfiguredNetworks();
    for( WifiConfiguration i : list ) {
        if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
            wifi.disconnect();
            wifi.enableNetwork(i.networkId, true);
            wifi.reconnect();
            break;
        }
    }




    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("auto_start_tethering", false);
    editor.commit();

    Log.d("CONNECTION", "Connecting using " + networkSSID + " and " + networkPass + ".");
}
Community
  • 1
  • 1
Drew
  • 1,014
  • 2
  • 10
  • 21

0 Answers0