zigbeeAnaloglibrary.jar包是用来获取四通道独立采集模块中的数据的
首先将jar包和so文件添加到工程,可参考
https://blog.csdn.net/qq_40733723/article/details/89032484
基本步骤
1、初始化ui
2、初始化zigbee
(1)打开串口
(2)如果串口打开成功则开启数据接收线程
3、初始化回调函数,由于子线程无法更新ui,需要发送消息给ui线程
4、创建Handler用来接收子线程发来的消息
xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="@android:color/black"
android:text="温度:"/>
<TextView
android:id="@+id/tvTemp"
android:layout_width="wrap_content"
android:textSize="30sp"
android:textColor="@android:color/black"
android:layout_toRightOf="@id/textView1"
android:layout_height="wrap_content"
android:text="-"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_marginTop="20dp"
android:layout_below="@id/textView1"
android:textColor="@android:color/black"
android:text="湿度:"/>
<TextView
android:id="@+id/tvHum"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:textSize="30sp"
android:layout_below="@id/textView1"
android:textColor="@android:color/black"
android:layout_toRightOf="@id/textView2"
android:layout_height="wrap_content"
android:text="-"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_marginTop="20dp"
android:layout_below="@id/textView2"
android:textColor="@android:color/black"
android:text="光照:"/>
<TextView
android:id="@+id/tvLight"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:textSize="30sp"
android:layout_below="@id/textView2"
android:textColor="@android:color/black"
android:layout_toRightOf="@id/textView2"
android:layout_height="wrap_content"
android:text="-"/>
</RelativeLayout>
主函数代码
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.newland.zigbeeanaloglibrary.ZigBeeAnalogServiceAPI;
import com.newland.zigbeeanaloglibrary.ZigBeeService;
import com.newland.zigbeeanaloglibrary.ZigbeeAnalogHelper;
import com.newland.zigbeeanaloglibrary.response.OnHumResponse;
import com.newland.zigbeeanaloglibrary.response.OnLightResponse;
import com.newland.zigbeeanaloglibrary.response.OnTemperatureResponse;
public class MainActivity extends AppCompatActivity {
private TextView tvTemp;
private TextView tvHum;
private TextView tvLight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initZigbee();
initGetDataCallBack();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
tvTemp.setText(msg.obj.toString());
break;
case 2:
tvHum.setText(msg.obj.toString());
break;
case 3:
tvLight.setText(msg.obj.toString());
break;
}
}
};
private void initView() {
tvTemp = findViewById(R.id.tvTemp);
tvHum = findViewById(R.id.tvHum);
tvLight = findViewById(R.id.tvLight);
}
private void initZigbee() {
ZigBeeAnalogServiceAPI.openPort(1, 0, 5);
if (ZigbeeAnalogHelper.com < 0) {
Toast.makeText(this, "串口打开失败", Toast.LENGTH_SHORT).show();
}
ZigBeeService service = new ZigBeeService();
service.start();
}
private void initGetDataCallBack() {
ZigBeeAnalogServiceAPI.getTemperature("temp", new OnTemperatureResponse() {
@Override
public void onValue(double v) {
}
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 1;
msg.obj = s;
handler.sendMessage(msg);
}
});
ZigBeeAnalogServiceAPI.getHum("hum", new OnHumResponse() {
@Override
public void onValue(double v) {
}
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 2;
msg.obj = s;
handler.sendMessage(msg);
}
});
ZigBeeAnalogServiceAPI.getLight("light", new OnLightResponse() {
@Override
public void onValue(double v) {
}
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 3;
msg.obj = s;
handler.sendMessage(msg);
}
});
}
@Override
protected void onDestroy() {
ZigBeeAnalogServiceAPI.closeUart();
super.onDestroy();
}
}