package com.yc.nmeacli; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import java.util.StringTokenizer; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private Button btCnx; private Button btSend; private Button btClose; private int port; private byte[] send_data = new byte[1024]; private byte[] receiveData = new byte[1024]; private String receiveSentence; private String sendSentence=null; private DatagramSocket client_socket; private InetAddress ipAddress; private Socket socket; private boolean reception; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btCnx = (Button) findViewById(R.id.btcnx); btCnx.setOnClickListener(this); btSend = (Button) findViewById(R.id.btsend); btSend.setOnClickListener(this); btClose = (Button) findViewById(R.id.btclose); btClose.setOnClickListener(this); } private class ecouteNmea extends AsyncTask { @Override protected void onPreExecute() { super.onPreExecute(); reception=true; Toast.makeText(getApplicationContext(), "Début du traitement asynchrone ecouteNmea", Toast.LENGTH_LONG).show(); } @Override protected void onProgressUpdate(String... values){ super.onProgressUpdate(values); ((TextView) findViewById(R.id.lognmea)).setText(receiveSentence); //((TextView) findViewById(R.id.compteur)).setText(String.valueOf(values[0])); //mProgressBar.setProgress(values[0]); } @Override protected Void doInBackground(Void... arg0) { while (reception) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); try { synchronized (receivePacket) { client_socket.receive(receivePacket); receiveSentence = new String(receivePacket.getData()); } publishProgress(receiveSentence); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!reception) { break; } } return null; } @Override protected void onPostExecute(Void result) { client_socket.close(); Toast.makeText(getApplicationContext(), "Le traitement asynchrone ecouteNmea est terminé", Toast.LENGTH_LONG).show(); } } @Override public void onClick(View v) { if (v.getId() == R.id.btcnx) { String ip_adr = ((EditText) findViewById(R.id.adr_ip)).getText().toString(); port = Integer.parseInt( ((EditText) findViewById(R.id.port)).getText().toString()); try { client_socket = new DatagramSocket(port); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { ipAddress = InetAddress.getByName(ip_adr); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } new ecouteNmea().execute(); } if (v.getId() == R.id.btsend) { // exemple initial sentence = GPGLL,5300.97914,N,00259.98174,E,125926,A // final sentence to sendd with check= $GPGLL,5300.97914,N,00259.98174,E,125926,A*28 String sendSentenceNoCheck = ((EditText) findViewById(R.id.nmea_sentence)).getText().toString(); sendSentence="$"+sendSentenceNoCheck+"*"+getCheckSum(sendSentenceNoCheck); ((EditText) findViewById(R.id.nmea_sentence)).setText(sendSentence); send_data = sendSentence.getBytes(); DatagramPacket send_packet = new DatagramPacket(send_data,sendSentence.length(), ipAddress, port); try { client_socket.send(send_packet); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (v.getId() == R.id.btclose) { reception = false; } } private String getCheckSum(String sentence) { int checkSum=0; for(int i = 0; i < sentence.length(); i++) { checkSum = checkSum ^ (int)sentence.charAt(i); } return Integer.toHexString(checkSum); } }