package android.serialport.api.sample;
|
|
import java.util.Calendar;
|
import android.content.Context;
|
import android.content.SharedPreferences;
|
import android.graphics.Color;
|
import android.os.Bundle;
|
import android.os.Handler;
|
import android.os.Message;
|
import android.serialport.api.SerialPort;
|
import android.support.v4.app.Fragment;
|
import android.view.LayoutInflater;
|
import android.view.MotionEvent;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.view.inputmethod.InputMethodManager;
|
import android.widget.Button;
|
import android.widget.EditText;
|
import android.widget.RadioButton;
|
import android.widget.SeekBar;
|
import android.widget.Switch;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
import android.widget.SeekBar.OnSeekBarChangeListener;
|
import sendData.Commands;
|
import sendData.SendDatas;
|
|
public class Fragment_BUS extends Fragment {
|
|
private static Context mContext;
|
|
private View mView;
|
|
public static final int INDUCTOR_LEAVE = 1;
|
|
private static TextView Seekbar_value;
|
|
private static EditText T_TextView;
|
private static EditText key_state;
|
private static EditText c_value;
|
|
private static Switch loop1Button;
|
private static Switch loop2Button;
|
private static Switch loop3Button;
|
private static Switch loop4Button;
|
private static Switch powerButton;
|
public static Switch openBackLight;
|
|
private static RadioButton radioButton1;
|
private static RadioButton radioButton2;
|
private static RadioButton radioButton3;
|
private static RadioButton radioButton4;
|
private static RadioButton radioButton5;
|
private static RadioButton radioButton6;
|
|
private static SeekBar SSeekbar;
|
private static Button Btn_state;
|
private static Button C_Btn;
|
|
int id = -1;
|
|
@Override
|
public void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
}
|
|
@Override
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
mView = inflater.inflate(R.layout.console, container, false);
|
|
iniView();
|
|
iniData();
|
|
return mView;
|
}
|
|
@Override
|
public void onStart() {
|
super.onStart();
|
}
|
|
@Override
|
public void onStop() {
|
super.onStop();
|
}
|
|
@Override
|
public void onDestroy() {
|
super.onDestroy();
|
}
|
|
private void iniView() {
|
|
c_value = (EditText) mView.findViewById(R.id.c_value);
|
T_TextView = (EditText) mView.findViewById(R.id.Temperature);
|
key_state = (EditText) mView.findViewById(R.id.key_status);
|
Seekbar_value = (TextView) mView.findViewById(R.id.seekbar_value);
|
|
loop1Button = (Switch) mView.findViewById(R.id.loopId1);
|
loop1Button.setTag("0");
|
loop1Button.setOnClickListener(onClick);
|
|
loop2Button = (Switch) mView.findViewById(R.id.loopId2);
|
loop2Button.setTag("0");
|
loop2Button.setOnClickListener(onClick);
|
|
loop3Button = (Switch) mView.findViewById(R.id.loopId3);
|
loop3Button.setTag("0");
|
loop3Button.setOnClickListener(onClick);
|
|
loop4Button = (Switch) mView.findViewById(R.id.loopId4);
|
loop4Button.setTag("0");
|
loop4Button.setOnClickListener(onClick);
|
|
powerButton = (Switch) mView.findViewById(R.id.powerButton);
|
powerButton.setTag("Off");
|
powerButton.setOnClickListener(powerOnClick);
|
|
radioButton1 = (RadioButton) mView.findViewById(R.id.radioButton1);
|
radioButton2 = (RadioButton) mView.findViewById(R.id.radioButton2);
|
radioButton3 = (RadioButton) mView.findViewById(R.id.radioButton3);
|
radioButton4 = (RadioButton) mView.findViewById(R.id.radioButton4);
|
radioButton5 = (RadioButton) mView.findViewById(R.id.radioButton5);
|
radioButton6 = (RadioButton) mView.findViewById(R.id.radioButton6);
|
|
openBackLight = (Switch) mView.findViewById(R.id.openbacklight);
|
openBackLight.setTag("1");
|
openBackLight.setChecked(true);
|
openBackLight.setOnClickListener(backLightOnClick);
|
|
Btn_state = (Button) mView.findViewById(R.id.btn_state);
|
Btn_state.setTag(new Inductor());
|
Btn_state.setBackground(null);
|
|
C_Btn = (Button) mView.findViewById(R.id.c_btn);
|
C_Btn.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
public void onClick(View v) {
|
// 设置补偿值
|
try {
|
int _value = Integer.parseInt(c_value.getText().toString());
|
byte[] sendBytes = SendDatas.AddSendData(Commands.SET_TEMP_COMPENSATE_VALUE.getCommand(),
|
Global.SubNet_Id, Global.Device_Id,
|
new byte[] { (byte) 1, (byte) 0, (byte) (_value + 10) });
|
ReceiveAndSend.SendDatasList.add(sendBytes);
|
|
SharedPreferences SP = MainActivity.CuttentActivity.getSharedPreferences("Serialport",
|
MainActivity.CuttentActivity.MODE_PRIVATE);
|
SharedPreferences.Editor editor = SP.edit();
|
editor.putInt("c_value", _value);
|
editor.commit();
|
editor.apply();
|
|
InputMethodManager imm = (InputMethodManager) MainActivity.CuttentActivity
|
.getSystemService(MainActivity.CuttentActivity.INPUT_METHOD_SERVICE);
|
// 得到InputMethodManager的实例
|
if (imm.isActive()) {
|
// 如果开启
|
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
|
// 关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
|
}
|
} catch (Exception e) {
|
|
}
|
}
|
});
|
|
SSeekbar = (SeekBar) mView.findViewById(R.id.set_sensitivity);
|
SSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
|
|
@Override
|
public void onStopTrackingTouch(SeekBar seekBar) {
|
// 写
|
int _value = seekBar.getProgress();
|
byte[] sendBytes = SendDatas.AddSendData(Commands.Sensor.getCommand(), Global.SubNet_Id,
|
Global.Device_Id, new byte[] { 1, (byte) _value });
|
ReceiveAndSend.SendDatasList.add(sendBytes);
|
}
|
|
@Override
|
public void onStartTrackingTouch(SeekBar seekBar) {
|
}
|
|
@Override
|
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
Seekbar_value.setText(progress + "");
|
}
|
});
|
}
|
|
private int getC_value() {
|
SharedPreferences SP = MainActivity.CuttentActivity.getSharedPreferences("Serialport",
|
MainActivity.CuttentActivity.MODE_PRIVATE);
|
int _value = SP.getInt("c_value", 0);
|
return _value;
|
}
|
|
private void iniData() {
|
|
mContext = getContext();
|
|
ReceiveAndSend.SendDatasList.clear();
|
|
ReadSensitivity();
|
|
c_value.setText(getC_value() + "");
|
}
|
|
private void ReadSensitivity() {
|
byte[] sendBytes = SendDatas.AddSendData(Commands.Sensor.getCommand(), Global.SubNet_Id, Global.Device_Id,
|
new byte[] { 0 });
|
ReceiveAndSend.SendDatasList.add(sendBytes);
|
}
|
|
public static Handler mHandler = new Handler() {
|
|
@Override
|
public void handleMessage(Message msg) {
|
|
Global.BUS bus;
|
try {
|
bus = (Global.BUS) msg.obj;
|
} catch (Exception e) {
|
bus = null;
|
}
|
|
if (bus == null)
|
return;
|
|
switch (bus.index) {
|
case -1:
|
Toast.makeText(mContext, bus.str, Toast.LENGTH_LONG).show();
|
break;
|
case 1: {
|
if (bus.contact1 == 0) {
|
radioButton1.setChecked(true);
|
} else {
|
radioButton1.setChecked(false);
|
}
|
|
if (bus.contact2 == 0) {
|
radioButton2.setChecked(true);
|
} else {
|
radioButton2.setChecked(false);
|
}
|
|
if (bus.contact3 == 0) {
|
radioButton3.setChecked(true);
|
} else {
|
radioButton3.setChecked(false);
|
}
|
|
if (bus.contact4 == 0) {
|
radioButton4.setChecked(true);
|
} else {
|
radioButton4.setChecked(false);
|
}
|
|
if (bus.contact5 == 0) {
|
radioButton5.setChecked(true);
|
} else {
|
radioButton5.setChecked(false);
|
}
|
|
if (bus.contact6 == 0) {
|
radioButton6.setChecked(true);
|
} else {
|
radioButton6.setChecked(false);
|
}
|
}
|
break;
|
case 2: {
|
|
}
|
break;
|
case 3: {
|
|
}
|
break;
|
case 4: {
|
|
}
|
break;
|
case 5: {
|
|
}
|
break;
|
case 6: {
|
|
}
|
break;
|
case 7: {
|
// 温度
|
if (bus.value == 0)// 摄氏度
|
T_TextView.setText(bus.value2 + "℃");
|
else
|
T_TextView.setText(bus.value2 + "℉");
|
}
|
break;
|
case 8: {
|
// 传感器灵敏度
|
SSeekbar.setProgress(bus.value);
|
Seekbar_value.setText(bus.value + "");
|
}
|
break;
|
case 9: {// 传感器探测
|
Btn_state.setBackgroundColor(Color.RED);
|
((Inductor) Btn_state.getTag()).LastTime = Calendar.getInstance();
|
}
|
break;
|
case 10: {// 按键检测
|
if (bus.value == 0) {
|
key_state.setText("KEY_UP");
|
} else if (bus.value == 1) {
|
key_state.setText("KEY_DOWN");
|
} else if (bus.value == 2) {
|
key_state.setText("KEY_LONGPRESS");
|
}
|
}
|
break;
|
case 11:
|
try {
|
if (bus.value == 1) {
|
powerButton.setChecked(true);
|
powerButton.setTag("On");
|
} else {
|
powerButton.setChecked(false);
|
powerButton.setTag("Off");
|
}
|
} catch (Exception e) {
|
e.getMessage();
|
}
|
break;
|
case 12:
|
break;
|
case 13:
|
break;
|
case 14:
|
break;
|
case 15:
|
break;
|
case 16:
|
break;
|
case 17:
|
break;
|
case 18:
|
break;
|
case 19:
|
break;
|
}
|
}
|
|
};
|
|
public static Handler MainHandler = new Handler() {
|
|
@Override
|
public void handleMessage(Message msg) {
|
super.handleMessage(msg);
|
|
switch (msg.what) {
|
case INDUCTOR_LEAVE:
|
Btn_state.setBackground(null);
|
break;
|
default:
|
break;
|
}
|
}
|
|
};
|
|
private android.view.View.OnClickListener onClick = new android.view.View.OnClickListener() {
|
|
@Override
|
public void onClick(View v) {
|
|
Switch view = (Switch) v;
|
if (Global.mOutputStream == null) {
|
Toast.makeText(getActivity(), "Please Open Serial Port", android.widget.Toast.LENGTH_SHORT).show();
|
view.setChecked(false);
|
return;
|
}
|
int tag = Integer.parseInt(view.getTag().toString());
|
|
if (tag == 0) {
|
view.setTag("100");
|
view.setChecked(true);
|
} else {
|
view.setTag("0");
|
view.setChecked(false);
|
}
|
tag = Integer.parseInt(view.getTag().toString());
|
if (view.equals(loop1Button)) {
|
setData(1, tag);
|
} else if (view.equals(loop2Button)) {
|
setData(2, tag);
|
} else if (view.equals(loop3Button)) {
|
setData(3, tag);
|
} else if (view.equals(loop4Button)) {
|
setData(4, tag);
|
}
|
}
|
};
|
|
private android.view.View.OnClickListener powerOnClick = new android.view.View.OnClickListener() {
|
|
@Override
|
public void onClick(View view) {
|
|
if (view.getTag().toString().equals("On")) {
|
view.setTag("Off");
|
Global.mApplication.closeSerialPort();
|
try {
|
Global.mOutputStream.close();
|
Global.mOutputStream = null;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return;
|
}
|
powerButton.setChecked(false);
|
} else if (view.getTag().toString().equals("Off")) {
|
ReceiveAndSend.getSerialPort();
|
}
|
}
|
};
|
|
private android.view.View.OnClickListener backLightOnClick = new android.view.View.OnClickListener() {
|
|
@Override
|
public void onClick(View v) {
|
|
Switch view = (Switch) v;
|
if (view.isChecked()) {
|
// sendBackLight(1);
|
if (0 < SerialPort.BackLight_ON()) {
|
Toast.makeText(getActivity(), "-1", Toast.LENGTH_SHORT).show();
|
}
|
|
} else {
|
// sendBackLight(0);
|
if (0 < SerialPort.BackLight_OFF()) {
|
Toast.makeText(getActivity(), "-1", Toast.LENGTH_SHORT).show();
|
}
|
}
|
}
|
};
|
|
private android.view.View.OnTouchListener onTouch = new android.view.View.OnTouchListener() {
|
|
@Override
|
public boolean onTouch(View v, MotionEvent event) {
|
|
if (MotionEvent.ACTION_DOWN == event.getAction()) {
|
v.setBackgroundResource(R.drawable.selected);
|
if (Global.mOutputStream == null) {
|
Toast.makeText(getActivity(), "Please Open Serial Port", android.widget.Toast.LENGTH_SHORT).show();
|
} else {
|
setBuzzerData();
|
}
|
|
} else if (MotionEvent.ACTION_UP == event.getAction()) {
|
v.setBackgroundResource(R.drawable.unselected);
|
}
|
return false;
|
}
|
};
|
|
/** send buzzer */
|
private void setBuzzerData() {
|
|
byte[] sendBytes = SendDatas.AddSendData(Commands.buzzer.getCommand(), Global.SubNet_Id, Global.Device_Id,
|
new byte[] { (byte) 2, (byte) 5, (byte) 1 });
|
ReceiveAndSend.SendDatasList.add(sendBytes);
|
}
|
|
private void readDateTime() {
|
|
byte[] sendBytes = SendDatas.AddSendData(Commands.ReadTime.getCommand(), Global.SubNet_Id, Global.Device_Id,
|
new byte[] {});
|
ReceiveAndSend.SendDatasList.add(sendBytes);
|
}
|
|
private byte[] sendData(int loopId, int linght) {
|
|
byte[] sendBytes = SendDatas.AddSendData(Commands.regulationControl.getCommand(), Global.SubNet_Id,
|
Global.Device_Id, new byte[] { (byte) loopId, (byte) linght });
|
return sendBytes;
|
}
|
|
private void setData(int loopId, int linght) {
|
|
byte[] sendBytes = sendData(loopId, linght);
|
ReceiveAndSend.SendDatasList.add(sendBytes);
|
}
|
|
|
}
|