zigbeelibrary.jar包是用来接收zigbee协调器发来的数据,或者通过协调器控制其网内的继电器
首先将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:text="人体:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView1"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_toRightOf="@id/tvPerson"
android:text="可燃气体:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvFireGas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView2"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_toRightOf="@id/tvFireGas"
android:text="空气质量:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvCo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView3"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginTop="20dp"
android:text="火焰:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvFire"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/textView4"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginLeft="150dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/tvFire"
android:text="光照:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/textView5"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginLeft="150dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/tvLight"
android:text="温度:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/textView6"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginLeft="150dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/tvTemp"
android:text="湿度:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvHum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/textView7"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView5"
android:layout_marginTop="20dp"
android:text="继电器1"
android:textColor="@android:color/black"
android:textSize="25sp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView8"
android:layout_marginTop="20dp"
android:onClick="relay1Open"
android:text="开"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView8"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/button1"
android:onClick="relay1Close"
android:text="关"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView5"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/button2"
android:text="继电器2"
android:textColor="@android:color/black"
android:textSize="25sp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView8"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/button2"
android:onClick="relay2Open"
android:text="开"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView8"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/button3"
android:onClick="relay2Close"
android:text="关"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView5"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/button4"
android:text="继电器3"
android:textColor="@android:color/black"
android:textSize="25sp" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView8"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/button4"
android:onClick="relay3Open"
android:text="开"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView8"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/button5"
android:onClick="relay3Close"
android:text="关"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_marginTop="20dp"
android:text="value1:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvValue1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/textView11"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/tvValue1"
android:text="value2:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvValue2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/textView12"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/tvValue2"
android:text="value3:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvValue3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/textView14"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/tvValue3"
android:text="value4:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tvValue4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/textView15"
android:text="-"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView11"
android:layout_marginTop="20dp"
android:text="双联继电器1"
android:textColor="@android:color/black"
android:textSize="25sp" />
<Button
android:id="@+id/button7"
android:onClick="dRealy1Open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView16"
android:text="开"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button8"
android:onClick="dRealy1Close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView16"
android:layout_toRightOf="@id/button7"
android:text="关"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView11"
android:layout_toRightOf="@id/textView16"
android:layout_marginTop="20dp"
android:text="双联继电器2"
android:layout_marginLeft="200dp"
android:textColor="@android:color/black"
android:textSize="25sp" />
<Button
android:id="@+id/button9"
android:onClick="dRealy2Open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button8"
android:layout_below="@id/textView17"
android:text="开"
android:layout_alignLeft="@id/textView17"
android:textColor="@android:color/black"
android:textSize="30sp" />
<Button
android:id="@+id/button10"
android:onClick="dRealy2Close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView17"
android:layout_toRightOf="@id/button9"
android:text="关"
android:textColor="@android:color/black"
android:textSize="30sp" />
</RelativeLayout>
主函数代码
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.newland.zigbeelibrary.ZigBeeService;
import com.newland.zigbeelibrary.ZigBeeServiceAPI;
import com.newland.zigbeelibrary.ZigbeeHelper;
import com.newland.zigbeelibrary.response.OnCoResponse;
import com.newland.zigbeelibrary.response.OnFireResponse;
import com.newland.zigbeelibrary.response.OnFiregasResponse;
import com.newland.zigbeelibrary.response.OnHumResponse;
import com.newland.zigbeelibrary.response.OnLightResponse;
import com.newland.zigbeelibrary.response.OnPersonResponse;
import com.newland.zigbeelibrary.response.OnTemperatureResponse;
import com.newland.zigbeelibrary.response.OnValue1Response;
import com.newland.zigbeelibrary.response.OnValue2Response;
import com.newland.zigbeelibrary.response.OnValue3Response;
import com.newland.zigbeelibrary.response.OnValue4Response;
public class MainActivity extends AppCompatActivity {
//继电器开关命令
private final static char Relay1Open[] = new char[]{0xFF, 0xF5, 0x05,
0x02, 0x01, 0x00, 0x00, 0x01, 0x03};
private final static char Relay1Close[] = new char[]{0xFF, 0xF5, 0x05,
0x02, 0x01, 0x00, 0x00, 0x02, 0x02};
private final static char Relay2Open[] = new char[]{0xFF, 0xF5, 0x05,
0x02, 0x02, 0x00, 0x00, 0x01, 0x02};
private final static char Relay2Close[] = new char[]{0xFF, 0xF5, 0x05,
0x02, 0x02, 0x00, 0x00, 0x02, 0x01};
private final static char Relay3Open[] = new char[]{0xFF, 0xF5, 0x05,
0x02, 0x03, 0x00, 0x00, 0x01, 0x01};
private final static char Relay3Close[] = new char[]{0xFF, 0xF5, 0x05,
0x02, 0x03, 0x00, 0x00, 0x02, 0x00};
private final static char Relay4Open[] = new char[]{0xFF, 0xF5, 0x05,
0x02, 0x04, 0x00, 0x00, 0x11, 0x01};
private final static char Relay4Close[] = new char[]{0xFF, 0xF5, 0x05,
0x02, 0x04, 0x00, 0x00, 0x22, 0x00};
private final static char Relay5Open[] = new char[]{0xFF, 0xF5, 0x05,
0x02, 0x05, 0x00, 0x00, 0x11, 0x01};
private final static char Relay5Close[] = new char[]{0xFF, 0xF5, 0x05,
0x02, 0x05, 0x00, 0x00, 0x22, 0x00};
private TextView tvPerson;
private TextView tvFireGas;
private TextView tvCo;
private TextView tvFire;
private TextView tvLight;
private TextView tvTemp;
private TextView tvHum;
private TextView tvValue1;
private TextView tvValue2;
private TextView tvValue3;
private TextView tvValue4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initZigbee();
initGetDataCallBack();
}
//找到ui控件
private void initView() {
tvPerson = findViewById(R.id.tvPerson);
tvFireGas = findViewById(R.id.tvFireGas);
tvCo = findViewById(R.id.tvCo);
tvFire = findViewById(R.id.tvFire);
tvLight = findViewById(R.id.tvLight);
tvTemp = findViewById(R.id.tvTemp);
tvHum = findViewById(R.id.tvHum);
tvValue1 = findViewById(R.id.tvValue1);
tvValue2 = findViewById(R.id.tvValue2);
tvValue3 = findViewById(R.id.tvValue3);
tvValue4 = findViewById(R.id.tvValue4);
}
//发送继电器1开指令
public void relay1Open(View v) {
//参数1,ZigbeeHelper.com为调用打开串口函数的返回值
//参数2,Relay1Open为静态指令
ZigBeeServiceAPI.sendCMD(ZigbeeHelper.com, Relay1Open);
}
//发送继电器1关指令
public void relay1Close(View v) {
ZigBeeServiceAPI.sendCMD(ZigbeeHelper.com, Relay1Close);
}
public void relay2Open(View v) {
ZigBeeServiceAPI.sendCMD(ZigbeeHelper.com, Relay2Open);
}
public void relay2Close(View v) {
ZigBeeServiceAPI.sendCMD(ZigbeeHelper.com, Relay2Close);
}
public void relay3OPen(View v) {
ZigBeeServiceAPI.sendCMD(ZigbeeHelper.com, Relay3Open);
}
public void relay3Close(View v) {
ZigBeeServiceAPI.sendCMD(ZigbeeHelper.com, Relay3Close);
}
public void dRealy1Open(View v){
ZigBeeServiceAPI.sendCMD(ZigbeeHelper.com,Relay4Open);
}
public void dRealy1Close(View v){
ZigBeeServiceAPI.sendCMD(ZigbeeHelper.com,Relay4Close);
}
public void dRealy2Open(View v){
ZigBeeServiceAPI.sendCMD(ZigbeeHelper.com,Relay5Open);
}
public void dRealy2Close(View v){
ZigBeeServiceAPI.sendCMD(ZigbeeHelper.com,Relay5Close);
}
//接收到zigbee发来的数据后更新ui
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1://人体传感器
tvPerson.setText(msg.obj.toString());
break;
case 2://可燃气体传感器
tvFireGas.setText(msg.obj.toString());
break;
case 3://空气质量传感器
tvCo.setText(msg.obj.toString());
break;
case 4://火焰传感器
tvFire.setText(msg.obj.toString());
break;
case 5://光照传感器
tvLight.setText(msg.obj.toString());
break;
case 6://温度传感器
tvTemp.setText(msg.obj.toString());
break;
case 7://湿度传感器
tvHum.setText(msg.obj.toString());
break;
case 8://四通道1
tvValue1.setText(msg.obj.toString());
break;
case 9://四通道2
tvValue2.setText(msg.obj.toString());
break;
case 10://四通道3
tvValue3.setText(msg.obj.toString());
break;
case 11://四通道4
tvValue4.setText(msg.obj.toString());
break;
}
}
};
//初始化zigbee
private void initZigbee() {
/**
* openPort打开接口
* com,第n个接口
* mode,模式,0表示使用串口,1表示使用usb
* baudRate,波特率
*/
ZigBeeServiceAPI.openPort(1, 0, 5);
//串口打开成功后,将会有一个大于0的数赋值给ZigbeeHelper类的属性com,否则为-1
if (ZigbeeHelper.com < 0) {
Toast.makeText(this, "串口打开失败", Toast.LENGTH_SHORT).show();
return;
}
//打开数据接收线程
ZigBeeService service = new ZigBeeService();
service.start();
}
//初始化回调函数
private void initGetDataCallBack() {
ZigBeeServiceAPI.getPerson("person", new OnPersonResponse() {
@Override
public void onValue(int i) {
}
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 1;
msg.obj = s;
//发送消息给ui线程
handler.sendMessage(msg);
}
});
ZigBeeServiceAPI.getFiregas("firegas", new OnFiregasResponse() {
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 2;
msg.obj = s;
handler.sendMessage(msg);
}
@Override
public void onValue(double v) {
}
});
ZigBeeServiceAPI.getCo("co", new OnCoResponse() {
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 3;
msg.obj = s;
handler.sendMessage(msg);
}
@Override
public void onValue(double v) {
}
});
ZigBeeServiceAPI.getFire("fire", new OnFireResponse() {
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 4;
msg.obj = s;
handler.sendMessage(msg);
}
@Override
public void onValue(double v) {
}
});
ZigBeeServiceAPI.getLight("light", new OnLightResponse() {
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 5;
msg.obj = s;
handler.sendMessage(msg);
}
@Override
public void onValue(double v) {
}
});
ZigBeeServiceAPI.getTemperature("temp", new OnTemperatureResponse() {
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 6;
msg.obj = s;
handler.sendMessage(msg);
}
@Override
public void onValue(double v) {
}
});
ZigBeeServiceAPI.getHum("hum", new OnHumResponse() {
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 7;
msg.obj = s;
handler.sendMessage(msg);
}
@Override
public void onValue(double v) {
}
});
//zigbee四通道数据回调函数初始化
ZigBeeServiceAPI.getValue1("value1", new OnValue1Response() {
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 8;
msg.obj = s;
handler.sendMessage(msg);
}
@Override
public void onValue(double v) {
}
});
ZigBeeServiceAPI.getValue2("value2", new OnValue2Response() {
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 9;
msg.obj = s;
handler.sendMessage(msg);
}
@Override
public void onValue(double v) {
}
});
ZigBeeServiceAPI.getValue3("value3", new OnValue3Response() {
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 10;
msg.obj = s;
handler.sendMessage(msg);
}
@Override
public void onValue(double v) {
}
});
ZigBeeServiceAPI.getValue4("value4", new OnValue4Response() {
@Override
public void onValue(String s) {
Message msg = Message.obtain();
msg.what = 11;
msg.obj = s;
handler.sendMessage(msg);
}
@Override
public void onValue(double v) {
}
});
}
@Override
protected void onDestroy() {
ZigBeeServiceAPI.closeUart();
super.onDestroy();
}
}