Android Open Source - GeoCarDroid Connected Thread






From Project

Back to project page GeoCarDroid.

License

The source code is released under:

GNU General Public License

If you think the Android project GeoCarDroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.geocar.bluetooth;
/*ww w .j a  va2  s . c o m*/
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import android.bluetooth.BluetoothSocket;

public class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private DataInputStream mDataInputStream;
    private InputStreamReader mmInStreamReader;
    private BufferedReader mBufferedReader;
    private final OutputStream mmOutStream;
    private ConnectionHandler mhandler;
    
    public final int MESSAGE_READ = 4;
 
    public ConnectedThread(BluetoothSocket socket, ConnectionHandler handler ) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null; 
        mhandler = handler;
        
        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }
        
        mmInStream = tmpIn;
        mmOutStream = tmpOut;
        if( mmInStream !=null ){
          try {
            mDataInputStream = new DataInputStream(mmInStream);
            mmInStreamReader = new InputStreamReader( mDataInputStream, "UTF-8" );
        mBufferedReader = new BufferedReader( mmInStreamReader );
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        mBufferedReader = null;
        mmInStreamReader = null;
        mDataInputStream = null;
      }
        } 
    }
 
    private void mySleep(long mil ){
      try {
      Thread.sleep(mil);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    }
    public void run() {
      byte[] buffer = new byte[128];  // buffer store for the stream
        int bytes; // bytes returned from read()
        int endOfLineIndex;
        StringBuilder sb = new StringBuilder();
        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
              if( mmInStream.available() >0 ){ 
                bytes = mmInStream.read(buffer);
                  byte[] readBuf = (byte[]) buffer;
                  String strIncom = new String(readBuf, 0, bytes, "UTF-8");                 // create string from bytes array
                  //Remove null characters that get filter (maybe because of some delays)
                  strIncom = strIncom.replace(String.valueOf(((char)0)), "" );
                  sb.append(strIncom);// append string                  
                  endOfLineIndex = sb.indexOf("\n");  // determine the end-of-line
                  if (endOfLineIndex > 0){
                      // add the current string to EOL to a local string
                      String sbprint = sb.substring(0, endOfLineIndex);
                      mhandler.onDataReceived( sbprint );
                      sb = new StringBuilder( sb.substring(endOfLineIndex+1) );
                  }
              }
            }catch (IOException e) {
                break;
            }
        }
        if( mhandler!=null ){
          mhandler.onDisconnect();  
        }        
    }
 
    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) { }
    }
 
    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}




Java Source Code List

com.geocar.bluetooth.AlertType.java
com.geocar.bluetooth.BthDeviceManager.java
com.geocar.bluetooth.ConnectThread.java
com.geocar.bluetooth.ConnectedThread.java
com.geocar.bluetooth.ConnectionHandler.java
com.geocar.bluetooth.GeoSendDataThread.java
com.geocar.bluetooth.SocketHandler.java
com.geocar.geocarconnect.FileInfo.java
com.geocar.geocarconnect.FileManager.java
com.geocar.geocarconnect.GeoCarAlert.java
com.geocar.geocarconnect.MainActivity.java
com.geocar.geocarconnect.PrefActivity.java
com.geocar.geocarconnect.Settings.java
com.geocar.geocarconnect.StartMenu.java
com.geocar.sms.SmsListener.java