【转】手机 蓝牙 控制 继电器 无线门锁 物联网 安卓源码 安卓开发 Android WIFI控...
一、功能:1.AndroidWIFI连接与断开,保存连接IP及端口。2.WIFI控制3个继电器3.16进制发送指令,可以单条和多条发送,用逗号隔开。4.16进制接收如需特别功能可以找我定制(程序源码,定制专家 手机:18910620895 旺旺:chenhao0568QQ:345139427)。二、源码如下:登陆Login.Javaview plain copy
https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg
[*]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;
[*] }
[*] }
[*] );
[*]
[*]}
先上图:
http://img.blog.csdn.net/20150510091647831?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmhhbzA1Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Centerhttp://img.blog.csdn.net/20150510091652527?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmhhbzA1Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Centerhttp://img.blog.csdn.net/20150510091853211?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmhhbzA1Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Centerhttp://img.blog.csdn.net/20150510091702433?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmhhbzA1Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Centerhttp://img.blog.csdn.net/20150510091705662?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmhhbzA1Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Centerhttp://img.blog.csdn.net/20150510091710186?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmhhbzA1Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Centerhttp://img.blog.csdn.net/20150510091909467?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmhhbzA1Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Centerhttp://img.blog.csdn.net/20150510091641030?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmhhbzA1Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Centerhttp://img.blog.csdn.net/20150510091916705?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmhhbzA1Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center文章出自:http://blog.csdn.net/chenhao0568/article/details/45618345
页:
[1]