首页

android wifi 连接Demo

java

2020-8-10

package com.example.myihome;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
/*import android.annotation.SuppressLint;
 */
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity {

	Timer timer;
	TimerTask task;
	Handler handler;
	Button lamp, air, curtain, safe, leave, go, sleep;
	Intent intent;
	ImageButton safe1, safe2;

	/*
	 * @SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi", "NewApi",
	 * "NewApi", "NewApi", "NewApi", "NewApi", "NewApi", "NewApi", "NewApi" })
	 */public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

		/*
		 * StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		 * .detectDiskReads() .detectDiskWrites() .detectNetwork() // or
		 * .detectAll() for all detectable problems .penaltyLog() .build());
		 * 
		 * StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
		 * .detectLeakedSqlLiteObjects()
		 * .detectLeakedClosableObjects().penaltyLog() .penaltyDeath()
		 * .build());
		 */
		lamp = (Button) findViewById(R.id.lamp);
		air = (Button) findViewById(R.id.air);
		curtain = (Button) findViewById(R.id.curtain);
		safe = (Button) findViewById(R.id.safebtn);
		leave = (Button) findViewById(R.id.leave);
		go = (Button) findViewById(R.id.go);
		sleep = (Button) findViewById(R.id.sleep);

		lamp.setOnClickListener(listener);
		air.setOnClickListener(listener);
		curtain.setOnClickListener(listener);
		safe.setOnClickListener(listener);
		leave.setOnClickListener(listener);
		go.setOnClickListener(listener);
		sleep.setOnClickListener(listener);

		safe1 = (ImageButton) findViewById(R.id.safe1);
		safe2 = (ImageButton) findViewById(R.id.safe2);

		// 两个buttond状态01GIO 返回数据IO= 00状态00

		timer = new Timer();
		task = new TimerTask() {

			public void run() {
				// TODO Auto-generated method stub
				MySocket socket = new MySocket("192.168.1.58", 6510);
				if (socket.connect()) {
					socket.sendMsg("01GIO");
					char ss[] = socket.recvMsg();
					socket.close();
					Message msg = new Message();
					msg.what = 1;
					System.out.println(new String(ss, 5, 2));
					msg.obj = new String(ss, 5, 2);
					handler.sendMessage(msg);
				} else {
					handler.sendEmptyMessage(0);
				}
			}
		};

		handler = new Handler() {

			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 0)
					Toast.makeText(MainActivity.this, "连接失败",
							Toast.LENGTH_SHORT).show();
				else {
					String data = (String) msg.obj;
					if (data.equals("00")) {
						safe1.setImageResource(R.drawable.nook);
						safe2.setImageResource(R.drawable.nook);
					} else if (data.equals("01")) {
						safe1.setImageResource(R.drawable.nook);
						safe2.setImageResource(R.drawable.ok);

					} else if (data.equals("10")) {
						safe1.setImageResource(R.drawable.ok);
						safe2.setImageResource(R.drawable.nook);

					} else if (data.equals("11")) {
						safe1.setImageResource(R.drawable.ok);
						safe2.setImageResource(R.drawable.ok);
					}

				}
			}

		};
		timer.schedule(task, 2000, 2000);

	}

	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		timer.cancel();
	}

	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	private OnClickListener listener = new OnClickListener() 
	{

		public void onClick(View v) {
			// TODO Auto-generated method stub

			switch (v.getId()) {
			case R.id.lamp:
				intent = new Intent(MainActivity.this, lamp.class);
				startActivity(intent);
				finish();
				break;
			case R.id.air:
				intent = new Intent(MainActivity.this, air.class);
				startActivity(intent);
				finish();
				break;
			case R.id.curtain:
				intent = new Intent(MainActivity.this, curtain.class);
				startActivity(intent);
				finish();
				break;
			case R.id.safebtn:
				intent = new Intent(MainActivity.this, safe.class);
				startActivity(intent);
				finish();
				break;
			case R.id.leave:
				// Send("01C11");
				// Send("01S11");
				timer.schedule(task, 2000, 2000);
				MySocket socket = new MySocket("192.168.1.58", 6510);// 创建socket,需要ip和端口号
				if (socket.connect()) {
					socket.sendMsg("01C11");

					socket.recvMsg();
					// socket.sendMsg("01S11");

					// socket.recvMsg();
					socket.close();

				} else {
					Toast.makeText(MainActivity.this, "连接超时",
							Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.go:
				timer.cancel();
				MySocket socket1 = new MySocket("192.168.1.58", 6510);// 创建socket,需要ip和端口号
				if (socket1.connect()) {
					socket1.sendMsg("01S11");
					socket1.recvMsg();
					socket1.sendMsg("0FC111");

					socket1.recvMsg();
					socket1.sendMsg("0FS100");

					socket1.recvMsg();
					socket1.close();
				} else {
					Toast.makeText(MainActivity.this, "连接超时",
							Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.sleep:

				timer.schedule(task, 2000, 2000);
				MySocket socket2 = new MySocket("192.168.1.58", 6510);// 创建socket,需要ip和端口号
				if (socket2.connect()) {
					socket2.sendMsg("01C11");
					socket2.recvMsg();
					socket2.sendMsg("0FC111");

					socket2.recvMsg();
					socket2.sendMsg("0FS010");

					socket2.recvMsg();
					socket2.close();

				} else {
					Toast.makeText(MainActivity.this, "连接超时",
							Toast.LENGTH_SHORT).show();
				}
				break;

			}
		}
	};

	private boolean Send(String args) {
		MySocket socket = new MySocket("192.168.1.56", 6510);// 创建socket,需要ip和端口号
		if (socket.connect()) {
			socket.sendMsg(args);

			socket.recvMsg();

			socket.close();

		} else {
			Toast.makeText(this, "连接超时", Toast.LENGTH_SHORT).show();
		}

		return false;
	}
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
MyIHome_wifi/.classpath , 356
MyIHome_wifi/.project , 810
MyIHome_wifi/.settings/org.eclipse.jdt.core.prefs , 208
MyIHome_wifi/AndroidManifest.xml , 1105
MyIHome_wifi/bin/classes/com/example/myihome/air$1.class , 2784
MyIHome_wifi/bin/classes/com/example/myihome/air.class , 3514
MyIHome_wifi/bin/classes/com/example/myihome/curtain$1.class , 1275
MyIHome_wifi/bin/classes/com/example/myihome/curtain$2.class , 1275
MyIHome_wifi/bin/classes/com/example/myihome/curtain.class , 2183
MyIHome_wifi/bin/classes/com/example/myihome/lamp$1.class , 910
MyIHome_wifi/bin/classes/com/example/myihome/lamp$2.class , 910
MyIHome_wifi/bin/classes/com/example/myihome/lamp.class , 2374
MyIHome_wifi/bin/classes/com/example/myihome/MainActivity$1.class , 2511
MyIHome_wifi/bin/classes/com/example/myihome/MainActivity$2.class , 1484
MyIHome_wifi/bin/classes/com/example/myihome/MainActivity$3.class , 1480
MyIHome_wifi/bin/classes/com/example/myihome/MainActivity.class , 2892
MyIHome_wifi/bin/classes/com/example/myihome/MySocket.class , 3070
MyIHome_wifi/bin/classes/com/example/myihome/R$attr.class , 343
MyIHome_wifi/bin/classes/com/example/myihome/R$drawable.class , 562
MyIHome_wifi/bin/classes/com/example/myihome/R$id.class , 1209
MyIHome_wifi/bin/classes/com/example/myihome/R$layout.class , 520
MyIHome_wifi/bin/classes/com/example/myihome/R$string.class , 516
MyIHome_wifi/bin/classes/com/example/myihome/R$style.class , 398
MyIHome_wifi/bin/classes/com/example/myihome/R.class , 587
MyIHome_wifi/bin/classes/com/example/myihome/safe$1$1.class , 951
MyIHome_wifi/bin/classes/com/example/myihome/safe$1.class , 1216
MyIHome_wifi/bin/classes/com/example/myihome/safe$2.class , 1654
MyIHome_wifi/bin/classes/com/example/myihome/safe.class , 1912
MyIHome_wifi/bin/classes.dex , 17916
MyIHome_wifi/bin/MyIHome.apk , 76675
MyIHome_wifi/bin/res/drawable-hdpi/circle.png , 14760
MyIHome_wifi/bin/res/drawable-hdpi/ic_action_search.png , 409
MyIHome_wifi/bin/res/drawable-hdpi/ic_launcher.png , 4129
MyIHome_wifi/bin/res/drawable-hdpi/lamp.png , 5366
MyIHome_wifi/bin/res/drawable-hdpi/nook.png , 9267
MyIHome_wifi/bin/res/drawable-hdpi/ok.png , 9897
MyIHome_wifi/bin/res/drawable-ldpi/ic_launcher.png , 1756
MyIHome_wifi/bin/res/drawable-mdpi/ic_action_search.png , 311
MyIHome_wifi/bin/res/drawable-mdpi/ic_launcher.png , 2654
MyIHome_wifi/bin/res/drawable-xhdpi/ic_action_search.png , 491
MyIHome_wifi/bin/res/drawable-xhdpi/ic_launcher.png , 5456
MyIHome_wifi/bin/resources.ap_ , 65757
MyIHome_wifi/gen/com/example/myihome/R.java , 2884
MyIHome_wifi/ic_launcher-web.png , 37099
MyIHome_wifi/libs/android-support-v4.jar , 385685
MyIHome_wifi/proguard-project.txt , 781
MyIHome_wifi/project.properties , 563
MyIHome_wifi/res/drawable-hdpi/circle.png , 15464
MyIHome_wifi/res/drawable-hdpi/ic_action_search.png , 3120
MyIHome_wifi/res/drawable-hdpi/ic_launcher.png , 3014
MyIHome_wifi/res/drawable-hdpi/lamp.png , 5542
MyIHome_wifi/res/drawable-hdpi/nook.png , 9633
MyIHome_wifi/res/drawable-hdpi/ok.png , 10148
MyIHome_wifi/res/drawable-ldpi/ic_launcher.png , 1504
MyIHome_wifi/res/drawable-mdpi/ic_action_search.png , 3030
MyIHome_wifi/res/drawable-mdpi/ic_launcher.png , 1969
MyIHome_wifi/res/drawable-xhdpi/ic_action_search.png , 3199
MyIHome_wifi/res/drawable-xhdpi/ic_launcher.png , 4006
MyIHome_wifi/res/layout/activity_main.xml , 3344
MyIHome_wifi/res/layout/air.xml , 2174
MyIHome_wifi/res/layout/curtain.xml , 1217
MyIHome_wifi/res/layout/lamp.xml , 2216
MyIHome_wifi/res/layout/safe.xml , 3367
MyIHome_wifi/res/values/strings.xml , 236
MyIHome_wifi/res/values/styles.xml , 85
MyIHome_wifi/src/com/example/myihome/air.java , 4562
MyIHome_wifi/src/com/example/myihome/curtain.java , 3059
MyIHome_wifi/src/com/example/myihome/lamp.java , 2608
MyIHome_wifi/src/com/example/myihome/MainActivity.java , 6207
MyIHome_wifi/src/com/example/myihome/MySocket.java , 2936
MyIHome_wifi/src/com/example/myihome/safe.java , 3768
没有账号? 忘记密码?

社交账号快速登录