1. Are you ready for the Galaxy S20? Here is everything we know so far!

Unable to get ip from DHCP server

Discussion in 'Android Apps & Games' started by gautam1237, Feb 14, 2011.

  1. gautam1237

    gautam1237 Lurker
    Thread Starter

    hi,
    I was writing a app which when starts search fr a specific wifi-128
    bit and connects to it..
    I am able to connect to wifi but not getting ip from DHCP server
    rather i am getting ip from zeroconf.
    however when i try to connect my device to wifi by manually going to
    Wifi Settings and then choosing network it gets ip from DHCP.



    package com.wifi;

    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.List;

    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.wifi.WifiConfiguration;
    import android.net.wifi.WifiInfo;
    import android.net.wifi.WifiManager;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    ACTIVITY:

    public class Mywifi extends Activity implements OnClickListener {

    public WifiManager manager;
    BroadcastReceiver receiver;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mywifi);

    //Test
    System.out.println("Mywifi called");

    // Setup UI
    View b1=findViewById(R.id.Scan);
    b1.setOnClickListener(this);
    View b2=findViewById(R.id.Send);
    b2.setOnClickListener(this);

    //Setup Wifi
    manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    //Register Receiver
    IntentFilter filter =new IntentFilter();


    filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

    filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    if (receiver == null)
    receiver = new WifiReceiver(this);
    registerReceiver(receiver,filter);

    }

    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){

    case R.id.Scan:
    manager.startScan();
    Toast.makeText(this, "Scan Started!!",
    Toast.LENGTH_SHORT).show();

    break;
    case R.id.Send:

    try {

    Toast.makeText(this, "Sending before socket!!",
    Toast.LENGTH_SHORT).show();
    Socket clientSocket = new Socket("10.2.28.122", 5000);
    PrintWriter outToServer = new PrintWriter(
    clientSocket.getOutputStream(),true);
    outToServer.println("3 pt 1 0");
    Toast.makeText(this, "Sending!!",
    Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    Toast.makeText(this, "Exception !! :'(",
    Toast.LENGTH_SHORT).show();
    }
    break;
    }
    }
    }


    RECEIVER:



    package com.wifi;

    import java.util.List;



    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.net.DhcpInfo;
    import android.net.wifi.ScanResult;
    import android.net.wifi.WifiConfiguration;
    import android.net.wifi.WifiConfiguration.AuthAlgorithm;
    import android.net.wifi.WifiConfiguration.GroupCipher;
    import android.net.wifi.WifiConfiguration.KeyMgmt;
    import android.net.wifi.WifiInfo;
    import android.net.wifi.WifiManager;
    import android.widget.Toast;

    public class WifiReceiver extends BroadcastReceiver{

    Mywifi mywifi;
    int added=0;
    public WifiReceiver(Mywifi object2) {
    super();
    this.mywifi = object2;
    System.out.println("Wifi Receiver");

    }
    @Override
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    List<ScanResult> results = mywifi.manager.getScanResults();
    Toast.makeText(mywifi, "On Receive Called!!",
    Toast.LENGTH_SHORT).show();
    System.out.println("On Receive Called!!");



    if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)&&
    added==0) {
    Toast.makeText(mywifi,"Scan Results available",
    Toast.LENGTH_SHORT).show();
    int test=0;
    for (ScanResult result:results)
    {
    Toast.makeText(mywifi,"Serching for our wifi",
    Toast.LENGTH_SHORT).show();
    Toast.makeText(mywifi,result.SSID+" Found!!",
    Toast.LENGTH_SHORT).show();
    if(result.SSID.equals("CBSlinksys"))
    {
    Toast.makeText(mywifi,"Our wifi found!! :)",
    Toast.LENGTH_SHORT).show();
    test=1;
    System.out.println("varified");

    //setting up the wifi

    WifiConfiguration mywificonf =new WifiConfiguration();

    mywificonf.SSID ="\""+result.SSID+"\"";
    mywificonf.BSSID=result.BSSID;
    mywificonf.hiddenSSID=false;



    //accessing security
    mywificonf.allowedAuthAlgorithms.clear();
    mywificonf.allowedGroupCiphers.clear();
    mywificonf.allowedKeyManagement.clear();
    mywificonf.allowedPairwiseCiphers.clear();
    mywificonf.allowedProtocols.clear();
    mywificonf.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);

    mywificonf.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
    mywificonf.allowedKeyManagement.set(KeyMgmt.NONE);
    mywificonf.allowedGroupCiphers.set(GroupCipher.WEP40);
    mywificonf.allowedGroupCiphers.set(GroupCipher.WEP104);

    mywificonf.wepKeys[0] = "\"1234567890\"";
    mywificonf.wepKeys[1] = "\"1234567890\"";
    mywificonf.wepKeys[2] = "\"1234567890\"";
    mywificonf.wepKeys[3] = "\"1234567890\"";
    mywificonf.priority=100000;
    mywificonf.wepTxKeyIndex=0;

    //add this to configured network
    int netId = mywifi.manager.addNetwork(mywificonf);
    if(netId < 0) {
    System.out.println("Unable to add network configuration for our
    SSID: "+mywificonf.SSID);
    Toast.makeText(mywifi,"Unable to add network configuration for
    our SSID: "+mywificonf.SSID, Toast.LENGTH_LONG).show();
    added=1;
    return;
    }

    else{
    System.out.println("Updated!!");
    Toast.makeText(mywifi,"Updated!!", Toast.LENGTH_SHORT).show();
    }
    //connet to mywifi
    boolean success=mywifi.manager.enableNetwork(netId, true);
    if(success){
    System.out.println("Successfully connected!! :D");
    Toast.makeText(mywifi,"Successfully Connected !! :D",
    Toast.LENGTH_SHORT).show();
    added=1;

    break;

    }
    else{
    System.out.println("Connection Failed!! :( Returning...");
    Toast.makeText(mywifi,"Connection Failed!! :( Returning...",
    Toast.LENGTH_LONG).show();
    return;
    }

    }
    }
    if(test==0){
    System.out.println("Required Wifi not found!!");
    Toast.makeText(mywifi,"Our Wifi not found!! :(",
    Toast.LENGTH_SHORT).show();
    }
    }

    if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
    {
    Toast.makeText(mywifi,"Network state Changed!!",
    Toast.LENGTH_SHORT).show();
    WifiInfo mywifiinfo= mywifi.manager.getConnectionInfo();
    System.out.println("Current connection
    SSID:"+mywifiinfo.getSSID());
    Toast.makeText(mywifi,"Current connection SSID:
    "+mywifiinfo.getSSID(), Toast.LENGTH_SHORT).show();
    int ipadd=mywifiinfo.getIpAddress();
    System.out.println("Connection ip address:"+ipadd);
    Toast.makeText(mywifi,"Connection ip address: "+ipadd,
    Toast.LENGTH_LONG).show();
    String myipadd;
    myipadd=((ipadd & 0xFF)+"."+(ipadd>>8 & 0xFF)+"."+(ipadd>>16 & 0xFF)
    +"."+(ipadd>>24 & 0xFF));
    Toast.makeText(mywifi,"Connection ip address: "+myipadd,
    Toast.LENGTH_LONG).show();

    }
    }
    }
     

    Advertisement

Loading...

Share This Page

Loading...