-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathWiFiCarController.java
More file actions
375 lines (342 loc) · 14.3 KB
/
WiFiCarController.java
File metadata and controls
375 lines (342 loc) · 14.3 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package com.fei435;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.UnknownHostException;
import com.fei435.Constant;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
public class WiFiCarController{ //WiFiCar连接的封装类
private boolean mThreadFlag = false;//这是网络连接线程的flag
private int mSocketStatus = Constant.STATUS_INIT;
private boolean bReaddyToSendCmd = true;
private SocketClient mtcpSocket; //此为socket的状态,getWiFiStatus获取到的是WiFi网络的状态
private Handler mHandler;
private TextView mLogText;
private Context mContext;
private ControlThread mThreadClient = null;
public WiFiCarController (Handler mHandler, TextView mLogText, Context mContext) {
this.mHandler = mHandler;
this.mLogText = mLogText;
this.mContext = mContext;
getWifiStatus(); //获取WiFi状态保存到Constant中
}
/**
* bytes转换成十六进制字符串
* @param byte[] b byte数组
* @return String 每个Byte值之间空格分隔
*/
private String byte2HexStr(byte[] b){
String stmp="";
StringBuilder sb = new StringBuilder("");
for (int n=0;n<b.length;n++)
{
stmp = Integer.toHexString(b[n] & 0xFF);
sb.append((stmp.length()==1)? "0"+stmp : stmp);
sb.append(" ");
}
return sb.toString().toUpperCase().trim();
}
private String byte2IntStr(byte[] b){
String stmp="";
StringBuilder sb = new StringBuilder("");
for (int n=0;n<b.length;n++)
{
stmp = Integer.toString(b[n] & 0xFF);
sb.append((stmp.length()==1)? "0"+stmp : stmp);
sb.append(" ");
}
return sb.toString().toUpperCase().trim();
}
private void initWifiConnection() { //在此函数中已经尝试打开socket,并做错误判断
mSocketStatus = Constant.STATUS_INIT;
Log.i("Socket", "initWifiConnection");
try {
if (mtcpSocket != null) {
mtcpSocket.closeSocket();
}
String clientUrl = Constant.ROUTER_CONTROL_URL;
int clientPort = Constant.ROUTER_CONTROL_PORT;
if (Constant.m4test) {
clientUrl = Constant.ROUTER_CONTROL_URL_TEST;
clientPort = Constant.ROUTER_CONTROL_PORT_TEST;
}
try {
mtcpSocket = new SocketClient(clientUrl, clientPort);
mSocketStatus = Constant.STATUS_CONNECTED;
Log.i("socket", "Wifi Connect created ip=" + clientUrl + " port=" + clientPort);
} catch (UnknownHostException e) {
e.printStackTrace();
Log.e("socket", "creating socket error UnknownHostException:"+e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e("socket", "creating socket error IOException:"+e.toString());
}
} catch (Exception e) {
Log.d("socket", "initWifiConnection exception:"+e.toString());
}
Message msg = new Message();
if (mSocketStatus != Constant.STATUS_CONNECTED || null == mtcpSocket) {
msg.what = Constant.MSG_ID_ERR_CONN;
} else {
msg.what = Constant.MSG_ID_CON_SUCCESS;
}
mHandler.sendMessage(msg);
}
//发送消息给mLogText,参数String是mLogText将要显示的内容
private void setUiInfo(String str){
Message msg = new Message();
msg.what = Constant.MSG_ID_SET_UI_INFO;
msg.obj = str;
mHandler.sendMessage(msg);
}
public class ControlThread extends Thread{ //接受数据包的线程
public void run()
{
Log.i("socket thread", "mThreadClient 已经开始");
BufferedInputStream is = null;
try {
Log.i("socket", "WiFiConnection init complete");
//取得输入、输出流
//mBufferedReaderClient = new BufferedReader(new InputStreamReader(mtcpSocket.getInputStream()));//这个是字符流,没用
is = new BufferedInputStream(mtcpSocket.getInputStream());
} catch (Exception e) {
Message msg = new Message();
msg.what = Constant.MSG_ID_ERR_INIT_READ;
mHandler.sendMessage(msg);
return;
}
byte[] buffer = new byte[1024];
long lastTicket = System.currentTimeMillis();
byte[] command = {0,0,0,0,0};
int commandLength = 0;
int i = 0;
while (mThreadFlag)
{
if(mSocketStatus == Constant.STATUS_CONNECTED &&
getWifiStatus() == Constant.WIFI_STATE_CONNECTED){
try {
Log.i("socket thread", "mThreadClient work 1s");
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// try
// {
// //Log.i("socket thread","mThreadFlag:" + mThreadFlag+System.currentTimeMillis());
// int ret = is.read(buffer);
// Log.i("socket thread","is.read(buffer) ret="+ret);
// if (ret > 0) {
//
// printRecBuffer("receive buffer", buffer, ret);
//
// if(ret > 0 && ret <= Constant.COMMAND_LENGTH ) {
// long newTicket = System.currentTimeMillis();
// long ticketInterval = newTicket - lastTicket;
// Log.d("Socket", "time ticket interval =" + ticketInterval);
//
// //距离上次接收小于1000ms才组包,否则大于1000ms就算是接收完了或者丢弃
// if (ticketInterval < Constant.MIN_COMMAND_REC_INTERVAL) { //小车端发了一半命令,但是没有发完,然后1s之内又发过来,在buffer中取ret大的数据追加到command中,最多追加commandLenth
// if (commandLength > 0) {
// commandLength = appendBuffer(buffer, ret, command, commandLength);//
// } else {
// Log.d("Socket", "not recognized command_1"); //若1s之内没有下文了,则丢弃包
// }
// } else {
// if (buffer[0] == Constant.COMMAND_PERFIX ) { //新收到的包
// for (i = 0; i < ret; i++) {
// command[i] = buffer[i];
// }
// commandLength = ret;
// } else {
// Log.d("Socket", "not recognized command_2");
// commandLength = 0;
// }
// }
//
// lastTicket = newTicket; //更新时间戳
// printRecBuffer ("print command", command, commandLength);
//
// if (commandLength >= Constant.COMMAND_LENGTH) { //判断是否已经接受完一条命令 实际上等于就够了
// Message msg = new Message();
// msg.what = Constant.MSG_ID_CON_READ;
// msg.obj = command;
// mHandler.sendMessage(msg);
// commandLength = 0;
// }
// }
// }
// } catch (Exception e) {
// Message msg = new Message();
// Log.i("socket thread", e.toString());
// msg.what = Constant.MSG_ID_ERR_RECEIVE;
// mHandler.sendMessage(msg);
// }
} else{
try {
Log.i("socket thread", "WiFi或者socket连接未就绪,sleep(100)");
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Log.i("socket thread", "mThreadClient 已经终止");
}
}
public void sendCommand(byte[] data) { //发送命令的函数
if ( mSocketStatus != Constant.STATUS_CONNECTED || null == mtcpSocket) {
setUiInfo("状态异常, 无法发送命令 " + byte2IntStr(data));
Log.i("socket command","状态异常, 无法发送命令 " + byte2HexStr(data));
return;
}
if (!bReaddyToSendCmd) {
setUiInfo("please wait 1 second to send msg ....");
Log.i("socket","not ready to send command,wait 1s pls");
return;
}
//调试命令发送时使用
//tag:(mlogtext|socket|settingclick|SurfaceStatus|heart|inspect|MjpegView|ScreenCapture|filelock|speed)
//tag:(MjpegView|ScreenCapture|filelock)
//来在logcat做filter
try {
mtcpSocket.sendMsg(data);
setUiInfo("发送命令" + byte2IntStr(data) + "到WiFiCar成功");
Log.i("socket command","发送命令" + byte2HexStr(data) + "到WiFiCar成功");
} catch (Exception e) {
Log.i("Socket", e.getMessage() != null ? e.getMessage().toString() : "sendCommand error!");
Log.i("socket", e.toString());
setUiInfo("发送命令" + byte2IntStr(data) + "到WiFiCar失败,请检查连接");
Log.i("socket command","发送命令" + byte2HexStr(data) + "到WiFiCar失败,请检查连接");
}
}
//此函数获取WiFi连接状态
private int getWifiStatus () {
int status = Constant.WIFI_STATE_UNKNOW;
ConnectivityManager conMan = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
WifiManager mWifiMng = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
switch (mWifiMng.getWifiState()) {
case WifiManager.WIFI_STATE_DISABLED:
case WifiManager.WIFI_STATE_DISABLING:
case WifiManager.WIFI_STATE_ENABLING:
case WifiManager.WIFI_STATE_UNKNOWN:
status = Constant.WIFI_STATE_DISABLED;
break;
case WifiManager.WIFI_STATE_ENABLED:
status = Constant.WIFI_STATE_NOT_CONNECTED;
State wifiState = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if (State.CONNECTED == wifiState) {
WifiInfo info = mWifiMng.getConnectionInfo();
if (null != info) {
String bSSID = info.getBSSID();
String SSID = info.getSSID();
Log.i("socket", "getWifiStatus bssid=" + bSSID + " ssid=" + SSID);
if (null != SSID && SSID.length() > 0) {
//if (SSID.toLowerCase().contains(Constant.WIFI_SSID_PERFIX)) {
status = Constant.WIFI_STATE_CONNECTED;
//}
}
}
}
break;
default:
break;
}
Constant.CURRENT_WIFI_STATE = status;
return status;
}
public void connectToRouter() {
int status = getWifiStatus(); //获取WiFi连接状态
if (Constant.WIFI_STATE_CONNECTED == status || Constant.m4test) {
//连接服务器
initWifiConnection();
if (mSocketStatus == Constant.STATUS_CONNECTED){
if(!mThreadFlag){
mThreadFlag = true;
//网络连接线程
try {
mThreadClient = new ControlThread();
mThreadClient.start();
} catch (IllegalThreadStateException e) {
Log.e("socket", "mThreadClient 启动失败" + e.getMessage());
}
}
} else {
setUiInfo("连接到WiFiCar失败,控制地址有误!");
Log.i("socket","连接到WiFiCar失败,控制地址有误!");
}
} else if (Constant.WIFI_STATE_NOT_CONNECTED == status) {
setUiInfo("初始化连接路由器失败,wifi未连接!");
Log.i("socket","初始化连接路由器失败,wifi未连接!");
} else {
setUiInfo("初始化连接路由器失败,wifi未开启!");
Log.i("socket","初始化连接路由器失败,wifi未开启!");
}
}
public void disconnFromRouter() {
int status = getWifiStatus();
if (Constant.WIFI_STATE_CONNECTED == status && mThreadFlag) {
Log.i("socket thread", "mThreadClient status:try join");
mThreadFlag = false;
boolean retry = true;
while (retry) {
try {
mThreadClient.join();
Log.i("socket thread", "mThreadClient status:join");
retry = false;
} catch (InterruptedException e) {
Log.i("socket", "关闭mThreadClient失败:"+e.toString());
e.printStackTrace();
}
}
}
//关闭socket
if(null != mtcpSocket) {
try {
Log.i("socket", "关闭mtcpSocket..");
mtcpSocket.closeSocket();
} catch (Exception e) {
e.printStackTrace();
Log.i("socket", "error closing socket:"+e.toString());
}
}
if (null != mHandler) {
int i;
for (i = Constant.MSG_ID_LOOP_START + 1; i < Constant.MSG_ID_LOOP_END; i++ ) {
mHandler.removeMessages(i);
}
}
}
private int appendBuffer (byte[] buffer, int len, byte[] dstBuffer, int dstLen) {
int j = 0;
int i = dstLen;
for (i = dstLen; i < Constant.COMMAND_LENGTH && j < len; i++) {
dstBuffer[i] = buffer[j];
j++;
}
return i;
}
//打印接收到的数据包
void printRecBuffer(String tag, byte[] buffer, int len) {
StringBuilder sb = new StringBuilder();
sb.append(tag);
sb.append(" len = ");
sb.append(len);
sb.append(" :");
for (int i =0 ;i < len; i++) {
sb.append(buffer[i]);
sb.append(", ");
}
Log.i("socket printRecBuffer", sb.toString());
}
public void selfcheck() {
sendCommand(Constant.COMM_SELF_CHECK);
}
}