package com.example.led;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import static java.lang.System.out;
import static java.lang.System.runFinalization;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
volatile Boolean flag_SRX =null;
private Handler handler=null;
private volatile Boolean flag=null;
private Bundle bundle=null;
private Button button=null;
private Button mLedON=null;
private Button mLedOFF=null;
private EditText editText1=null;
private EditText editText2=null;
private Socket socket=null;
private BufferedReader bufferedReader=null;
private BufferedWriter bufferedWriter=null;
@SuppressLint("HandlerLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final boolean loop = true;
flag = false;
flag_SRX = false;
bundle = new Bundle();
button = (Button) findViewById(R.id.mBtnConnect);
mLedON = (Button) findViewById(R.id.mLedON);
mLedOFF = (Button) findViewById(R.id.mLedOFF);
editText1 = (EditText) findViewById(R.id.mIp);
final EditText editText2 = (EditText) findViewById(R.id.mPort);
handler = new Handler() { //测试子线程给主线程传输数据
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
Log.i("handler", "处理函数");
if (msg.what == 0x11) {
}runFinalization();
if (msg.what == 0x22) button.setText("关闭连接");
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!flag) {
new Thread(new Runnable() {
@Override
public void run() {
try {
char time = 0;
socket = new Socket(editText1.getText().toString(), Integer.valueOf(editText2.getText().toString()));
bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.i("开始循环等待!", "Warring");
while (socket.isConnected() == false) {
try {
Thread.sleep(100);
time++;
if (time == 10) {
Log.i("等待时间到,退出等待!", "Warring");
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag_SRX = true;
flag = true;
handler.sendEmptyMessage(0x22);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} else {
try {
bufferedReader.close();
bufferedWriter.close();
socket.close();
Log.i("关闭连接!", "Warring");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
bufferedWriter.close();
socket.close();
socket = null;
} catch (IOException e) {
e.printStackTrace();
}
}
flag = false;
flag_SRX = false;
button.setText("连接网络");
}
}
});
mLedON.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (socket == null) return;
new Thread(new Runnable() {
@Override
public void run() {
try {
bufferedWriter.append("1\r\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Log.i("开灯", "TCP上");
}
}).start();
}
});
mLedOFF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (socket == null) return;
new Thread(new Runnable() {
@Override
public void run() {
try {
bufferedWriter.append("0\r\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Log.i("关灯", "TCP上");
}
}).start();
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
毕设&课程作业_智能灯光控制系统.zip
共581个文件
h:112个
d:102个
c:102个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 59 浏览量
2024-02-29
09:27:17
上传
评论
收藏 10.55MB ZIP 举报
温馨提示
计算机类毕业设计、课程作业,系统源码!!!
资源推荐
资源详情
资源评论
收起资源包目录
毕设&课程作业_智能灯光控制系统.zip (581个子文件)
smartlamp.axf 594KB
PWM.opt.bak 4KB
PWM.Uv2.bak 3KB
smartlamp_sct.Bak 494B
gradlew.bat 2KB
gradlew.bat 2KB
stm32f1xx_hal_i2c.c 174KB
stm32f1xx_hal_tim.c 161KB
stm32f10x_tim.c 107KB
stm32f1xx_hal_spi.c 99KB
stm32f1xx_hal_sd.c 88KB
stm32f1xx_hal_uart.c 85KB
stm32f1xx_hal_mmc.c 81KB
stm32f1xx_hal_adc.c 79KB
stm32f1xx_hal_usart.c 77KB
stm32f1xx_hal_irda.c 72KB
stm32f1xx_hal_eth.c 69KB
stm32f1xx_hal_smartcard.c 66KB
stm32f1xx_ll_usb.c 65KB
stm32f10x_flash.c 61KB
stm32f1xx_hal_tim_ex.c 61KB
stm32f1xx_hal_nand.c 60KB
stm32f1xx_hal_can.c 56KB
stm32f1xx_hal_adc_ex.c 52KB
stm32f1xx_hal_rtc.c 51KB
stm32f10x_rcc.c 50KB
stm32f1xx_hal_rcc.c 49KB
stm32f1xx_hal_i2s.c 48KB
stm32f1xx_ll_sdmmc.c 48KB
stm32f10x_adc.c 46KB
stm32f1xx_ll_tim.c 45KB
stm32f10x_i2c.c 45KB
stm32f10x_can.c 44KB
stm32f1xx_ll_adc.c 41KB
stm32f1xx_hal_pcd.c 39KB
stm32f10x_usart.c 37KB
stm32f1xx_hal_flash_ex.c 36KB
system_stm32f10x.c 36KB
system_stm32f10x.c 36KB
stm32f1xx_ll_fsmc.c 35KB
stm32f10x_fsmc.c 35KB
stm32f1xx_hal_hcd.c 34KB
stm32f1xx_hal_nor.c 33KB
stm32f1xx_hal_dac.c 32KB
stm32f1xx_hal_rcc_ex.c 31KB
stm32f1xx_hal_flash.c 30KB
stm32f10x_spi.c 30KB
stm32f10x_dma.c 29KB
stm32f1xx_hal_dma.c 28KB
stm32f10x_sdio.c 28KB
stm32f1xx_hal_dac_ex.c 24KB
stm32f1xx_hal_pccard.c 24KB
stm32f1xx_ll_utils.c 24KB
stm32f10x_gpio.c 23KB
stm32f1xx_hal_sram.c 22KB
stm32f1xx_hal_gpio.c 22KB
stm32f1xx_hal_pwr.c 22KB
EdpKit.c 21KB
stm32f1xx_ll_spi.c 21KB
stm32f1xx_hal_cec.c 21KB
stm32f1xx_hal.c 21KB
stm32f1xx_hal_cortex.c 20KB
stm32f10x_dac.c 19KB
stm32f1xx_ll_rtc.c 18KB
stm32f1xx_ll_usart.c 18KB
stm32f1xx_hal_rtc_ex.c 17KB
core_cm3.c 17KB
stm32f1xx_ll_rcc.c 15KB
stm32f1xx_ll_dma.c 14KB
stm32f1xx_hal_wwdg.c 12KB
sample.c 12KB
stm32f10x_cec.c 11KB
stm32f1xx_ll_dac.c 11KB
stm32f1xx_hal_timebase_rtc_alarm_template.c 11KB
stm32f1xx_hal_crc.c 11KB
stm32f1xx_hal_iwdg.c 10KB
usart.c 9KB
stm32f1xx_ll_gpio.c 9KB
stm32f1xx_ll_i2c.c 9KB
stm32f10x_pwr.c 9KB
stm32f1xx_hal_spi_ex.c 8KB
stm32f10x_rtc.c 8KB
stm32f1xx_ll_exti.c 8KB
stm32f10x_bkp.c 8KB
stm32f1xx_hal_pcd_ex.c 8KB
misc.c 7KB
esp8266.c 7KB
stm32f10x_exti.c 7KB
stm32f1xx_hal_timebase_tim_template.c 6KB
malloc.c 6KB
stm32f10x_wwdg.c 6KB
usart.c 6KB
stm32f1xx_hal_gpio_ex.c 5KB
stm32f10x_dbgmcu.c 5KB
delay.c 5KB
stm32f10x_iwdg.c 5KB
onenet.c 4KB
timer.c 4KB
stm32f1xx_ll_crc.c 4KB
stm32f1xx_ll_pwr.c 3KB
共 581 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
学术菜鸟小晨
- 粉丝: 1w+
- 资源: 5001
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功