Router One-click Bridging Android Implementation

Source: Internet
Author: User

Before starting, first say what is called a key bridge, the so-called one-key bridging, is a click. Network relay settings can be made to the connected router. To enable the router to access the network via an existing wireless router.

So how do you implement this function in several ways? Can say there are very many. Just be able to communicate with the routing gateway. Generally be able to choose. Socket communication, HTTP communication. For HTTP communication can choose HttpClient, httpurlconnection and so on. In this article, for the convenience of implementation. We choose one of the features of Android, WebKit. That is, using the WebView control to implement one-click Bridging

First, what is WebView?
The following are the references:
http://blog.csdn.net/chenshijun0101/article/details/7045394
And:
http://blog.csdn.net/t12x3456/article/details/13769731/

Second, see me realize


Package com.leo.switchrouter;

Import Android.app.Service;
Import Android.content.Context;
Import android.content.Intent;
Import android.content.SharedPreferences;
Import Android.os.Handler;
Import Android.os.IBinder;
Import Android.os.Message;
Import Android.os.SystemClock;
Import Android.text.TextUtils;
Import Android.webkit.HttpAuthHandler;
Import android.webkit.WebSettings;
Import Android.webkit.WebView;
Import android.webkit.WebViewClient;
Import Android.widget.Toast;

