package android.serialport.api.sample;
|
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
|
import android.app.Activity;
|
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.View;
|
import android.view.ViewGroup;
|
import android.widget.Button;
|
import android.widget.EditText;
|
import android.widget.Toast;
|
|
public class Fragment_KNX extends Fragment {
|
|
private static Activity CurentActivity;
|
private OutputStream mOutputStream;
|
private InputStream mInputStream;
|
private KNX_Serialport mKNX_SerialPort;
|
private SerialPort mSerialPort;
|
|
private View mView;
|
private static Button mSwitchButton_1;
|
private static Button mLight_1;
|
private static EditText mObjectEditText1_1;
|
private static EditText mObjectEditText1_2;
|
|
private static Button mSwitchButton_2;
|
private static Button mLight_2;
|
private static EditText mObjectEditText2_1;
|
private static EditText mObjectEditText2_2;
|
|
private static Button mSwitchButton_3;
|
private static Button mLight_3;
|
private static EditText mObjectEditText3_1;
|
private static EditText mObjectEditText3_2;
|
|
private static Button mSwitchButton_4;
|
private static Button mLight_4;
|
private static EditText mObjectEditText4_1;
|
private static EditText mObjectEditText4_2;
|
|
@Override
|
public void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
}
|
|
@Override
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
mView = inflater.inflate(R.layout.fragment_knx, container, false);
|
|
iniView();
|
|
iniData();
|
|
return mView;
|
}
|
|
@Override
|
public void onDestroy() {
|
super.onDestroy();
|
}
|
|
@Override
|
public void onStart() {
|
super.onStart();
|
}
|
|
@Override
|
public void onStop() {
|
super.onStop();
|
}
|
|
private void iniView() {
|
|
mSwitchButton_1 = (Button) mView.findViewById(R.id.switch_btn1);
|
mLight_1 = (Button) mView.findViewById(R.id.light1_1);
|
mSwitchButton_1.setOnClickListener(OnClick);
|
mLight_1.setOnClickListener(OnClickLight);
|
mLight_1.setTag((int) 0);
|
mLight_1.setBackground(getResources().getDrawable(R.drawable.light_def));
|
|
mSwitchButton_2 = (Button) mView.findViewById(R.id.switch_btn2);
|
mLight_2 = (Button) mView.findViewById(R.id.light_2);
|
mSwitchButton_2.setOnClickListener(OnClick);
|
mLight_2.setOnClickListener(OnClickLight);
|
mLight_2.setTag((int) 0);
|
mLight_2.setBackground(getResources().getDrawable(R.drawable.light_def));
|
|
mSwitchButton_3 = (Button) mView.findViewById(R.id.switch_btn3);
|
mLight_3 = (Button) mView.findViewById(R.id.light_3);
|
mSwitchButton_3.setOnClickListener(OnClick);
|
mLight_3.setOnClickListener(OnClickLight);
|
mLight_3.setTag((int) 0);
|
mLight_3.setBackground(getResources().getDrawable(R.drawable.light_def));
|
|
mSwitchButton_4 = (Button) mView.findViewById(R.id.switch_btn4);
|
mLight_4 = (Button) mView.findViewById(R.id.light_4);
|
mSwitchButton_4.setOnClickListener(OnClick);
|
mLight_4.setOnClickListener(OnClickLight);
|
mLight_4.setTag((int) 0);
|
mLight_4.setBackground(getResources().getDrawable(R.drawable.light_def));
|
|
mObjectEditText1_1 = (EditText) mView.findViewById(R.id.edit_obj1_1);
|
mObjectEditText1_2 = (EditText) mView.findViewById(R.id.edit_obj1_2);
|
mObjectEditText1_2.setTag("");
|
|
mObjectEditText2_1 = (EditText) mView.findViewById(R.id.edit_obj2_1);
|
mObjectEditText2_2 = (EditText) mView.findViewById(R.id.edit_obj2_2);
|
mObjectEditText2_2.setTag("");
|
|
mObjectEditText3_1 = (EditText) mView.findViewById(R.id.edit_obj3_1);
|
mObjectEditText3_2 = (EditText) mView.findViewById(R.id.edit_obj3_2);
|
mObjectEditText3_2.setTag("");
|
|
mObjectEditText4_1 = (EditText) mView.findViewById(R.id.edit_obj4_1);
|
mObjectEditText4_2 = (EditText) mView.findViewById(R.id.edit_obj4_2);
|
mObjectEditText4_2.setTag("");
|
}
|
|
private void iniData() {
|
|
CurentActivity = getActivity();
|
|
SetControlData(mObjectEditText1_2, mObjectEditText1_2.getText().toString());
|
|
SetControlData(mObjectEditText2_2, mObjectEditText2_2.getText().toString());
|
//
|
SetControlData(mObjectEditText3_2, mObjectEditText3_2.getText().toString());
|
//
|
SetControlData(mObjectEditText4_2, mObjectEditText4_2.getText().toString());
|
|
getSerialPort();
|
|
KNX_ReceiveAndSend.Start(mKNX_SerialPort, mOutputStream, mInputStream);
|
}
|
|
/**
|
* Open the serial port, input and output streams
|
*/
|
private void getSerialPort() {
|
|
mKNX_SerialPort = new KNX_Serialport();
|
|
try {
|
|
mSerialPort = mKNX_SerialPort.getSerialPort();
|
mOutputStream = mSerialPort.getOutputStream();
|
mInputStream = mSerialPort.getInputStream();
|
|
} catch (Exception e) {
|
e.getMessage();
|
}
|
}
|
|
private static void SetLightState(Button btn, int _state) {
|
|
if (_state == 0) {
|
btn.setBackground(CurentActivity.getResources().getDrawable(R.drawable.light_def));
|
btn.setTag(0);
|
} else {
|
btn.setBackground(CurentActivity.getResources().getDrawable(R.drawable.light));
|
btn.setTag(1);
|
}
|
}
|
|
private static void SetControlData(View v, String str) {
|
|
if (str.equals(""))
|
return;
|
try {
|
int obj1 = Integer.parseInt(str.split("/")[0].trim());
|
int obj2 = Integer.parseInt(str.split("/")[1].trim());
|
int obj3 = Integer.parseInt(str.split("/")[2].trim());
|
v.setTag(KNX.set_Group_addr(obj1, obj2, obj3));
|
} catch (Exception e) {
|
e.getMessage();
|
}
|
}
|
|
public static Handler mHandler = new Handler() {
|
|
@Override
|
public void handleMessage(Message msg) {
|
super.handleMessage(msg);
|
|
String str = Integer.toString(msg.arg1);
|
if (str.equals(mObjectEditText1_2.getTag().toString())) {
|
SetLightState(mLight_1, msg.arg2);
|
|
} else if (str.equals(mObjectEditText2_2.getTag().toString())) {
|
SetLightState(mLight_2, msg.arg2);
|
|
} else if (str.equals(mObjectEditText3_2.getTag().toString())) {
|
SetLightState(mLight_3, msg.arg2);
|
|
} else if (str.equals(mObjectEditText4_2.getTag().toString())) {
|
SetLightState(mLight_4, msg.arg2);
|
|
}
|
}
|
|
};
|
|
private android.view.View.OnClickListener OnClickLight = new android.view.View.OnClickListener() {
|
|
@Override
|
public void onClick(View v) {
|
|
if (mObjectEditText1_1.getText().toString() == "" || mObjectEditText2_1.getText().toString() == ""
|
|| mObjectEditText3_1.getText().toString() == "" || mObjectEditText4_1.getText().toString() == ""
|
|| mObjectEditText2_2.getText().toString() == "" || mObjectEditText2_2.getText().toString() == ""
|
|| mObjectEditText3_2.getText().toString() == "" || mObjectEditText4_2.getText().toString() == "") {
|
Toast.makeText(CurentActivity, "Group_addr can not be empty", Toast.LENGTH_SHORT).show();
|
return;
|
}
|
|
if (v.equals(mLight_1)) {
|
|
try {
|
String str = mObjectEditText1_1.getText().toString();
|
int obj1 = Integer.parseInt(str.split("/")[0].trim());
|
int obj2 = Integer.parseInt(str.split("/")[1].trim());
|
int obj3 = Integer.parseInt(str.split("/")[2].trim());
|
int state = Integer.parseInt(mLight_1.getTag().toString());
|
state = state == 0 ? 1 : 0;
|
byte[] sendBytes = KNX.get_SendBuffer(12345, obj1, obj2, obj3, state);
|
String[] tmp = new String[sendBytes.length];
|
for (int i = 0; i < sendBytes.length; i++) {
|
tmp[i] = Integer.toHexString(sendBytes[i] & 0xFF);
|
}
|
|
mOutputStream.write(sendBytes, 0, sendBytes[1] & 0xFF);
|
// 根据长度截取数据发送
|
mOutputStream.flush();
|
|
} catch (Exception e) {
|
e.getMessage();
|
}
|
} else if (v.equals(mLight_2)) {
|
try {
|
String str = mObjectEditText2_1.getText().toString();
|
int obj1 = Integer.parseInt(str.split("/")[0].trim());
|
int obj2 = Integer.parseInt(str.split("/")[1].trim());
|
int obj3 = Integer.parseInt(str.split("/")[2].trim());
|
int state = Integer.parseInt(mLight_2.getTag().toString());
|
state = state == 0 ? 1 : 0;
|
byte[] sendBytes = KNX.get_SendBuffer(12345, obj1, obj2, obj3, state);
|
String[] tmp = new String[sendBytes.length];
|
for (int i = 0; i < sendBytes.length; i++) {
|
tmp[i] = Integer.toHexString(sendBytes[i] & 0xFF);
|
}
|
|
mOutputStream.write(sendBytes, 0, sendBytes[1] & 0xFF);
|
// 根据长度截取数据发送
|
mOutputStream.flush();
|
|
} catch (Exception e) {
|
e.getMessage();
|
}
|
} else if (v.equals(mLight_3)) {
|
try {
|
String str = mObjectEditText3_1.getText().toString();
|
int obj1 = Integer.parseInt(str.split("/")[0].trim());
|
int obj2 = Integer.parseInt(str.split("/")[1].trim());
|
int obj3 = Integer.parseInt(str.split("/")[2].trim());
|
int state = Integer.parseInt(mLight_3.getTag().toString());
|
state = state == 0 ? 1 : 0;
|
byte[] sendBytes = KNX.get_SendBuffer(12345, obj1, obj2, obj3, state);
|
String[] tmp = new String[sendBytes.length];
|
for (int i = 0; i < sendBytes.length; i++) {
|
tmp[i] = Integer.toHexString(sendBytes[i] & 0xFF);
|
}
|
|
mOutputStream.write(sendBytes, 0, sendBytes[1] & 0xFF);
|
// 根据长度截取数据发送
|
mOutputStream.flush();
|
|
} catch (Exception e) {
|
e.getMessage();
|
}
|
} else if (v.equals(mLight_4)) {
|
try {
|
String str = mObjectEditText4_1.getText().toString();
|
int obj1 = Integer.parseInt(str.split("/")[0].trim());
|
int obj2 = Integer.parseInt(str.split("/")[1].trim());
|
int obj3 = Integer.parseInt(str.split("/")[2].trim());
|
int state = Integer.parseInt(mLight_4.getTag().toString());
|
state = state == 0 ? 1 : 0;
|
byte[] sendBytes = KNX.get_SendBuffer(12345, obj1, obj2, obj3, state);
|
String[] tmp = new String[sendBytes.length];
|
for (int i = 0; i < sendBytes.length; i++) {
|
tmp[i] = Integer.toHexString(sendBytes[i] & 0xFF);
|
}
|
|
mOutputStream.write(sendBytes, 0, sendBytes[1] & 0xFF);
|
// 根据长度截取数据发送
|
mOutputStream.flush();
|
|
} catch (Exception e) {
|
e.getMessage();
|
}
|
}
|
}
|
};
|
|
private android.view.View.OnClickListener OnClick = new android.view.View.OnClickListener() {
|
|
@Override
|
public void onClick(View v) {
|
|
if (v.equals(mSwitchButton_1)) {
|
|
SetControlData(mObjectEditText1_2, mObjectEditText1_2.getText().toString());
|
} else if (v.equals(mSwitchButton_2)) {
|
//
|
SetControlData(mObjectEditText2_2, mObjectEditText2_2.getText().toString());
|
} else if (v.equals(mSwitchButton_3)) {
|
//
|
SetControlData(mObjectEditText3_2, mObjectEditText3_2.getText().toString());
|
} else if (v.equals(mSwitchButton_4)) {
|
//
|
SetControlData(mObjectEditText4_2, mObjectEditText4_2.getText().toString());
|
}
|
}
|
};
|
}
|