标签:import alt false ted this help extend end port
import com.example.analoglib.Analog4150ServiceAPI;
public class BasePort {
public int openADAMPort(int com,int mode,int baudRate){
return Analog4150ServiceAPI.openPort(com, mode, baudRate);
}
public void closeADAMPort(){
Analog4150ServiceAPI.closeUart();
}
}
import android.util.Log;
import com.example.analoglib.Analog4150ServiceAPI;
import com.example.analoglib.OnFireResponse;
import com.example.analoglib.OnPersonResponse;
import com.example.analoglib.OnSmorkResponse;
import com.example.analoglib.ReceiveThread;
public class ADAM4150 extends BasePort{
//风扇开关命令,这些命名是根据协议的
private final char[] open1Fan={0x01,0x05,0x00,0x10,0xFF,0x00,0x8D,0xFF};
private final char[] close1Fan={0x01,0x05,0x00,0x10,0x00,0x00,0xCC,0x0F};
private final char[] open2Fan={0x01,0x05,0x00,0x11,0xFF,0x00,0xDC,0x3F};
private final char[] close2Fan={0x01,0x05,0x00,0x11,0x00,0x00,0x9D,0xCF};
//创建ADAM4150类的构造方法,并实现人体、烟雾、火焰的实时取值,并用get方法返回
public static int mADAM4150_fd=0;
private boolean rePerson;
private boolean reFire;
private boolean reSmork;
public ADAM4150(int com,int mode,int baudRate){
//打开串口
mADAM4150_fd=openADAMPort(com, mode, baudRate);
ReceiveThread mReceiveThread=new ReceiveThread();
mReceiveThread.start();
//设置人体回调函数,人体传感器接入DI0
Analog4150ServiceAPI.getPerson("person", new OnPersonResponse() {
@Override
public void onValue(boolean arg0) {
rePerson=!arg0;
}
@Override
public void onValue(String arg0) {
Log.e("cjI",arg0);
}
});
Analog4150ServiceAPI.getFire("fire", new OnFireResponse() {
@Override
public void onValue(boolean arg0) {
reFire=!arg0;
}
@Override
public void onValue(String arg0) {
Log.e("cjI",arg0);
}
});
Analog4150ServiceAPI.getSmork("Smork", new OnSmorkResponse() {
@Override
public void onValue(boolean arg0) {
reSmork=!arg0;
}
@Override
public void onValue(String arg0) {
Log.e("cjI",arg0);
}
});
}
//获取传感器值,true或者false
public boolean getPerson(){
return rePerson;
}
public boolean getFire(){
return reFire;
}
public boolean getSmork(){
return reSmork;
}
//打开风扇
public void openFan1(){
Analog4150ServiceAPI.sendRelayControl(open1Fan);
}
public void openFan2(){
Analog4150ServiceAPI.sendRelayControl(open2Fan);
}
public void closeFan1(){
Analog4150ServiceAPI.sendRelayControl(close1Fan);
}
public void closeFan2(){
Analog4150ServiceAPI.sendRelayControl(close2Fan);
}
}
import com.example.analoglib.Analog4150ServiceAPI;
import com.example.analoglib.AnalogHelper;
import com.example.analoglib.OnFireResponse;
import com.example.analoglib.OnPersonResponse;
import com.example.analoglib.OnReceiveCompleteListener;
import com.example.analoglib.OnSmorkResponse;
import com.example.analoglib.ReceiveThread;
import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ImageView;
public class MainActivity extends Activity {
private TextView mTvFire;
private TextView mTvPerson;
private TextView mTvSmoke;
private ImageView mImgFire;
private ADAM4150 mAdam4150;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();//初始化控件
}
private void initView() {
mTvFire = (TextView) findViewById(R.id.textView1);
mTvPerson = (TextView) findViewById(R.id.textView3);
mTvSmoke = (TextView) findViewById(R.id.textView2);
mImgFire=(ImageView) findViewById(R.id.imageView2);
//com6口,USB模式,9600波特率
mAdam4150=new ADAM4150(6,1,3);
mHandler.postDelayed(mRunnable,ms);
}
private int ms=1000;
//声明一个Handler对象
private Handler mHandler=new Handler();
//声明一个Runnable对象
private Runnable mRunnable=new Runnable() {
@Override
public void run() {
mHandler.postDelayed(mRunnable,ms);
mTvPerson.setText(mAdam4150.getPerson()?"无人":"有人");
mTvFire.setText(mAdam4150.getFire()?"无火":"有火");
mTvSmoke.setText(mAdam4150.getSmork()?"正常":"危险");
//如果有火,显示火焰图片。否则就不显示火焰图片
if(mAdam4150.getFire()){
mImgFire.setVisibility(View.GONE);
}else{
mImgFire.setVisibility(View.VISIBLE);
// long ms=500;
// Vibrate(MainActivity.this,ms);
}
Analog4150ServiceAPI.send4150();
}
};
//自定义Vibrate方法,调用系统自身的服务,实现震动功能
public static void Vibrate(final Activity activity,long milliseconds){
Vibrator vib=(Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(milliseconds);
}
}
标签:import alt false ted this help extend end port
原文地址:http://www.cnblogs.com/aqyl/p/6920675.html