关闭

zigbee网关 android客户端编写温度计功能

标签: zigbee网关LM75Aandroidtcp
884人阅读 评论(0) 收藏 举报
分类:

参考如何在Android手机上画温度计?

但是不符合想要的需求。需求是温度是外界控制的,通过tcp传进来的;并且温度计的量程也太小了,最好在室温以内;最好能添加显示当前温度;温度计你最好在画面中间显示;经过修改后的画面如下:



布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:id="@+id/ipport"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           
            android:hint="@string/ipport_hint"
            android:text="@string/ipport_text" >
        </EditText>

        <Button
            android:id="@+id/StartConnect"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="true"
            android:text="@string/StartConnect_name" />

    </LinearLayout>

    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>



定义几个成员变量指示温度计的属性

	private static final int TOP = 40; // 温度计距离顶部的位置
	private static final int PIX1DEGREE = 30; // 温度计每一摄氏度占用的像素数
	private static final int BEGIN_DEGREE = 15; // 温度计量程起始温度
	private static final int END_DEGREE = 35; // 温度计量程终止温度
	private static final int WIDTH = 40; // 温度计的宽度



对draw函数进行了修改,主要把原先的定值改为可以变化的

/*
 * @author fanruitian 
 * @QQ 1021200380
 * @date 2015.11.20
 * */	
private void draw(String temp) {
		int HEIGHT = (END_DEGREE - BEGIN_DEGREE) * PIX1DEGREE; // 矩形的高度
		float tem1 = Float.parseFloat(temp);
		Canvas canvas = mHolder.lockCanvas();
		Paint mPaint = new Paint();
		mPaint.setColor(Color.BLACK);
		canvas.drawRect(0,0,canvas.getWidth(),canvas.getHeight(),mPaint);
		
		//Paint mPaint = new Paint();
		mPaint.setColor(Color.WHITE);
		// 画一个居中的距离顶部TOPpx,长宽分别为WIDTH和HEIGHT的矩形
		canvas.drawRect(canvas.getWidth() / 2 - WIDTH / 2, TOP,
				canvas.getWidth() / 2 + WIDTH / 2, TOP + HEIGHT, mPaint);
		Paint paintCircle = new Paint();
		paintCircle.setColor(Color.RED);
		Paint paintLine = new Paint();
		paintLine.setColor(Color.BLUE);
		// 画红条代表当前的温度
		int y = TOP + HEIGHT - (int) ((tem1 - BEGIN_DEGREE) * PIX1DEGREE);
		System.out.println(y);
		canvas.drawRect(canvas.getWidth() / 2 - WIDTH / 2, y, canvas.getWidth()
				/ 2 + WIDTH / 2, TOP + HEIGHT, paintCircle);
		// 在矩形的底部画一个半径为WIDTH的圆,稍稍往上WIDTH/4个像素,这样就不是相交的了
		canvas.drawCircle(canvas.getWidth() / 2, TOP + HEIGHT + WIDTH - WIDTH
				/ 4, WIDTH, paintCircle);
		int ydegree = TOP + HEIGHT;
		int tem = BEGIN_DEGREE;
		int i = 0;
		while (ydegree > TOP) {
			canvas.drawLine(canvas.getWidth() / 2 + WIDTH / 2, ydegree,
					canvas.getWidth() / 2 + WIDTH / 2 + 7, ydegree, mPaint);
			if (i % 10 == 0) {
				canvas.drawLine(canvas.getWidth() / 2 + WIDTH / 2, ydegree,
						canvas.getWidth() / 2 + WIDTH / 2 + 7, ydegree,
						paintLine);
				// canvas.drawLine(60, ydegree, 72, ydegree, paintLine);
				canvas.drawText(tem + "", canvas.getWidth() / 2 + WIDTH / 2
						+ 10, ydegree + 5, mPaint);
				tem++;
			}
			i++;
			ydegree = ydegree - PIX1DEGREE / 10;
		}
		paintCircle.setTextSize(70);
		paintCircle.setTextAlign(Paint.Align.CENTER);
		canvas.drawText(tem1 + "℃", canvas.getWidth() / 2 - 10,
				canvas.getHeight() - 55, paintCircle);
		mHolder.unlockCanvasAndPost(canvas);// 更新屏幕显示内容
	}


再就是添加一些tcp功能,用于打通网络处理字符串,启动一个线程用于接收数据处理数据

	// 线程:监听服务器发来的消息
	private Runnable mRunnable = new Runnable() {
		public void run() {
			//bConnect.setText("停止连接");
			String msgText = eIpPort.getText().toString();
			int start = msgText.indexOf(":");
			String sIP = msgText.substring(0, start);
			String sPort = msgText.substring(start + 1);
			BufferedReader mBufferedReaderClient = null;
			int port = Integer.parseInt(sPort);
			try {
				// 连接服务器
				mSocketClient = new Socket();
				mSocketClient.connect(new InetSocketAddress(sIP, port), 3000);
				// 取得输入、输出流
				mBufferedReaderClient = new BufferedReader(
						new InputStreamReader(mSocketClient.getInputStream()));
				// break;
			} catch (Exception e) {
				//bConnect.setText("开始连接");
				e.printStackTrace();

			}
			char[] buffer = new char[512];
			int count = 0;

			try {
				while (bRun) {
					if ((count = mBufferedReaderClient.read(buffer)) > 0) {
						String recvMessageClient = getInfoBuff(buffer, count);
						System.out.println(recvMessageClient);
						ProcessData(recvMessageClient);
					}
				}
			} catch (Exception e) {
				//bConnect.setText("开始连接");
				e.printStackTrace();
			}
		}

	};

为了一打开软件就能看到画面需要在合适的时候调用draw函数

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		this.draw("15");
	}


简单的测试一下,向手机发送字符串

{"From":"Endian","Addr":"8021","Type":"Message","Content":"Temperature:27.4"}

看一下手机接收的图片,如上图所示

成功

更多资料请参考 http://web.nenewind.com

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:83398次
    • 积分:1434
    • 等级:
    • 排名:千里之外
    • 原创:62篇
    • 转载:1篇
    • 译文:0篇
    • 评论:10条
    文章分类
    最新评论