@@ -44,13 +44,15 @@ public class AutoDriveActivity extends AppCompatActivity {
44
44
public String newUrl ;
45
45
private boolean isClientRunning = false ;
46
46
public static int CMD = 99 ;
47
+ public static String targetLocation ;
47
48
public static int UDP_LocationReceivingPort = 11445 ;
48
49
49
50
Button btn_manualDrive , btn_camera , btn_showCoordinates , btn_GPS ;
50
51
WebView wb_liveFeed ;
51
52
EditText ipAddress , mapLat , mapLon ;
52
53
MapView map = null ;
53
54
DatagramSocket ds = null ;
55
+ String gpsresponse ;
54
56
55
57
@ Override
56
58
protected void onCreate (Bundle savedInstanceState ) {
@@ -140,8 +142,7 @@ public void onClick(View view) {
140
142
map .invalidate ();
141
143
142
144
getIPandPort ();
143
- SendTargetAsyncTask send_targetLocation = new SendTargetAsyncTask ();
144
- send_targetLocation .execute ();
145
+ new SendTargetAsyncTask ().execute ();
145
146
} catch (Exception e ) {
146
147
e .printStackTrace ();
147
148
}
@@ -153,7 +154,7 @@ public void onClick(View view) {
153
154
public void onClick (View view ) {
154
155
if (!isClientRunning ) {
155
156
Log .e ("UDP" , "onClick: Reading UDP Packet..." );
156
- receivePiLocation ();
157
+ new ReceivePiLocation (). execute ();
157
158
} else {
158
159
Log .e ("UDP" , "onClick: A Thread already started and not finished." );
159
160
}
@@ -162,41 +163,6 @@ public void onClick(View view) {
162
163
163
164
}
164
165
165
- public void setMapView (String result ) {
166
- //parse the result values
167
- String regex = "(.)*(\\ d)(.)*" ;
168
- Pattern pattern = Pattern .compile (regex );
169
- boolean containsNumber = pattern .matcher (result ).matches ();
170
-
171
- double lat = 0 ;
172
- double lon = 0 ;
173
- if (containsNumber ) {
174
- String [] coords = result .split ("," );
175
- lat = Double .parseDouble (coords [0 ]);
176
- String templon = coords [1 ];
177
- Pattern p = Pattern .compile ("\\ d*\\ .\\ d+" );
178
- Matcher m = p .matcher (templon );
179
- while (m .find ()) {
180
- lon = Double .parseDouble (m .group ());
181
- }
182
-
183
- final IMapController mapViewController = map .getController ();
184
- mapViewController .setCenter (new GeoPoint (lat , lon ));
185
- mapViewController .setZoom (15.0d );
186
-
187
- //add the gps location marker
188
- Marker GPSMarker = new Marker (map );
189
- final GeoPoint markerPointGPS = new GeoPoint (lat , lon );
190
- GPSMarker .setPosition (markerPointGPS );
191
- GPSMarker .setAnchor (Marker .ANCHOR_CENTER , Marker .ANCHOR_BOTTOM );
192
- GPSMarker .setIcon (ContextCompat .getDrawable (this , R .drawable .gps_marker ));
193
- GPSMarker .setTitle ("Device Location" );
194
- map .getOverlays ().add (GPSMarker );
195
- map .invalidate ();
196
- } else {
197
- Toast .makeText (getApplicationContext (), "Can't read GPS data." , Toast .LENGTH_LONG ).show ();
198
- }
199
- }
200
166
201
167
public void getIPandPort () {
202
168
String iPandPort = ipAddress .getText ().toString ();
@@ -230,45 +196,98 @@ protected void onStop() {
230
196
editor .commit ();
231
197
}
232
198
233
- private void receivePiLocation () {
234
- new Thread (new Runnable () {
235
- @ Override
236
- public void run () {
237
- String lText ;
238
- byte [] lMsg = new byte [16 ];
239
- DatagramPacket dp = new DatagramPacket (lMsg , lMsg .length );
240
- try {
241
- isClientRunning = true ;
242
- ds = new DatagramSocket (UDP_LocationReceivingPort );
243
- ds .setSoTimeout (7000 );
244
- ds .setReuseAddress (true );
245
- ds .receive (dp );
246
- lText = new String (dp .getData ());
247
- Log .e ("UDP" , "run: paket alındı!" + lText );
248
- } catch (SocketException e ) {
249
- e .printStackTrace ();
250
- } catch (IOException e ) {
251
- e .printStackTrace ();
252
- } finally {
253
- if (ds != null ) {
254
- ds .close ();
255
- isClientRunning = false ;
256
- }
199
+ public void setMapView (String result ) {
200
+ //parse the result values
201
+ String regex = "(.)*(\\ d)(.)*" ;
202
+ Pattern pattern = Pattern .compile (regex );
203
+ try {
204
+ boolean containsNumber = pattern .matcher (result ).matches ();
205
+ double lat = 0 ;
206
+ double lon = 0 ;
207
+ double header = 0 ;
208
+ if (containsNumber ) {
209
+ String [] coords = result .split ("/" );
210
+ lat = Double .parseDouble (coords [0 ]);
211
+ lon = Double .parseDouble (coords [1 ]);
212
+ String tempHeader = coords [2 ];
213
+ Pattern p = Pattern .compile ("\\ d*\\ .\\ d+" );
214
+ Matcher m = p .matcher (tempHeader );
215
+ while (m .find ()) {
216
+ header = Double .parseDouble (m .group ());
257
217
}
218
+
219
+ final IMapController mapViewController = map .getController ();
220
+ mapViewController .setCenter (new GeoPoint (lat , lon ));
221
+ mapViewController .setZoom (15.0d );
222
+
223
+ //add the gps location marker
224
+ Marker GPSMarker = new Marker (map );
225
+ final GeoPoint markerPointGPS = new GeoPoint (lat , lon );
226
+ GPSMarker .setPosition (markerPointGPS );
227
+ GPSMarker .setAnchor (Marker .ANCHOR_CENTER , Marker .ANCHOR_BOTTOM );
228
+ GPSMarker .setIcon (ContextCompat .getDrawable (this , R .drawable .gps_marker ));
229
+ GPSMarker .setTitle ("Device Location" );
230
+ map .getOverlays ().add (GPSMarker );
231
+ map .invalidate ();
232
+ } else {
233
+ Toast .makeText (getApplicationContext (), "Can't read GPS data." , Toast .LENGTH_LONG ).show ();
258
234
}
259
- }).start ();
235
+ } catch (Exception e ) {
236
+ e .printStackTrace ();
237
+ }
238
+
239
+
260
240
}
261
241
242
+ public class ReceivePiLocation extends AsyncTask <Void , Void , Boolean > {
243
+
244
+ @ Override
245
+ protected void onPreExecute () {
246
+ isClientRunning = true ;
247
+ }
248
+
249
+ @ Override
250
+ protected Boolean doInBackground (Void ... voids ) {
251
+ byte [] lMsg = new byte [64 ];
252
+ DatagramPacket dp = new DatagramPacket (lMsg , lMsg .length );
253
+ try {
254
+ isClientRunning = true ;
255
+ ds = new DatagramSocket (UDP_LocationReceivingPort );
256
+ ds .setSoTimeout (7000 );
257
+ ds .setReuseAddress (true );
258
+ ds .receive (dp );
259
+ gpsresponse = new String (dp .getData ());
260
+ Log .e ("UDP" , "run: paket alındı!" + gpsresponse );
261
+ } catch (SocketException e ) {
262
+ e .printStackTrace ();
263
+ } catch (IOException e ) {
264
+ e .printStackTrace ();
265
+ } finally {
266
+ if (ds != null ) {
267
+ ds .close ();
268
+ isClientRunning = false ;
269
+ }
270
+ }
271
+ return true ;
272
+ }
273
+
274
+ protected void onPostExecute (Boolean state ) {
275
+ if (state ) {
276
+ setMapView (gpsresponse );
277
+ }
278
+ }
279
+ }
262
280
public class SendTargetAsyncTask extends AsyncTask <Void , Void , Void > {
263
281
Socket socket ;
264
282
265
283
@ Override
266
284
protected Void doInBackground (Void ... params ) {
267
285
try {
268
- InetAddress inetAddress = InetAddress .getByName (wifiModuleIP );
269
- socket = new java .net .Socket (inetAddress , 10200 );
286
+ InetAddress inetAddress = InetAddress .getByName (AutoDriveActivity . wifiModuleIP );
287
+ socket = new java .net .Socket (inetAddress , 10201 );
270
288
PrintStream printStream = new PrintStream (socket .getOutputStream ());
271
- printStream .print (CMD );
289
+ targetLocation = mapLat .getText ().toString () + "/" + mapLon .getText ().toString ();
290
+ printStream .print (targetLocation );
272
291
printStream .close ();
273
292
socket .close ();
274
293
} catch (UnknownHostException e ) {
0 commit comments