智能空调安卓APP远程控制空调开关和温度(TCP/IP协议)
1.效果2.APP图3.和普通的遥控器做比较众所周知,一般普通的空调遥控器是通过红外线实现控制空调的,但是这种空调遥控器是使用电池的,而且还会被爸妈偷偷藏起来让你热醒…还有一点就是它不能离开空调的视线使用。针对以上的不足,我想做一款可以远程控制的空调手机遥控器,也就是说,出门在外也能控制家里空调的遥控器。例如,今天出门了但是好热,想一回到家就能享受到空调的冷气,但是回到家再开空调也是等好一会儿才能
1.效果
2.APP图
3.和普通的遥控器做比较
众所周知,一般普通的空调遥控器是通过红外线实现控制空调的,但是这种空调遥控器是使用电池的,而且还会被爸妈偷偷藏起来让你热醒…还有一点就是它不能离开空调的视线使用。针对以上的不足,我想做一款可以远程控制的空调手机遥控器,也就是说,出门在外也能控制家里空调的遥控器。例如,今天出门了但是好热,想一回到家就能享受到空调的冷气,但是回到家再开空调也是等好一会儿才能变冷。另外,普通的空调遥控器不够人性化,只能一度一度地调节温度。我写的这款APP,可以从16度直接跳到30度,是不是很赞?哈哈哈…
4.这APP真的能控制空调吗?
普通的遥控器之所以可以控制空调,是因为使用了红外线,但是APP使用的是tcp协议,这样怎么才能控制空调啊?空调也没有带这个协议啊!
我的的实现路线是这样的:
首先单片机配置WiFi模块(自带tcp协议栈的WiFi模块)作为服务器接收安卓APP无线发送过来的命令,当接收到命令的时候,就执行红外发射模块功能,当然,怎么做到远程控制呢?不在同一个局域网的时候还能连接上单片机吗?其实把单片机局域网的ip地址和端口号映射到公网就可以啦,可以使用花生壳。
还有,自己家的空调,怎么知道对应的红外协议呢?每一家的空调遥控器使用的发射命令可能都不太一样。这个可以上网搜索一下就有啦!去年还在学校的时候,就做过远程控制空调的功能,但是那时候是通过电脑java的串口实现控制的,还使用了modbus。
我觉得可以尝试一下的,所以想着先把APP做出来,硬件实现可以先放到后面。
5.安卓APP的实现:
实现环境:Android studio 3.5.2版本
build.gradle(app):
compileSdkVersion 29
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.myclient"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
app主要难度在于自定义view和tcp协议的编写,只要了解过一般都可以写的出来的!加油!
1.MainActivity.java 主活动就是个空壳子,放了个fragment而已,为什么我不直接在主活动实现呢?其实是为了以后APP的开拓。
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.myclient.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/home"
android:name="com.example.myclient.TestFragment"/>
</RelativeLayout>
3.TempControlView.java
public class TempControlView extends View {
// 控件宽
private int width;
// 控件高
private int height;
// 刻度盘半径
private int dialRadius;
// 圆弧半径
private int arcRadius;
// 刻度高
private int scaleHeight = dp2px(10);
// 刻度盘画笔
private Paint dialPaint;
// 圆弧画笔
private Paint arcPaint;
// 标题画笔
private Paint titlePaint;
// 温度标识画笔
private Paint tempFlagPaint;
// 旋转按钮画笔
private Paint buttonPaint;
// 温度显示画笔
private Paint tempPaint;
// 文本提示
private String title = "温度调节";
// 温度
private int temperature = 15;
// 最低温度
private int minTemp = 15;
// 最高温度
private int maxTemp = 5;
// 四格代表温度1度
private int angleRate = 4;
// 每格的角度
private float angleOne = (float) 270 / (maxTemp - minTemp) / angleRate;
// 按钮图片
private Bitmap buttonImage = BitmapFactory.decodeResource(getResources(),
R.mipmap.btn_rotate);
// 按钮图片阴影
private Bitmap buttonImageShadow = BitmapFactory.decodeResource(getResources(),
R.mipmap.btn_rotate_shadow);
// 抗锯齿
private PaintFlagsDrawFilter paintFlagsDrawFilter;
// 温度改变监听
private OnTempChangeListener onTempChangeListener;
// 控件点击监听
private OnClickListener onClickListener;
// 以下为旋转按钮相关
// 当前按钮旋转的角度
private float rotateAngle;
// 当前的角度
private float currentAngle;
/**
* 是否可以旋转
*/
private boolean canRotate = false;
public TempControlView(Context context) {
this(context, null);
}
public TempControlView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TempControlView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
dialPaint = new Paint();
dialPaint.setAntiAlias(true);
dialPaint.setStrokeWidth(dp2px(2));
dialPaint.setStyle(Paint.Style.STROKE);
arcPaint = new Paint();
arcPaint.setAntiAlias(true);
arcPaint.setColor(Color.parseColor("#3CB7EA"));
arcPaint.setStrokeWidth(dp2px(2));
arcPaint.setStyle(Paint.Style.STROKE);
titlePaint = new Paint();
titlePaint.setAntiAlias(true);
titlePaint.setTextSize(sp2px(20));
titlePaint.setColor(Color.parseColor("#FFFFFF"));
titlePaint.setStyle(Paint.Style.STROKE);
tempFlagPaint = new Paint();
tempFlagPaint.setAntiAlias(true);
tempFlagPaint.setTextSize(sp2px(25));
tempFlagPaint.setColor(Color.parseColor("#FFFFFF"));
tempFlagPaint.setStyle(Paint.Style.STROKE);
buttonPaint = new Paint();
tempFlagPaint.setAntiAlias(true);
paintFlagsDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
tempPaint = new Paint();
tempPaint.setAntiAlias(true);
tempPaint.setTextSize(sp2px(40));
tempPaint.setColor(Color.parseColor("#3B434E"));
tempPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// 控件宽、高
width = height = Math.min(h, w);
// 刻度盘半径
dialRadius = width / 2 - dp2px(20);
// 圆弧半径
arcRadius = dialRadius - dp2px(20);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawScale(canvas);
drawArc(canvas);
drawText(canvas);
drawButton(canvas);
drawTemp(canvas);
}
/**
* 绘制刻度盘
*
* @param canvas 画布
*/
private void drawScale(Canvas canvas) {
canvas.save();
canvas.translate(getWidth() / 2, getHeight() / 2);
// 顺时针旋转135-2度
canvas.rotate(133);
//未达到的温度
dialPaint.setColor(Color.parseColor("#00ff00"));
for (int i = angleRate * maxTemp; i > angleRate * temperature; i--) {
canvas.drawLine(0, -dialRadius, 0, -dialRadius + scaleHeight, dialPaint);
canvas.rotate(-angleOne);
}
//已经达到的温度
dialPaint.setColor(Color.parseColor("#E37364"));
for (int i = temperature * angleRate; i >= minTemp * angleRate; i--) {
canvas.drawLine(0, -dialRadius, 0, -dialRadius + scaleHeight, dialPaint);
canvas.rotate(-angleOne);
}
canvas.restore();
}
/**
* 绘制刻度盘下的圆弧
* @param canvas 画布
*/
private void drawArc(Canvas canvas) {
canvas.save();
canvas.translate(getWidth() / 2, getHeight() / 2);
canvas.rotate(135 + 2);
RectF rectF = new RectF(-arcRadius, -arcRadius, arcRadius, arcRadius);
canvas.drawArc(rectF, 0, 265, false, arcPaint);
canvas.restore();
}
/**
* 绘制标题与温度标识
* @param canvas 画布
*/
private void drawText(Canvas canvas) {
canvas.save();
// 绘制标题
float titleWidth = titlePaint.measureText(title);
canvas.drawText(title, (width - titleWidth) / 2, dialRadius * 2 + dp2px(15), titlePaint);
// 绘制最小温度标识
// 最小温度如果小于10,显示为0x
String minTempFlag = "";
if (minTemp <= 0) {
minTempFlag = minTemp + "";
} else {
minTempFlag = minTemp < 10 ? "0" + minTemp : minTemp + "";
}
float tempFlagWidth = titlePaint.measureText(maxTemp + "");
canvas.rotate(55, width / 2, height / 2);
canvas.drawText(minTempFlag, (width - tempFlagWidth) / 2, height + dp2px(5), tempFlagPaint);
// 绘制最大温度标识
canvas.rotate(-105, width / 2, height / 2);
canvas.drawText(maxTemp + "", (width - tempFlagWidth) / 2, height + dp2px(5), tempFlagPaint);
canvas.restore();
}
/**
* 绘制旋转按钮
* @param canvas 画布
*/
private void drawButton(Canvas canvas) {
// 按钮宽高
int buttonWidth = buttonImage.getWidth();
int buttonHeight = buttonImage.getHeight();
// 按钮阴影宽高
int buttonShadowWidth = buttonImageShadow.getWidth();
int buttonShadowHeight = buttonImageShadow.getHeight();
// 绘制按钮阴影
canvas.drawBitmap(buttonImageShadow, (width - buttonShadowWidth) / 2,
(height - buttonShadowHeight) / 2, buttonPaint);
Matrix matrix = new Matrix();
// 设置按钮位置,移动到控件中心
matrix.setTranslate((width - buttonWidth) / 2, (height - buttonHeight) / 2);
// 设置旋转角度,旋转中心为控件中心,当前也是按钮中心
matrix.postRotate(45 + rotateAngle, width / 2, height / 2);
//设置抗锯齿
canvas.setDrawFilter(paintFlagsDrawFilter);
canvas.drawBitmap(buttonImage, matrix, buttonPaint);
}
/**
* 绘制温度
*
* @param canvas 画布
*/
private void drawTemp(Canvas canvas) {
canvas.save();
canvas.translate(getWidth() / 2, getHeight() / 2);
float tempWidth = tempPaint.measureText(temperature + "");
float tempHeight = (tempPaint.ascent() + tempPaint.descent()) / 2;
// if (temperature==15){
// canvas.drawText("off", -tempWidth-30 - dp2px(5), -tempHeight, tempPaint);
// }else{
canvas.drawText(temperature + "℃", -tempWidth-30 - dp2px(5), -tempHeight, tempPaint);
// }
canvas.restore();
}
private boolean isDown;
private boolean isMove;
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!canRotate) {
return super.onTouchEvent(event);
} else {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isDown = true;
float downX = event.getX();
float downY = event.getY();
currentAngle = calcAngle(downX, downY);
break;
case MotionEvent.ACTION_MOVE:
isMove = true;
float targetX;
float targetY;
downX = targetX = event.getX();
downY = targetY = event.getY();
float angle = calcAngle(targetX, targetY);
// 滑过的角度增量
float angleIncreased = angle - currentAngle;
// 防止越界
if (angleIncreased < -270) {
angleIncreased = angleIncreased + 360;
} else if (angleIncreased > 270) {
angleIncreased = angleIncreased - 360;
}
IncreaseAngle(angleIncreased);
currentAngle = angle;
invalidate();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
if (isDown) {
if (isMove) {
// 纠正指针位置
rotateAngle = (float) ((temperature - minTemp) * angleRate * angleOne);
invalidate();
// 回调温度改变监听
if (onTempChangeListener != null) {
onTempChangeListener.change(temperature);
}
isMove = false;
} else {
// 点击事件
if (onClickListener != null) {
onClickListener.onClick(temperature);
}
}
isDown = false;
}
break;
}
}
return true;
}
}
/**
* 以按钮圆心为坐标圆点,建立坐标系,求出(targetX, targetY)坐标与x轴的夹角
*
* @param targetX x坐标
* @param targetY y坐标
* @return (targetX, targetY)坐标与x轴的夹角
*/
private float calcAngle(float targetX, float targetY) {
float x = targetX - width / 2;
float y = targetY - height / 2;
double radian;
if (x != 0) {
float tan = Math.abs(y / x);
if (x > 0) {
if (y >= 0) {
radian = Math.atan(tan);
} else {
radian = 2 * Math.PI - Math.atan(tan);
}
} else {
if (y >= 0) {
radian = Math.PI - Math.atan(tan);
} else {
radian = Math.PI + Math.atan(tan);
}
}
} else {
if (y > 0) {
radian = Math.PI / 2;
} else {
radian = -Math.PI / 2;
}
}
return (float) ((radian * 180) / Math.PI);
}
/**
* 增加旋转角度
*
* @param angle 增加的角度
*/
private void IncreaseAngle(float angle) {
rotateAngle += angle;
if (rotateAngle < 0) {
rotateAngle = 0;
} else if (rotateAngle > 270) {
rotateAngle = 270;
}
// 加上0.5是为了取整时四舍五入
temperature = (int) ((rotateAngle / angleOne) / angleRate + 0.5) + minTemp;
}
/**
* 设置几格代表1度,默认4格
*
* @param angleRate 几格代表1度
*/
public void setAngleRate(int angleRate) {
this.angleRate = angleRate;
}
/**
* 设置温度
*
* @param temp 设置的温度
*/
public void setTemp(int temp) {
setTemp(minTemp, maxTemp, temp);
}
/**
* 设置温度
*
* @param minTemp 最小温度
* @param maxTemp 最大温度
* @param temp 设置的温度
*/
public void setTemp(int minTemp, int maxTemp, int temp) {
this.minTemp = minTemp;
this.maxTemp = maxTemp;
if (temp < minTemp) {
this.temperature = minTemp;
} else {
this.temperature = temp;
}
// 计算每格的角度
angleOne = (float) 270 / (maxTemp - minTemp) / angleRate;
// 计算旋转角度
rotateAngle = (float) ((temp - minTemp) * angleRate * angleOne);
invalidate();
}
/**
* 设置旋钮是否可以旋转
*
* @param canRotate
*/
public void setCanRotate(boolean canRotate) {
this.canRotate = canRotate;
}
public boolean getCanRotate() {
return this.canRotate;
}
/**
* 设置温度改变监听
*
* @param onTempChangeListener 监听接口
*/
public void setOnTempChangeListener(OnTempChangeListener onTempChangeListener) {
this.onTempChangeListener = onTempChangeListener;
}
/**
* 设置点击监听
*
* @param onClickListener 点击回调接口
*/
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
/**
* 温度改变监听接口
*/
public interface OnTempChangeListener {
/**
* 回调方法
*
* @param temp 温度
*/
void change(int temp);
}
/**
* 点击回调接口
*/
public interface OnClickListener {
/**
* 点击回调方法
*
* @param temp 温度
*/
void onClick(int temp);
}
public int dp2px(float dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getResources().getDisplayMetrics());
}
private int sp2px(float sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
getResources().getDisplayMetrics());
}
}
4.TestFragment.java
public class TestFragment extends Fragment {
private Button startButton;
private EditText IPText;
private Context mContext;
private boolean isConnecting=false;
private Thread mThreadClient=null;
private Socket mSocketClient=null;
private PrintWriter mPrintWriterClient=null;
private String res="";
private TextView recvText,recvText1,recvText2;
private TempControlView tempControl;
private Switch switch_c;
private boolean flag=false;
@SuppressLint("SetTextI18n")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_test, null);
mContext=getContext();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build()
);
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.penaltyLog()
.penaltyDeath()
.build());
IPText= view.findViewById(R.id.IPText);
IPText.setText("192.168.1.127:8080");
startButton= view.findViewById(R.id.StartConnect);
startButton.setOnClickListener(StartClickListener);
recvText= view.findViewById(R.id.tv1);
recvText1= view.findViewById(R.id.textView3);
recvText2= view.findViewById(R.id.textView4);
tempControl = view.findViewById(R.id.temp_control);
// 设置三格代表温度1度
tempControl.setAngleRate(1);
tempControl.setTemp(16, 30, 16);
//设置旋钮是否可旋转
tempControl.setCanRotate(true);
tempControl.setOnTempChangeListener(new TempControlView.OnTempChangeListener() {
@Override
public void change(int temp) {
switch (temp){
case 16:
send("*16",1);
break;
case 17:
send("*17",1);
break;
case 18:
send("*18",1);
break;
case 19:
send("*19",1);
break;
case 20:
send("*20",1);
case 21:
send("*21",1);
break;
case 22:
send("*22",1);
break;
case 23:
send("*23",1);
break;
case 24:
send("*24",1);
break;
case 25:
send("*25",1);
break;
case 26:
send("*26",1);
break;
case 27:
send("*27",1);
break;
case 28:
send("*28",1);
break;
case 29:
send("*29",1);
break;
case 30:
send("*30",1);
break;
}
}
});
tempControl.setOnClickListener(new TempControlView.OnClickListener() {
@Override
public void onClick(int temp) {
switch (temp){
case 16:
send("*16",1);
break;
case 17:
send("*17",1);
break;
case 18:
send("*18",1);
break;
case 19:
send("*19",1);
break;
case 20:
send("*20",1);
case 21:
send("*21",1);
break;
case 22:
send("*22",1);
break;
case 23:
send("*23",1);
break;
case 24:
send("*24",1);
break;
case 25:
send("*25",1);
break;
case 26:
send("*26",1);
break;
case 27:
send("*27",1);
break;
case 28:
send("*28",1);
break;
case 29:
send("*29",1);
break;
case 30:
send("*30",1);
break;
}
}
});
switch_c=view.findViewById(R.id.switch_c);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
switch_c.setShowText(true);
}
switch_c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
switch_c.setSwitchTextAppearance(mContext,R.style.s_true);
switch_c.setShowText(true);
if (send("*ON",-1)){
flag=true;
}else{
switch_c.setChecked(false);
}
}else{
switch_c.setSwitchTextAppearance(mContext,R.style.s_false);
switch_c.setShowText(true);
if (send("*OFF",-2)){
flag=false;
tempControl.setTemp(16, 30, 16);
}else{
switch_c.setChecked(false);
}
}
}
});
return view;
}
//连接到智能衣柜
private View.OnClickListener StartClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(isConnecting)
{
isConnecting=false;
if(mSocketClient!=null)
{
try{
mSocketClient.close();
mSocketClient = null;
if (mPrintWriterClient!=null){
mPrintWriterClient.close();
mPrintWriterClient = null;
}
mThreadClient.interrupt();
startButton.setText("开始连接");
IPText.setEnabled(true);//可以输入ip和端口号
recvText.setText("断开连接\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}else
{
mThreadClient = new Thread(mRunnable);
mThreadClient.start();
}
}
};
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
String msgText = IPText.getText().toString();
if(msgText.length()<=0)
{
Message msg = new Message();
msg.what = 5;
mHandler.sendMessage(msg);
return;
}
int start = msgText.indexOf(":");
if((start==-1)||(start+1>=msgText.length()))
{
Message msg = new Message();
msg.what = 6;
mHandler.sendMessage(msg);
return;
}
String sIP= msgText.substring(0,start);
String sPort = msgText.substring(start+1);
int port = Integer.parseInt(sPort);
BufferedReader mBufferedReaderClient;
try
{
//连接服务器
mSocketClient = new Socket();
SocketAddress socAddress = new InetSocketAddress(sIP, port);
mSocketClient.connect(socAddress, 2000);//设置超时时间为2秒
//取得输入、输出流
mBufferedReaderClient =new BufferedReader(new InputStreamReader(mSocketClient.getInputStream()));
mPrintWriterClient=new PrintWriter(mSocketClient.getOutputStream(),true);
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}catch (Exception e) {
Message msg = new Message();
msg.what = 2;
mHandler.sendMessage(msg);
return;
}
char[] buffer = new char[256];
int count ;
while(true)
{
try
{
if((count = mBufferedReaderClient.read(buffer))>0)
{
res = getInfoBuff(buffer,count)+"\n";
Message msg = new Message();
msg.what = 4;
mHandler.sendMessage(msg);
}
}catch (Exception e) {
// TODO: handle exception
Message msg = new Message();
msg.what = 3;
mHandler.sendMessage(msg);
}
}
}
};
@SuppressLint("HandlerLeak")
Handler mHandler = new Handler()
{
@SuppressLint("SetTextI18n")
public void handleMessage(Message msg)
{
super.handleMessage(msg);
if(msg.what==4)
{
char []arrs;
arrs=res.toCharArray();//接收来自服务器的字符串
if (arrs.length>=9) {
recvText1.setText("温度: " + arrs[2] + arrs[3] + "℃" + ' ');
recvText2.setText("湿度: " + arrs[7] + arrs[8] + "%" + ' ');
}else {
showDialog("收到格式错误的数据:"+res);
}
}else if (msg.what==2){
showDialog("连接失败,服务器走丢了");
startButton.setText("开始连接");
}else if (msg.what==1){
showDialog("连接成功!");
recvText.setText("已连接空调\n");
IPText.setEnabled(false);//锁定ip地址和端口号
isConnecting = true;
startButton.setText("停止连接");
}else if (msg.what==3){
recvText.setText("断开连接\n");
}else if (msg.what==5){
recvText.setText("IP和端口号不能为空\n");
}
else if (msg.what==6){
recvText.setText("IP地址不合法\n");
}
}
};
private void showDialog(String msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle(msg);
builder.setCancelable(false);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
private String getInfoBuff(char[] buff,int count)
{
char[] temp = new char[count];
System.arraycopy(buff, 0, temp, 0, count);
return new String(temp);
}
private boolean send(String msg,int position){
if(isConnecting&&mSocketClient!=null){
if ((position==-1)||(position==-2)){
try
{
mPrintWriterClient.print(msg);
mPrintWriterClient.flush();
return true;
}catch (Exception e) {
// TODO: handle exception
Toast.makeText(mContext, "发送异常"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}else{
if (flag){
try
{
mPrintWriterClient.print(msg);
mPrintWriterClient.flush();
return true;
}catch (Exception e) {
// TODO: handle exception
Toast.makeText(mContext, "发送异常"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}else {
showDialog("您还没有开启空调哦,请先开启空调吧!");
tempControl.setTemp(16, 30, 16);
}
}
}else{
showDialog("您还没有连接空调呢!");
tempControl.setTemp(16, 30, 16);
}
return false;
}
}
5.fragment_test.xml
<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:background="#01092C"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#CFCACA"
android:id="@+id/IPText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ems="10"
android:textSize="20dp"
android:hint="请输入衣柜服务端的IP地址"
tools:ignore="HardcodedText" />
<Button
android:layout_marginRight="10dp"
android:layout_weight="2"
android:id="@+id/StartConnect"
android:background="#C3C7DA"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:text="连接空调" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv1"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="欢迎使用智能空调!(设备未连接!)"
android:textSize="20dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.myclient.TempControlView
android:id="@+id/temp_control"
android:layout_width="250dp"
android:layout_height="250dp"
/>
<Switch
android:layout_gravity="center"
android:id="@+id/switch_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:thumb="@drawable/thumb"
android:track="@drawable/track"
android:background="#ffffff"
tools:ignore="HardcodedText" />
</LinearLayout>
<LinearLayout
android:layout_marginLeft="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_weight="1"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#F3DBDB"
android:text="温度:0℃"
android:id="@+id/textView3" />
<TextView
android:layout_weight="1"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="湿度:0%"
android:textColor="#F3DBDB"
android:id="@+id/textView4" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
6.这玩意儿有前景吗?
我觉得是有的,因为可以将APP和单片机集成一个系统,里面集成所有空调的遥控器协议,而且还能通过单片机实现一个智能空调。最重要的一点就是在外面都能随意控制哦!这不增加了空调企业的卖点吗?
更多推荐
所有评论(0)