ControlDialog.java
8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.xgimi.smartscreen;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.xgimi.gimicinema.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class ControlDialog extends Dialog implements View.OnClickListener {
private static final String TAG = "ControlDialog";
private Context mContext;
private String mDeviceIp;
private TextView tvDeviceInfo;
private MessageThread messageThread;
private static final int PORT = 8009;
public ControlDialog(Context context, String DeviceIp) {
super(context);
this.mContext = context;
this.mDeviceIp = DeviceIp;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_control);
TextView tvDeviceIp = (TextView) findViewById(R.id.tv_dialog_ip);
tvDeviceIp.setText("[ " + mDeviceIp + " ]");
tvDeviceInfo = (TextView) findViewById(R.id.tv_device_info);
tvDeviceInfo.setMovementMethod(ScrollingMovementMethod.getInstance());
tvDeviceInfo.setText("序列号:\n\n软件版本:\n\n硬件版本:\n\n幕布状态:");
TextView tvUp = (TextView) findViewById(R.id.tv_up);
tvUp.setOnClickListener(this);
TextView tvDown = (TextView) findViewById(R.id.tv_down);
tvDown.setOnClickListener(this);
messageThread = new MessageThread();
new Thread(messageThread).start();
new Handler().postDelayed(new Runnable() {
public void run() {
getDeviceInfo();
}
}, 1000);
}
private void getDeviceInfo() {
JSONObject dataGetInfo = new JSONObject();
try {
dataGetInfo.put("cmd", 1);
} catch (JSONException e) {
e.printStackTrace();
}
Message msgGetInfo = new Message();
msgGetInfo.what = 0x345;
msgGetInfo.obj = dataGetInfo;
messageThread.sendHandler.sendMessage(msgGetInfo);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_up:/*
MessageEvent msgEvent = new MessageEvent();
msgEvent.setEventId(SmartScreenBean.UPACTION);
msgEvent.setMessage("192.168.31.164");
EventBus.getDefault().post(msgEvent);*/
JSONObject dataUp = new JSONObject();
getDeviceInfo();
try {
dataUp.put("cmd", 2);
dataUp.put("action", 0);
} catch (JSONException e) {
e.printStackTrace();
}
Message msgUp = new Message();
msgUp.what = 0x345;
msgUp.obj = dataUp;
messageThread.sendHandler.sendMessage(msgUp);
break;
case R.id.tv_down:
/* MessageEvent msgEvent1 = new MessageEvent();
msgEvent1.setEventId(SmartScreenBean.DOWNACTION);
msgEvent1.setMessage("192.168.31.164");
EventBus.getDefault().post(msgEvent1);*/
JSONObject dataDown = new JSONObject();
try {
dataDown.put("cmd", 2);
dataDown.put("action", 1);
} catch (JSONException e) {
e.printStackTrace();
}
Message msgDown = new Message();
msgDown.what = 0x345;
msgDown.obj = dataDown;
messageThread.sendHandler.sendMessage(msgDown);
break;
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x123) {
String data = (msg.obj).toString();
try {
JSONObject jsonObject = new JSONObject(data);
int cmd = jsonObject.getInt("cmd");
if (cmd == 1) {
int sn = jsonObject.getInt("sn");
String swver = jsonObject.getString("swver");
String hdver = jsonObject.getString("hdver");
tvDeviceInfo.setText("序列号:" + sn + "\n\n软件版本:" + swver + "\n\n硬件版本:" + hdver);
} else if (cmd == 2) {
int result = jsonObject.getInt("result");
if (result == 1) { //成功
Toast.makeText(mContext, "操作成功", Toast.LENGTH_SHORT).show();
} else if (result == 0) { //失败
Toast.makeText(mContext, "操作失败", Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
public class MessageThread implements Runnable {
//接收UI线程消息的Handler对象
public Handler sendHandler;
private Socket socket;
BufferedReader br = null;
PrintWriter pw = null;
@Override
public void run() {
try {
socket = new Socket(mDeviceIp, PORT);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
//启动一个线程来读取数据
new Thread(new Runnable() {
@Override
public void run() {
String data = null;
try {
while ((data = br.readLine()) != null) {
Log.i(TAG, "接收到的数据:" + data);
Message msg = new Message();
msg.what = 0x123;
msg.obj = data;
handler.sendMessage(msg);
}
} catch (IOException e) {
Log.i(TAG, "接收数据异常:" + e.getMessage());
e.printStackTrace();
}
}
}).start();
Looper.prepare();
sendHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x345) {
Log.i(TAG, "写数据:" + (msg.obj).toString());
// pw.write((msg.obj).toString());
pw.write("{\"cmd\":2,\"action\":0}");
pw.flush();
}
}
};
//启动Looper
Looper.loop();
} catch (SocketTimeoutException e) {
Log.i(TAG, "网络连接超时!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (pw != null) {
pw.close();
}
if (br != null) {
br.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}