该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package com.example.fandemonew;
import com.example.demo.library.SerialPort;
import com.example.demo.library.ShareData;
import com.example.demo.library.ZigBeeService;
import com.newland.jni.Linuxc;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
private char[] fanOpenCommand= {0xFF,0xF5,0x05,0x02,0x34,0x12,0x00,0x01,0x00};
private char[] fanCloseCommand= {0xFF,0xF5,0x05,0x02,0x34,0x12,0x00,0x02,0x00};
private char[] fan2OpenCommand= {0xFF,0xF5,0x05,0x02,0x01,0x00,0x00,0x01,0x03};
private char[] fan2CloseCommand= {0xFF,0xF5,0x05,0x02,0x01,0x00,0x00,0x02,0x02};
private String mControlName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OpenZigBee();
initView();
}
private void OpenZigBee() {
if (ShareData.com_fdZigBee > 0)
SerialPort.closePort(ShareData.com_fdZigBee);
// 读取串口设置值,无线传感网默认值为0
ShareData.com_fdZigBee = SerialPort.openPort(3, 0, 5);
if (ShareData.com_fdZigBee == -1) {
Toast.makeText(this, "串口打开失败", Toast.LENGTH_LONG).show();
}
Intent intent = new Intent(this, ZigBeeService.class);
this.startService(intent);
}
private void initView(){
String[] strs = {"请选择","风扇1","风扇2"};
Spinner sp = (Spinner) findViewById(R.id.sp);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, strs);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View arg1,
int position, long arg3) {
String itemName = parent.getItemAtPosition(position).toString();
mControlName = itemName;
}
@Override
public void onNothingSelected(AdapterView> arg0) {
// TODO Auto-generated method stub
}
});
findViewById(R.id.open_btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if("风扇1".equals(mControlName)){
sendCMD(fanOpenCommand);
}else if("风扇2".equals(mControlName)){
sendCMD(fan2OpenCommand);
}
}
});
findViewById(R.id.close_btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if("风扇1".equals(mControlName)){
sendCMD(fanCloseCommand);
}else if("风扇2".equals(mControlName)){
sendCMD(fan2CloseCommand);
}
}
});
}
private static void sendCMD(char[] cmd) {
String strcmd = String.valueOf(cmd);
Linuxc.sendMsgUartHex(ShareData.com_fdZigBee, strcmd, strcmd.length());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}