手机 蓝牙 控制 继电器 无线门锁 物联网 安卓源码 安卓开发 Android WIFI控制 详细注释测试稳定无bug
一、功能:
1.Android WIFI连接与断开,保存连接IP及端口。
2.WIFI控制3个继电器
3.16进制发送指令,可以单条和多条发送,用逗号隔开。
4.16进制接收
如需特别功能可以找我定制(程序源码,定制专家 手机:18910620895 旺旺:chenhao0568 QQ:345139427)。
二、源码如下:
登陆Login.java
package com.example.eeeee; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.annotation.SuppressLint; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; @SuppressLint("NewApi") public class Login<E, K, V> extends Activity { public static Socket mSocket; public static InputStream ips; public static OutputStream ops; // private final String DEBUG_TAG = "mySocketAct"; private String ip; private String port; //private ImageButton but7=null; private EditText ServerIp = null; private EditText ServerPort = null; private Button btn_login = null; private CheckBox remember; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.login); //“登陆”页面------------------------------------------------------------------------------------------- this.ServerIp = (EditText)super.findViewById(R.id.et_ip); this.ServerPort = (EditText)super.findViewById(R.id.et_port); btn_login = (Button)this.findViewById(R.id.btn_login); remember = (CheckBox)this.findViewById(R.id.cb_remember); loadIpPort(); ////载入IP地址及端口 btn_login.setOnClickListener(new View.OnClickListener() { //按“登陆”按钮后的事件 @Override public void onClick(View arg0) { // TODO Auto-generated method stub //容错判断 ip = ServerIp.getText().toString().trim(); port = ServerPort.getText().toString().trim(); ServerIp.requestFocus(); if(ip==null|ip.trim().equals("")) ServerIp.setError("请输入IP地址!"); if(port==null|port.trim().equals("")) ServerPort.setError("请输入端口号!"); else { connect(); } return; } }); } public void saveIpPort() //保存IP地址及端口 { SharedPreferences uiState = getSharedPreferences("RS485 Controller", MODE_PRIVATE); Editor et=uiState.edit(); et.putString("ip",ServerIp.getText().toString()); et.putString("port",ServerPort.getText().toString()); et.commit(); } public void loadIpPort() //载入IP地址及端口 { SharedPreferences uiState = getSharedPreferences("RS485 Controller", MODE_PRIVATE); ServerIp.setText(uiState.getString("ip","192.168.1.180" )); ServerPort.setText(uiState.getString("port", "8899")); } public void connect() { Toast.makeText(Login.this, "连接中……", Toast.LENGTH_SHORT).show(); new Thread(new Runnable() { @Override public void run() { try { mSocket = new Socket(InetAddress.getByName(ip), Integer.parseInt(port)); // mSocket =new Socket("192.168.1.180",8899); ops = mSocket.getOutputStream(); ips =mSocket.getInputStream(); } catch (Exception e) { e.printStackTrace(); } finally { } handler.sendMessage(handler.obtainMessage()); } }).start(); } // 消息处理队列 private Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if (mSocket == null) { Toast.makeText(Login.this, "连接失败,请重新登陆!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(Login.this, "连接成功!", Toast.LENGTH_SHORT).show(); if (remember.isChecked()) { saveIpPort();} //保存IP地址及端口 Intent intent = new Intent(); intent.setClass(Login.this,Relay.class); startActivity(intent); //进入Login页面 // finish(); } return false; } } ); }
先上图:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。