您的位置:首页 > 移动开发 > Android开发

基于DHT11温湿度传感器的Android硬件访问服务的简单实现(四)

2017-04-21 12:07 330 查看
本文的主要内容是编写一个Android APP来测试前面几节实现的DHT11温湿度传感器的Android硬件访问服务。

一、基本的开发环境

操作系统 :Windows 7 64bit

开发平台  : Android Studio 2.2.3

二、Android的消息处理机制

在Android中不同的线程之间进行通信是通过Handler来实现的。在Android应用程序的开发过程当中可以将线程分为两类:Main threader 和Work threader,就是主线程(也叫作UI线程)和工作线程(其他线程)。Android系统为了保证界面运行的流畅性,把所有和UI相关的操作都放在UI线程当中,其他线程是不允许进行UI相关的操作的。那么有时候要把子线程的消息显示在屏幕上就不能直接在子线程中调用UI控件进行了,而需要先把消息传递给UI线程,由UI线程对UI控件进行操作,这里就涉及到消息的传递。关于具体的Android消息处理机制请参考相关的文档或者书籍。

三、编写测试的Android APP

本测试例程的主要开发思路是:首先定义两个线程,UI线程和温湿度数据获取的线程,在UI线程中进行温湿度数据的显示,在温湿度数据采集线程中获取温湿度,并每隔一秒采集一次数据;然后定义一个Handler,通过handler来完成消息的传递。

整个APP简单的布局界面如下:



整个布局很简单,就是定义两个TextView控件来显示温度和湿度数据即可。它的布局文件content_main.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="cn.edu.syau.dht11_demo03.MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DHT11 测试3:"
android:textSize="20dp"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="温度 : "
android:textSize="20dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="  ℃"
android:id="@+id/temp"
android:textSize="20dp"
android:textColor="@color/colorPrimaryDark"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="湿度:"
android:textSize="20dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" %"
android:id="@+id/humi"
android:textSize="20dp"
android:textColor="@color/colorPrimaryDark"/>

</LinearLayout>

</LinearLayout>
获取前文在服务层当中添加到系统中的DHT11服务的代理类对象的实例,这样才能通过这个实例来访问硬件资源,从而采集到温湿度数据。但是标准的SDK中并没有定义对DHT11服务代理类获取的接口(当然不会有,这是我们自己实现的),此时可以通过Java的发射机制来实现(不推荐)或者把Android系统中编译服务生成的jar包导入到当前的Android工程当中,这里选择第二种方式。jar包在Android系统中的位置是:out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar。加入jar包之后就是获取服务的代理对象,如下所示:
/* 获取IDht11Service的服务代理类对象 */
service = IDht11Service.Stub.asInterface(ServiceManager.getService("dht11"));
然后定义一个handler用来对数据进行传递,这个handler的定义和使用如下:

class Dht11Handler extends Handler{

public Dht11Handler(Looper looper) {
super(looper);
}

@Override
public void handleMessage(Message msg) {
temp.setText(msg.arg1 + "℃");   //将接收到的消息进行获取,并显示到屏幕上
humi.setText(msg.arg2 + "%");
}
}

/* 定义一个消息处理的handler并用主线程的Looper来初始化 */
handler = new Dht11Handler(MainActivity.this.getMainLooper());
最后实现一个子线程,用它来采集温湿度数据,并把数据通过handler传递给UI线程,把温湿度数据显示出来,具体如下所示:
class Dht11Thread extends Thread{
@Override
public void run() {
byte[] buffer;
for(;;){
try {
buffer = service.dht11GetDatas();   // 获取温湿度传感器的数据

Message msg = handler.obtainMessage();
msg.arg1 = buffer[2];
msg.arg2 = buffer[0];
handler.sendMessage(msg);   // 获得一个消息,把采集到的数据绑定到消息当中,并发送消息

Thread.sleep(2000);

} catch (RemoteException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
对程序进行编译,安装到开发板上,结果如下图所示:



整个完整的的代码如下所示:

package cn.edu.syau.dht11_demo03;

import android.os.Bundle;
import android.os.Handler;
import andr
c33f
oid.os.IDht11Service;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView temp = null;
private TextView humi = null;

private IDht11Service service = null;

private Thread thread = null;
private Handler handler = null;

class Dht11Thread extends Thread{ @Override public void run() { byte[] buffer; for(;;){ try { buffer = service.dht11GetDatas(); // 获取温湿度传感器的数据 Message msg = handler.obtainMessage(); msg.arg1 = buffer[2]; msg.arg2 = buffer[0]; handler.sendMessage(msg); // 获得一个消息,把采集到的数据绑定到消息当中,并发送消息 Thread.sleep(2000); } catch (RemoteException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } }

class Dht11Handler extends Handler{

public Dht11Handler(Looper looper) {
super(looper);
}

@Override
public void handleMessage(Message msg) {
temp.setText(msg.arg1 + "℃"); //将接收到的消息进行获取,并显示到屏幕上
humi.setText(msg.arg2 + "%");
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.this.finish();
}
});

/* 获取IDht11Service的服务代理类对象 */
service = IDht11Service.Stub.asInterface(ServiceManager.getService("dht11"));

/* 根据ID号找到资源 */
temp = (TextView) findViewById(R.id.temp);
humi = (TextView) findViewById(R.id.humi);

/* 定义一个消息处理的handler并用主线程的Looper来初始化 */
handler = new Dht11Handler(MainActivity.this.getMainLooper());

/* 定义一个线程对象,让其不停地对DHT11的温湿度数据进行采集 */
thread = new Dht11Thread();
thread.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


至此,整个基于DHT11温湿度传感器的Android硬件访问服务就介绍完了,实现的很简单,说的也比较烂,只希望对看的你有一点帮助也好。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