用android客服端+eps8266+單片機+路由器作了一個遠程控制的系統,由於本身是在實驗室裏,因此把實驗室的門,燈作成了遠程控制的。html
控制距離有多遠------只能說很遠很遠,只要你手機能上網的地方,不對應該是隻要能打電話的地方,不對應該是隻要是移動網(我用的是移動的卡)覆蓋的地方,java
這篇只說明怎麼樣才能實現遠程通訊(在路由器上怎樣設置,wifi模塊eps8266怎樣設置),最後會貼上單片機,android的源碼android
請事先參考個人前幾篇文章編程
實現遠程最好用這兩種服務器
最新的學習文章app
http://www.cnblogs.com/yangfengwu/category/1187355.html (持續更新中)socket
http://www.cnblogs.com/yangfengwu/category/1099910.html (不肯定更新)tcp
有人的設置方法介紹ide
http://www.usr.cn/Faq/146.html學習
http://www.usr.cn/Faq/148.html
另外
在ESP8266使用詳解中介紹過它的鏈接路由器的模式,讓它鏈接上路由器後須要作一些設置
我作的手機tcp客服端
關於上面的遠程 與 近程鏈接
遠程鏈接:
當手機是2G,3G,4G上網的時候,或者,鏈接了wifi模塊所鏈接的路由器的時候,或者所鏈接的路由器橋接了wifi模塊所鏈接的路由器。
遠程鏈接服務器ip是路由器的公網ip
對於通訊端口號是設置wifi模塊所監聽的端口號(它通訊的端口號)
unsigned char code CIPSERVER[]="AT+CIPSERVER=1,10000\r\n";//開啓服務器模式,端口號10000
近程鏈接:
由於我設置了wifi模塊AP 兼 Station 模式,因此模塊會發出無線信號,我鏈接它的wifi信號也能完成通訊,只不過鏈接它的wifi信號後,要鏈接的ip地址爲192.168.4.1
好了附上單片機程序,原本我設置了返回數據,可是由於個人燈和門並無加檢測因此後來就去掉了
#define _MAIN_C_ #include "include.h" sbit L1 = P1^0; sbit L2 = P1^1; sbit L3 = P1^2; sbit L4 = P1^3; sbit kaiguan = P1^4; sbit led1 = P1^5; sbit led2 = P1^6; //AP unsigned char code CWMODE2[]="AT+CWMODE=2\r\n"; //設置模塊的工做的模式爲AP模式 //多鏈接ap+station模式 unsigned char code CWMODE3[]="AT+CWMODE=3\r\n"; //設置模塊的工做的模式爲ap+station模式 unsigned char code CWJAP[]="AT+CWJAP=\"TP-LINK_9750\",\"56565888\"\r\n";//設置鏈接的路由器 unsigned char code RST[]="AT+RST\r\n"; //重啓模塊使AT+CWMODE模式生效 unsigned char code CIPMUX[]="AT+CIPMUX=1\r\n"; //啓動多鏈接 unsigned char code CIPSERVER[]="AT+CIPSERVER=1,10000\r\n";//開啓服務器模式,端口號10000 //微秒延時 /* void us_delay(unsigned char t) { while(t--); } */ //ms秒延時 void Delay_ms(unsigned long ms) { unsigned char i, j,k; for(k=0;k<ms;k++) { _nop_(); _nop_(); i = 22; j = 128; do { while (--j); } while (--i); } } void delay1s(void) //偏差 -0.00000000024us { unsigned char a,b,c; for(c=95;c>0;c--) for(b=26;b>0;b--) for(a=185;a>0;a--); } //ESP8266設置 void ESP8266_Set(unsigned char *puf) { while(*puf!='\0') { Send_Uart(*puf); puf++; } } //多鏈接AP模式 void ManyConnect_AP() { P1=0xff; //模式ap while(1) { ESP8266_Set(CWMODE2);//返回ok delay1s(); if(Usart_AT_flage ==1) { if(strstr(Usart_Receive, "OK") ) { Usart_AT_flage = 0; L1 = 0; break; } } } //復位 ESP8266_Set(RST);//返回一大溜,不用判斷返回 delay1s(); delay1s(); //多鏈接 while(1) { ESP8266_Set(CIPMUX);//返回ok delay1s(); if(Usart_AT_flage ==1) { if(strstr(Usart_Receive, "OK") ) { Usart_AT_flage = 0; L2 = 0; break; } } } //啓動TCP服務 while(1) { ESP8266_Set(CIPSERVER);//返回ok,多了也返回ok delay1s(); if(Usart_AT_flage ==1) { if(strstr(Usart_Receive, "OK") ) { Usart_AT_flage = 0; L3 = 0; break; } } } } //多鏈接AP+Station 模式 void ManyConnect_ap_Station() { P1=0xff; //模式3 while(1) { ESP8266_Set(CWMODE3);//返回ok delay1s(); if(Usart_AT_flage ==1)//接收完成 { if(strstr(Usart_Receive, "OK") ) { Usart_AT_flage = 0; L1 = 0; break; } } } //鏈接路由 while(1) { //復位 ESP8266_Set(RST);//返回一大溜,不用判斷返回 delay1s(); delay1s(); delay1s(); delay1s(); ESP8266_Set(CWJAP);//返回ok delay1s(); delay1s(); delay1s(); delay1s(); delay1s(); delay1s(); delay1s(); delay1s(); delay1s(); delay1s(); delay1s(); delay1s(); delay1s(); delay1s(); if(Usart_AT_flage ==1) { if(strstr(Usart_Receive, "OK") ) { Usart_AT_flage = 0; L2 = 0; break; } } } //多鏈接 while(1) { ESP8266_Set(CIPMUX);//返回ok delay1s(); if(Usart_AT_flage ==1) { if(strstr(Usart_Receive, "OK") ) { Usart_AT_flage = 0; L3 = 0; break; } } } //啓動TCP服務 while(1) { ESP8266_Set(CIPSERVER);//返回ok,多了也返回ok delay1s(); if(Usart_AT_flage ==1) { if(strstr(Usart_Receive, "OK") ) { Usart_AT_flage = 0; L4 = 0; break; } } } } void delay1s500ms(void) //偏差 -0.000000000341us { unsigned char a,b,c; for(c=123;c>0;c--) for(b=212;b>0;b--) for(a=25;a>0;a--); } void main() { int i; InitUART(); while(1) { //ManyConnect_AP();//多鏈接AP模式 ManyConnect_ap_Station();//多鏈接AP+station模式 while(1) { //因爲消息的開頭是+IP 故作此判斷,00000000000000000號 if((Usart_Receive[0]=='+')&&(Usart_Receive[1]=='I')&&(Usart_Receive[2]=='P')) { if((Usart_Receive[3]=='D')&&(Usart_Receive[5]=='0')&&(Usart_Receive[6]==',')) { if(Usart_Receive[9]=='1') { kaiguan = 0; delay1s500ms(); kaiguan = 1; delay1s(); for(i = 0 ; i<20; i++) { Usart_Receive[i]=' '; } //ESP8266_Set("AT+CIPSEND=0,1\r\n"); //delay1s(); //ESP8266_Set("1");//向服務器發送數據 //delay1s(); //ESP8266_Set("AT+CIPSEND=0,1\r\n"); //delay1s(); //ESP8266_Set("1");//向服務器發送數據 } if(Usart_Receive[9]=='2') { kaiguan = 0; delay1s500ms(); kaiguan = 1; delay1s(); for(i = 0 ; i<20; i++) { Usart_Receive[i]=' '; } //ESP8266_Set("AT+CIPSEND=0,1\r\n"); //delay1s(); //ESP8266_Set("2");//向服務器發送數據 //delay1s(); //ESP8266_Set("AT+CIPSEND=0,1\r\n"); //delay1s(); //ESP8266_Set("2");//向服務器發送數據 } if(Usart_Receive[9]=='3') { led1 = 0; delay1s(); delay1s(); //ESP8266_Set("AT+CIPSEND=0,1\r\n"); //delay1s(); //ESP8266_Set("3");//向服務器發送數據 //ESP8266_Set("AT+CIPSEND=0,1\r\n"); //delay1s(); //ESP8266_Set("3");//向服務器發送數據 } if(Usart_Receive[9]=='4') { led1 = 1; delay1s(); delay1s(); /* ESP8266_Set("AT+CIPSEND=0,1\r\n"); delay1s(); ESP8266_Set("4");//向服務器發送數據 ESP8266_Set("AT+CIPSEND=0,1\r\n"); delay1s(); ESP8266_Set("4");//向服務器發送數據*/ } if(Usart_Receive[9]=='5') { led2 = 0; delay1s(); delay1s(); /* ESP8266_Set("AT+CIPSEND=0,1\r\n"); delay1s(); ESP8266_Set("5");//向服務器發送數據 ESP8266_Set("AT+CIPSEND=0,1\r\n"); delay1s(); ESP8266_Set("5");//向服務器發送數據/*/ } if(Usart_Receive[9]=='6') { led2 = 1; delay1s(); delay1s(); /* ESP8266_Set("AT+CIPSEND=0,1\r\n"); delay1s(); ESP8266_Set("6");//向服務器發送數據 ESP8266_Set("AT+CIPSEND=0,1\r\n"); delay1s(); ESP8266_Set("6");//向服務器發送數據*/ } } } //因爲消息的開頭是+IP 故作此判斷,111111111111111111111號 if((Usart_Receive[0]=='+')&&(Usart_Receive[1]=='I')&&(Usart_Receive[2]=='P')) { if((Usart_Receive[3]=='D')&&(Usart_Receive[5]=='1')&&(Usart_Receive[6]==',')) { if(Usart_Receive[9]=='1') { kaiguan = 0; delay1s500ms(); kaiguan = 1; delay1s(); for(i = 0 ; i<20; i++) { Usart_Receive[i]=' '; } } if(Usart_Receive[9]=='2') { kaiguan = 0; delay1s500ms(); kaiguan = 1; delay1s(); for(i = 0 ; i<20; i++) { Usart_Receive[i]=' '; } } if(Usart_Receive[9]=='3') { led1 = 0; delay1s();delay1s(); } if(Usart_Receive[9]=='4') { led1 = 1; delay1s();delay1s(); } if(Usart_Receive[9]=='5') { led2 = 0; delay1s();delay1s(); } if(Usart_Receive[9]=='6') { led2 = 1; delay1s();delay1s(); } } } //因爲消息的開頭是+IP 故作此判斷,222222222222222號 if((Usart_Receive[0]=='+')&&(Usart_Receive[1]=='I')&&(Usart_Receive[2]=='P')) { if((Usart_Receive[3]=='D')&&(Usart_Receive[5]=='2')&&(Usart_Receive[6]==',')) { if(Usart_Receive[9]=='1') { kaiguan = 0; delay1s500ms(); kaiguan = 1; delay1s(); for(i = 0 ; i<20; i++) { Usart_Receive[i]=' '; } } if(Usart_Receive[9]=='2') { kaiguan = 0; delay1s500ms(); kaiguan = 1; delay1s(); for(i = 0 ; i<20; i++) { Usart_Receive[i]=' '; } } if(Usart_Receive[9]=='3') { led1 = 0; delay1s();delay1s(); } if(Usart_Receive[9]=='4') { led1 = 1; delay1s();delay1s(); } if(Usart_Receive[9]=='5') { led2 = 0; delay1s();delay1s(); } if(Usart_Receive[9]=='6') { led2 = 1; delay1s();delay1s(); } } } //因爲消息的開頭是+IP 故作此判斷,00000000000000000號 if((Usart_Receive[0]=='+')&&(Usart_Receive[1]=='I')&&(Usart_Receive[2]=='P')) { if((Usart_Receive[3]=='D')&&(Usart_Receive[5]=='3')&&(Usart_Receive[6]==',')) { if(Usart_Receive[9]=='1') { kaiguan = 0; delay1s500ms(); kaiguan = 1; delay1s(); for(i = 0 ; i<20; i++) { Usart_Receive[i]=' '; } } if(Usart_Receive[9]=='2') { kaiguan = 0; delay1s500ms(); kaiguan = 1; delay1s(); for(i = 0 ; i<20; i++) { Usart_Receive[i]=' '; } } if(Usart_Receive[9]=='3') { led1 = 0; delay1s();delay1s(); } if(Usart_Receive[9]=='4') { led1 = 1; delay1s();delay1s(); } if(Usart_Receive[9]=='5') { led2 = 0; delay1s();delay1s(); } if(Usart_Receive[9]=='6') { led2 = 1; delay1s();delay1s(); } } } //因爲消息的開頭是+IP 故作此判斷,444444444444444444號 if((Usart_Receive[0]=='+')&&(Usart_Receive[1]=='I')&&(Usart_Receive[2]=='P')) { if((Usart_Receive[3]=='D')&&(Usart_Receive[5]=='0')&&(Usart_Receive[6]==',')) { if(Usart_Receive[9]=='1') { kaiguan = 0; delay1s500ms(); kaiguan = 1; delay1s(); for(i = 0 ; i<20; i++) { Usart_Receive[i]=' '; } } if(Usart_Receive[9]=='2') { kaiguan = 0; delay1s500ms(); kaiguan = 1; delay1s(); for(i = 0 ; i<20; i++) { Usart_Receive[i]=' '; } } if(Usart_Receive[9]=='3') { led1 = 0; delay1s();delay1s(); } if(Usart_Receive[9]=='4') { led1 = 1; delay1s();delay1s(); } if(Usart_Receive[9]=='5') { led2 = 0; delay1s();delay1s(); } if(Usart_Receive[9]=='6') { led2 = 1; delay1s();delay1s(); } } } } } }
#define _USART_C_ #include "include.h" unsigned char Usart_Receive[20]={0}; unsigned char Usart_Cnt=0; bit Usart_AT_flage; bit flage; bit Command_Flag; void InitUART(void) { TMOD = 0x20; SCON = 0x50; TH1 = 0xFD; TL1 = TH1; PCON = 0x00; EA = 1; ES = 1; TR1 = 1; } void Send_Uart(unsigned char value) { ES=0; //關閉串口中斷 TI=0; //清發送完畢中斷請求標誌位 SBUF=value; //發送 while(TI==0); //等待發送完畢 TI=0; //清發送完畢中斷請求標誌位 ES=1; //容許串口中斷 } void UARTInterrupt(void) interrupt 4 { if(RI) { RI=0; Usart_Receive[Usart_Cnt]=SBUF;//接收串口數據 Usart_Cnt++;// //返回數據以回車結尾,有回車,並且數據個數大於2,說明接收到了數據 if(Usart_Receive[Usart_Cnt-2]=='\r' && Usart_Receive[Usart_Cnt-1]=='\n' && Usart_Cnt >= 2) { Usart_Cnt = 0;//接收數據計數清零 Usart_AT_flage = 1;//數據接收成功標誌位 } else if(Usart_Cnt > 20)// { Usart_Cnt = 0; } } }
#ifndef __USART_H_ #define __USART_H_ #ifndef _USART_C_ #define _USART_ex_ extern #else #define _USART_ex_ #endif _USART_ex_ unsigned char Usart_Receive[20]; _USART_ex_ unsigned char Usart_Cnt; _USART_ex_ bit Usart_AT_flage; _USART_ex_ bit Command_Flag; _USART_ex_ unsigned char UsartData; _USART_ex_ void InitUART(void);//串口初始化 _USART_ex_ void Send_Uart(unsigned char value); #endif
#include <REGX52.H> #include "USART.h" #include <string.h> #include <intrins.h>
完了,,,,,,,
android代碼
package com.laboratory_control.yang; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import android.R.integer; import android.R.string; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.telephony.TelephonyManager; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { String imei;//手機imei號 long long_mima; String string_mima; private SharedPreferences pref; private SharedPreferences.Editor editor; private CheckBox rememberpass; static Socket socket = null;//定義socket Button shenqing_button;//申請密鑰 Button LandingButton;//登錄按鈕 Button FarButton;//遠程鏈接按鈕 Button NearButton;//進程鏈接按鈕 EditText PasswordEditText;//密碼輸入框 EditText FarIpEditText;//遠程ip輸入框 EditText NearIpEditText;//近程ip輸入框 EditText PortText;//定義端口輸入框 static InputStream inputStream=null;//定義輸入流 static OutputStream outputStream=null;//定義輸出流 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pref = PreferenceManager.getDefaultSharedPreferences(this); rememberpass = (CheckBox) findViewById(R.id.cb_mima); shenqing_button = (Button) findViewById(R.id.shenqingButton);//申請密鑰 LandingButton = (Button) findViewById(R.id.dengluButton);//登錄 FarButton = (Button) findViewById(R.id.yuanButton);//遠程按鈕 NearButton = (Button) findViewById(R.id.jinButton);//進程按鈕 FarIpEditText = (EditText) findViewById(R.id.IPEditText);//遠程ip輸入框 NearIpEditText = (EditText) findViewById(R.id.jinIPEditText);//進程ip輸入框 PortText = (EditText) findViewById(R.id.PORTEditText);//通訊端口 PasswordEditText = (EditText) findViewById(R.id.mimaEditText);//登錄密碼 boolean isRemember = pref.getBoolean("cb_mima", false);//獲得cb_mima文件存的值,得不到會返回false if (isRemember) //若是上次選擇了保存密碼 { //Toast.makeText(MainActivity.this, "123", Toast.LENGTH_SHORT).show(); String password = pref.getString("password", "");//取出密碼 PasswordEditText.setText(password);//把密碼輸入到密碼輸入框 rememberpass.setChecked(true);//選中記住密碼 } shenqing_button.setOnClickListener(shenqing_buttonListener); FarButton.setOnClickListener(FarButtonListener);//遠程監聽 NearButton.setOnClickListener(NearButtonListener);//進程監聽 LandingButton.setOnClickListener(LandingButtonListener);//登錄監聽 /** * 獲取IMEI號,IESI號,手機型號 */ getInfo(); /** * 爲徹底退出應用程序而加的代碼 */ ExitApplication.getInstance().addActivity(this); } /** * 申請密鑰監聽 */ private OnClickListener shenqing_buttonListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"請把"+imei+"提供給管理員",Toast.LENGTH_LONG).show(); } }; /** * 遠程按鈕鏈接監聽 */ private OnClickListener FarButtonListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //啓動遠程鏈接線程 FarConnect_Thread farconnect_Thread = new FarConnect_Thread(); farconnect_Thread.start(); } }; /** * //近程按鈕鏈接監聽 */ private OnClickListener NearButtonListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub NearConnect_Thread nearconnect_Thread = new NearConnect_Thread(); nearconnect_Thread.start(); } }; /** * * 近程鏈接線程 * */ class NearConnect_Thread extends Thread//繼承Thread { public void run()//重寫run方法 { try { if(true) //if (socket == null) { //用InetAddress方法獲取ip地址 InetAddress ipAddress = InetAddress.getByName(NearIpEditText.getText().toString()); int port =Integer.valueOf(PortText.getText().toString());//獲取端口號 socket = new Socket(ipAddress, port);//建立鏈接地址和端口 if (socket !=null) { runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"已成功鏈接!", Toast.LENGTH_SHORT).show(); } }); } } /* else { runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"成功鏈接!輸入密碼後便可登錄", Toast.LENGTH_SHORT).show(); } }); }*/ } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * * 遠程鏈接線程 * */ class FarConnect_Thread extends Thread//繼承Thread { public void run()//重寫run方法 { try { if(true) //if (socket == null) { //用InetAddress方法獲取ip地址 InetAddress ipAddress = InetAddress.getByName(FarIpEditText.getText().toString()); int port =Integer.valueOf(PortText.getText().toString());//獲取端口號 socket = new Socket(ipAddress, port);//建立鏈接地址和端口 if (socket !=null) { runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"成功鏈接!輸入密碼後便可登錄", Toast.LENGTH_SHORT).show(); } }); } } /* else { runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"已經鏈接!", Toast.LENGTH_SHORT).show(); } }); }*/ } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 登錄監聽,啓動控制Activity(活動) */ private OnClickListener LandingButtonListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //if(true) String password = PasswordEditText.getText().toString();//獲得輸入框輸入的密碼 if (socket !=null)//鏈接成功 { if (PasswordEditText.getText().toString().equals(string_mima))//密碼正確 { editor = pref.edit(); if (rememberpass.isChecked()) { editor.putBoolean("cb_mima", true); editor.putString("password", password); } else { editor.clear(); } editor.commit(); Intent intent = new Intent(); intent.setClass(MainActivity.this, Control.class); MainActivity.this.startActivity(intent); } else { runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"已鏈接,請輸入正確密碼!", Toast.LENGTH_SHORT).show(); } }); } } else { runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"請先鏈接!", Toast.LENGTH_SHORT).show(); } }); } } }; /** * 退出提示框 */ protected void dialog() { AlertDialog.Builder builder = new Builder(MainActivity.this); builder.setMessage("親,肯定要退出嗎"); builder.setTitle("提示"); builder.setPositiveButton("肯定", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); MainActivity.this.finish(); } }); builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { dialog(); return false; } return false; } /** * 獲取IMEI號,IESI號,手機型號 */ private void getInfo() { TelephonyManager mTm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE); imei = mTm.getDeviceId(); String aaaa = imei.substring(0,6); long_mima = Integer.parseInt(aaaa); long_mima = long_mima/3+666;//作運算看成登錄密碼 string_mima = ""+long_mima; //String imsi = mTm.getSubscriberId(); //String mtype = android.os.Build.MODEL; // 手機型號 //String mtyb= android.os.Build.BRAND;//手機品牌 //String numer = mTm.getLine1Number(); // 手機號碼,有的可得,有的不可得 Log.i("text", "手機IMEI號:"+imei); Log.i("456", string_mima); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/dd2" tools:context="com.laboratory_control.yang.MainActivity" > <!-- IP地址 --> <TextView android:text="遠程鏈接服務器IP:" android:id="@+id/IPTextView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20dp" /> <!-- 遠程IP地址輸入框 --> <EditText android:text="" android:id="@+id/IPEditText" android:layout_height="35dp" android:layout_width="match_parent" android:layout_marginTop="25dp" android:background="@android:color/white" /> <!-- 近程IP地址 --> <TextView android:text="近程鏈接服務器IP:" android:id="@+id/jinTextView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20dp" android:layout_marginTop="70dp" /> <!-- 近程IP地址輸入框 --> <EditText android:text="192.168.4.1" android:id="@+id/jinIPEditText" android:layout_height="35dp" android:layout_width="match_parent" android:layout_marginTop="95dp" android:background="@android:color/white" /> <!-- 端口號 --> <TextView android:text="通訊端口號:" android:id="@+id/PORTTextView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20dp" android:layout_marginTop="140dp" /> <!-- 端口號輸入框 --> <EditText android:text="10000" android:id="@+id/PORTEditText" android:layout_height="35dp" android:layout_width="fill_parent" android:layout_marginTop="165dp" android:background="@android:color/white" /> <!-- 密碼 --> <TextView android:text="登錄密碼:" android:id="@+id/mimaTextView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="210dp" android:textSize="20dp" /> <!-- 密碼輸入框 --> <EditText android:id="@+id/mimaEditText" android:layout_height="35dp" android:layout_width="fill_parent" android:layout_marginTop="235dp" android:hint="請輸入登錄密碼" android:inputType="textPassword" android:background="@android:color/white" /> <!-- 記住密碼複選框 --> <CheckBox android:id="@+id/cb_mima" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/mimaEditText" android:textSize="20dp" android:text="記住密碼" /> <!-- 近程按鈕 --> <Button android:id="@+id/jinButton" android:layout_height="70dp" android:layout_width="100dp" android:text="近程鏈接" android:layout_alignParentRight="true" android:layout_marginTop="380dp" /> <!-- 登錄按鈕 --> <Button android:id="@+id/dengluButton" android:layout_height="70dp" android:layout_width="100dp" android:text="登錄" android:layout_marginTop="380dp" android:layout_centerHorizontal="true" /> <!-- 遠程鏈接按鈕 --> <Button android:id="@+id/yuanButton" android:layout_height="70dp" android:layout_width="100dp" android:text="遠程鏈接" android:layout_alignParentLeft="true" android:layout_marginTop="380dp" /> <!-- 申請密鑰 --> <Button android:id="@+id/shenqingButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="申請密鑰" android:layout_alignParentRight="true" android:layout_marginTop="270dp" /> </RelativeLayout>
package com.laboratory_control.yang; import java.util.LinkedList; import java.util.List; import android.app.Activity; import android.app.Application; public class ExitApplication extends Application { private List<Activity> activityList = new LinkedList<Activity>(); private static ExitApplication instance; private ExitApplication() { } //單例模式中獲取惟一的ExitApplication實例 public static ExitApplication getInstance() { if(null==instance) { instance=new ExitApplication(); } return instance; } //添加Activity到容器中 public void addActivity(Activity activity) { activityList.add(activity); } //遍歷全部Activity並finish public void exit() { for(Activity activity:activityList) { activity.finish(); } System.exit(0); } }
package com.laboratory_control.yang; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.Toast; public class Control extends MainActivity { boolean door_lamp = true; boolean room_lamp = true; boolean door = true; ImageButton door_ImageButton;//門口燈按鈕 ImageView doorlampImageView;//門口燈圖片 ImageButton room_ImageButton;//門口燈按鈕 ImageView roomlampImageView;//門口燈圖片 ImageButton controlroom_ImageButton;//門按鈕 ImageView doorImageView;//門圖片 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.control_main); door_ImageButton = (ImageButton) findViewById(R.id.door_ImageButton); doorlampImageView = (ImageView) findViewById(R.id.DoorLamp_ImageView); room_ImageButton = (ImageButton) findViewById(R.id.room_ImageButton); roomlampImageView = (ImageView) findViewById(R.id.roomLamp_ImageView); controlroom_ImageButton = (ImageButton) findViewById(R.id.Control_door_ImageButton); doorImageView = (ImageView) findViewById(R.id.door_ImageView); /** * 門口燈按鈕監聽 */ door_ImageButton.setOnClickListener(door_ImageButton_Listener); /** * 屋裏燈按鈕監聽 */ room_ImageButton.setOnClickListener(room_ImageButton_Listener); /** * 門按鈕監聽 */ controlroom_ImageButton.setOnClickListener(controlroom_ImageButton_Listener); /** * 爲徹底退出應用程序而加的代碼 */ ExitApplication.getInstance().addActivity(this); } /** * 門口燈按鈕監聽事件 */ private OnClickListener door_ImageButton_Listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (door_lamp) //打開門口燈 { door_lamp = false; try { //獲取輸出流 outputStream = socket.getOutputStream(); //發送數據 outputStream.write("3".getBytes()); Toast.makeText(Control.this, "已發送打開門口燈命令", Toast.LENGTH_SHORT).show(); doorlampImageView.setImageResource(R.drawable.liangdeng); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else //關閉門口燈 { door_lamp = true; try { //獲取輸出流 outputStream = socket.getOutputStream(); //發送數據 outputStream.write("4".getBytes()); Toast.makeText(Control.this, "已發送關閉門口燈命令", Toast.LENGTH_SHORT).show(); doorlampImageView.setImageResource(R.drawable.heideng); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; /** * 屋裏燈按鈕監聽事件 */ private OnClickListener room_ImageButton_Listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (room_lamp) //打開屋裏燈 { room_lamp = false; try { //獲取輸出流 outputStream = socket.getOutputStream(); //發送數據 outputStream.write("5".getBytes()); Toast.makeText(Control.this, "已發送打屋裏燈燈命令", Toast.LENGTH_SHORT).show(); roomlampImageView.setImageResource(R.drawable.liangdeng); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else //關閉屋裏燈 { room_lamp = true; try { //獲取輸出流 outputStream = socket.getOutputStream(); //發送數據 outputStream.write("6".getBytes()); Toast.makeText(Control.this, "已發送關閉屋裏燈命令", Toast.LENGTH_SHORT).show(); roomlampImageView.setImageResource(R.drawable.heideng); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; /** * 門按鈕監聽事件 */ private OnClickListener controlroom_ImageButton_Listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (door) //打開門 { door = false; try { //獲取輸出流 outputStream = socket.getOutputStream(); //發送數據 outputStream.write("1".getBytes()); Toast.makeText(Control.this, "已發送打開門命令", Toast.LENGTH_SHORT).show(); doorImageView.setImageResource(R.drawable.men222); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else //關閉門 { door = true; try { //獲取輸出流 outputStream = socket.getOutputStream(); //發送數據 outputStream.write("2".getBytes()); Toast.makeText(Control.this, "已發送關閉門命令", Toast.LENGTH_SHORT).show(); doorImageView.setImageResource(R.drawable.men111); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; protected void dialog() { AlertDialog.Builder builder = new Builder(Control.this); builder.setMessage("親,您是想?"); builder.setTitle("提示"); builder.setPositiveButton("直接退出", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); /** * 徹底退出應用程序 */ ExitApplication.getInstance().exit(); } }); builder.setNegativeButton("返回上一層", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Control.this.finish(); } }); builder.create().show(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { dialog(); return false; } return false; } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" > <!-- 門口燈按鈕 --> <ImageButton android:id="@+id/door_ImageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="30dp" android:layout_marginTop="5dp" android:background="@android:color/black" android:src="@drawable/gnome_panel_force_quit" /> <!-- 門口燈圖片 --> <ImageView android:id="@+id/DoorLamp_ImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="16dp" android:background="@android:color/white" android:src="@drawable/heideng" /> <!-- 屋裏燈按鈕 --> <ImageButton android:id="@+id/room_ImageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="155dp" android:layout_alignRight="@id/door_ImageButton" android:background="@android:color/black" android:src="@drawable/gnome_panel_force_quit" /> <!-- 屋裏燈燈圖片 --> <ImageView android:id="@+id/roomLamp_ImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/DoorLamp_ImageView" android:layout_marginTop="150dp" android:background="@android:color/white" android:src="@drawable/heideng" /> <!-- 門口燈開關 --> <TextView android:text="門口燈控制" android:textColor="@android:color/holo_green_dark" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="35dp" android:layout_centerHorizontal="true" /> <!-- 屋裏燈開關 --> <TextView android:text="屋裏燈控制" android:textColor="@android:color/holo_green_dark" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="190dp" android:layout_centerHorizontal="true" /> <!-- 門控制按鈕 --> <ImageButton android:id="@+id/Control_door_ImageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="305dp" android:layout_alignRight="@id/door_ImageButton" android:background="@android:color/black" android:src="@drawable/gnome_panel_force_quit" /> <!-- 門開關 --> <TextView android:text="門控制" android:textColor="@android:color/holo_green_dark" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="337dp" android:layout_centerHorizontal="true" /> <!-- 門圖片 --> <ImageView android:id="@+id/door_ImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/DoorLamp_ImageView" android:layout_alignTop="@+id/Control_door_ImageButton" android:background="@android:color/white" android:src="@drawable/men111" /> </RelativeLayout>
權限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.laboratory_control.yang" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" /> <!-- 獲取手機信息權限 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <application android:allowBackup="true" android:icon="@drawable/a333333" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Control" android:label="Control" ></activity> </application> </manifest>
用到的圖片就不貼了
其實只要看過前幾片文章,作這個絕對沒問題。
我還在程序裏設置了申請密碼,讀手機的IMEI號 ,而後作了一些運算後當成登錄密碼,(運算方法只有我和信任的人本身知道,凡是想用這款軟件控制實驗室,必須經過咱們對他的手機的IMEI號進行運算後才能夠獲得登錄密碼)我是爲了保證每一個人的手機安裝軟件後都有本身單獨的登錄密碼,防止其餘人安裝軟件後就能夠控制實驗室。
上面有運算過程,我已經修改了。防止意外,如有問題QQ946029359
說明一下,其實軟件就是普通的TCP客戶端,只不過經過設置IP地址而實現的遠程控制,
最新版TCPClient請下載這個,
如今接着說,軟件下載的地址,可在百度手機助手,安卓市場,91助手下載更新,這個就是作的最終版的Android軟件,直接綁定設備實現遠程控制
今天剛作好
使用請看這一系列文章
http://www.cnblogs.com/yangfengwu/category/1187355.html
後期準備也會完善實驗室這個軟件,由於這是當年剛入門Android時作的軟件...反正有各類問題...........
軟件能夠在上搜索實驗室
關於密碼列如個人手機
取前六位863328除6 =143888而後減一 = 143887
由於路由器的公網IP會變,能夠用花生殼申請一個域名,這樣登錄的時候輸入域名就好了.解決公網IP會變的麻煩..這是網友雲霄(QQ名稱)告訴個人..謝謝他