路由器一键桥接Android实现
Posted cxchanpin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了路由器一键桥接Android实现相关的知识,希望对你有一定的参考价值。
開始之前,首先说一下什么叫一键桥接,所谓一键桥接,就是点击一下。就能够对所连接的路由器进行网络中继设置。以实现路由器能够通过已有的无线路由器进行网络訪问。
那么实现这个功能有几种方法呢?能够说有非常多种。仅仅要能够和路由网关进行通信即可。一般能够选择。Socket通信,Http通信。对于Http通信能够选择HttpClient, HttpURLConnection等。在本文中,为了实现的方便。我们选择android的特性之中的一个,webkit。也即是使用WebView控件来实现一键桥接
一、WebView是什么
可參考:
http://blog.csdn.net/chenshijun0101/article/details/7045394
和:
http://blog.csdn.net/t12x3456/article/details/13769731/
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 对桥接中须要的数据进行初始化
* @since 2015-06-17
* @author Leo
*
*/
public class SwitchRouterService extends Service {
/*定义的一些变量。注本文使用的路由器是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("桥接刚開始,进入第一步,共三个步骤");
new Thread(new FirstRunnable()).start();
}
break;
case Constant.WHAT_FIRST: {
isRunning = true;
toast("第一步完毕,进入第二步,共三个步骤");
new Thread(new SecondRunnable()).start();
}
break;
case Constant.WHAT_SECOND: {
isRunning = true;
toast("第二步完毕。进入第三步。共三个步骤");
new Thread(new ThirdRunnable()).start();
}
break;
case Constant.WHAT_THIRD: {
toast("桥接已经完毕。祝你生活愉快");
stopSelf();
}
break;
default:
toast("地球人 我来自火星");
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();
}
}
/*运行第一步,更改模式*/
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();");
}
/*第二步。填写中继源网络信息*/
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 + "‘;document.wireless_apcli.submit();");
}
/*更改信道。本文中使用Auto选项。可在实际需求中进行信道筛选*/
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();document.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");
}
/*对WebView进行初始化,使WebView能够操作JS,并自己主动登录*/
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);
}
});
}
}
三、总结
本文中所使用的方法,是投机取巧了,通过JS代码非常easy的操作配置页面。然后通过WebView操作JS代码,非常easy实现想要实现的功能。
那么实现这个功能有几种方法呢?能够说有非常多种。仅仅要能够和路由网关进行通信即可。一般能够选择。Socket通信,Http通信。对于Http通信能够选择HttpClient, HttpURLConnection等。在本文中,为了实现的方便。我们选择android的特性之中的一个,webkit。也即是使用WebView控件来实现一键桥接
一、WebView是什么
可參考:
http://blog.csdn.net/chenshijun0101/article/details/7045394
和:
http://blog.csdn.net/t12x3456/article/details/13769731/
二、看我实现
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 对桥接中须要的数据进行初始化
* @since 2015-06-17
* @author Leo
*
*/
public class SwitchRouterService extends Service {
/*定义的一些变量。注本文使用的路由器是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("桥接刚開始,进入第一步,共三个步骤");
new Thread(new FirstRunnable()).start();
}
break;
case Constant.WHAT_FIRST: {
isRunning = true;
toast("第一步完毕,进入第二步,共三个步骤");
new Thread(new SecondRunnable()).start();
}
break;
case Constant.WHAT_SECOND: {
isRunning = true;
toast("第二步完毕。进入第三步。共三个步骤");
new Thread(new ThirdRunnable()).start();
}
break;
case Constant.WHAT_THIRD: {
toast("桥接已经完毕。祝你生活愉快");
stopSelf();
}
break;
default:
toast("地球人 我来自火星");
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();
}
}
/*运行第一步,更改模式*/
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();");
}
/*第二步。填写中继源网络信息*/
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 + "‘;document.wireless_apcli.submit();");
}
/*更改信道。本文中使用Auto选项。可在实际需求中进行信道筛选*/
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();document.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");
}
/*对WebView进行初始化,使WebView能够操作JS,并自己主动登录*/
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);
}
});
}
}
三、总结
本文中所使用的方法,是投机取巧了,通过JS代码非常easy的操作配置页面。然后通过WebView操作JS代码,非常easy实现想要实现的功能。
以上是关于路由器一键桥接Android实现的主要内容,如果未能解决你的问题,请参考以下文章