The use of zigbeeAnaloglibrary.jar

The zigbeeAnaloglibrary.jar package is used to obtain the data in the four channel independent collection module

First, add the jar package and so file to the project. Refer to

https://blog.csdn.net/qq_40733723/article/details/89032484
Basic steps
1. Initialize ui
2. Initialize zigbee
(1) open the serial port
(2) if the serial port is opened successfully, start the data receiving thread
3. Initialize the callback function. Because the sub thread is unable to update the ui, it needs to send a message to the ui thread
4. Create a Handler to receive messages sent by a sub thread

xml code

<?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="Temperature:"/>
    <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="Humidity:"/>
    <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="Illumination:"/>
    <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>

Main function code

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, "Serial port open failed", 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();
    }
}

Tags: Android xml encoding

Posted on Sun, 17 Nov 2019 12:23:23 -0500 by duk