/**
* @see initialization of data required in bridging
* @since 2015-06-17
* @author Leo
*
*/
public class Switchrouterservice extends Service {
/* Some variables are defined. Note the router used in this article is goagent*/

private static final String TAG = "Switchrouterservice";
Private Switchrouterservice mservice = this;
Private Mwifimanager Mwifimanager = null;
Private sharedpreferences sp = null;
Private Handler Mhandler = null;
Private WebView Mwebview = null;

Private String address = "10.10.10.254";
Private String username = "admin";
Private String bpassword = "admin";
Private String Npassword = "0123456789";

Private String SSID = "Hui";
private int mode = 3;
private int type = 0;

Private String URL1 = "http://10.10.10.254/opmode.asp";
Private String url2 = "http://10.10.10.254/wireless/apcli.asp";
Private String url3 = "http://10.10.10.254/wireless/apcli.asp";

Private String network = "";

@Override
Public IBinder Onbind (Intent arg0) {
TODO auto-generated Method Stub
return null;
}

@Override
public void OnCreate () {
TODO auto-generated Method Stub
Super.oncreate ();
Mhandler = new Handler () {

@Override
public void Handlemessage (Message msg) {
TODO auto-generated Method Stub
Super.handlemessage (msg);
int what = Msg.what;
Switch (what) {
Case Constant.what_start: {
IsRunning = true;
Toast ("Bridge has just started, go to the first step, a total of three steps");
New Thread (New Firstrunnable ()). Start ();
}
Break
Case Constant.what_first: {
IsRunning = true;
Toast ("The first step is complete, go to the second step, a total of three steps");
New Thread (New Secondrunnable ()). Start ();
}
Break
Case Constant.what_second: {
IsRunning = true;
Toast ("The second step is complete. Enter the third step. A total of three steps ");
New Thread (New Thirdrunnable ()). Start ();
}
Break
Case Constant.what_third: {
Toast ("Bridge is finished.") Wish you a Happy life ");
Stopself ();
}
Break
Default
Toast ("Earth man I Come from Mars");
Break
}
}

};
InitData ();
Mhandler.sendemptymessage (Constant.what_start);
}

private void Toast (String text) {
Toast.maketext (Mservice, Text, Toast.length_short). Show ();
}

private void InitData () {
Mwifimanager = new Mwifimanager (this);
Initvalues ();
Initwebview ();
}

Private Boolean isrunning = false;
Private static final long DELAY = 5000;

Private class Firstrunnable implements Runnable {
public void Run () {
while (isrunning) {
System.out.println ("I am the First step");
String now = Mwifimanager.getnowwifiinfo (). Getssid ();
SYSTEM.OUT.PRINTLN (Network + "" + Now);
if (Textutils.equals (now, network)) {
Processfirst ();
Delay (delay);
IsRunning = false;
Mhandler.sendemptymessage (Constant.what_first);
}
}
}
}

Private class Secondrunnable implements Runnable {
public void Run () {
while (isrunning) {
System.out.println ("I am the Second step");
String now = Mwifimanager.getnowwifiinfo (). Getssid ();
if (Textutils.equals (now, network)) {
Processsecond ();
IsRunning = false;
Delay (delay);
Mhandler.sendemptymessage (Constant.what_second);
}
}
}
}

Private class Thirdrunnable implements Runnable {
public void Run () {
while (isrunning) {
System.out.println ("I am the Third step");
String now = Mwifimanager.getnowwifiinfo (). Getssid ();
if (Textutils.equals (now, network)) {
Processthird ();
IsRunning = false;
Delay (delay);
Mhandler.sendemptymessage (Constant.what_third);
}
}
}
}

private void delay (long time) {
try {
Thread.Sleep (time);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

/* Run the first step, change the mode */
private void Processfirst () {
URL1 = "http://" + address + "/opmode.asp";
Mwebview.loadurl (URL1);
System.out.println ("First:" + URL1);
Delay (delay);
Mwebview.loadurl ("javascript:document.opmode.opmode[3].checked = True;changemode (); Opmode_submit ();");
}

/* Step two. Fill in the Relay source network information */
private void Processsecond () {
URL2 = "http://" + address + "/wireless/apcli.asp";
Mwebview.loadurl (URL2);
System.out.println ("Second:" + url2);
Delay (delay);
Mwebview.loadurl ("Javascript:document.wireless_apcli.apcli_ssid.value = '")
+ SSID + "';");
Mwebview.loadurl ("javascript:document.wireless_apcli.apcli_mode.options["
+ mode
+ "].selected = true; Securitymodeswitch (); Encryptmodeswitch (); ");
Mwebview.loadurl ("javascript:document.wireless_apcli.apcli_enc.options["
+ Type + "].selected = true; Encryptmodeswitch (); ");
Mwebview.loadurl ("Javascript:document.wireless_apcli.apcli_wpapsk.value = '")
+ Npassword + "';d ocument.wireless_apcli.submit ();");
}
/* Change the channel. Use the Auto option in this article. Channel filtering can be performed in real-world requirements */
private void Processthird () {
URL3 = "http://" + address + "/wireless/basic.asp";
Mwebview.loadurl (URL3);
Delay (delay);
Mwebview.loadurl ("document.wireless_basic.sz11gchannel.options[0].selected = true; Chonchange ();d ocument.wireless_basic.submit (); ");
}

private void Initvalues () {
SP = Getsharedpreferences (Constant.sp_name, context.mode_private);
Address = sp.getstring (constant.key_address, "10.10.10.254");
Username = sp.getstring (constant.key_username, "admin");
Bpassword = sp.getstring (Constant.key_b_password, "admin");
Npassword = sp.getstring (Constant.key_n_password, "1234567890");
SSID = sp.getstring (Constant.key_ssid, "Huicai");

mode = Sp.getint (Constant.key_mode, 3);
Type = Sp.getint (constant.key_type, 0);

Network = sp.getstring (constant.key_network, "Huicai");
}

/* Initialize the WebView so that WebView can operate JS and log on by itself */
private void Initwebview () {
Mwebview = new WebView (this);
WebSettings settings = Mwebview.getsettings ();
Settings.setjavascriptenabled (TRUE);
Mwebview.setwebviewclient (New Webviewclient () {

@Override
public boolean shouldoverrideurlloading (WebView view, String URL) {
TODO auto-generated Method Stub
View.loadurl (URL);
Return super.shouldoverrideurlloading (view, URL);
}

@Override
public void Onreceivedhttpauthrequest (WebView view,
Httpauthhandler handler, string host, String realm) {
TODO auto-generated Method Stub
Handler.proceed (username, bpassword);
Super.onreceivedhttpauthrequest (view, Handler, host, realm);
}

});
}
}


Iii. Summary
The method used in this article is opportunistic, through the JS code very easy Operation Configuration page. Then through the WebView Operation JS Code, very easy to achieve the desired function.



Router One-click Bridging Android Implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.