package com.freak.fidji.locationoverbt;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Parcel;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by GBeguin on 27/11/2014.
 */
public class SendingThread extends Thread {

    private static final  String TAG = "SENDING_THREAD";
    private static final UUID mUuid = UUID.fromString("4364cf1a-7621-11e4-b116-123b93f75cba");
    private final BluetoothDevice mDevice;
    private final Context mContext;
    private BluetoothSocket mSocket;
    private boolean mRunning;
    private SendingThreadListener mListener;

    public SendingThread(Context context, BluetoothDevice device) {
        Log.i(TAG, "Create thread for device " + device.getAddress());
        mContext = context;
        mDevice = device;
        mRunning = false;
        mListener = null;


        Log.i(TAG, "Create socket for device " + mDevice.getAddress());        BluetoothSocket tmp = null;
        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(mUuid);
        } catch (IOException e) { }
        mSocket = tmp;
    }

    @Override
    public synchronized void start() {
        Log.i(TAG, "Start thread for device " + mDevice.getAddress());
        mRunning = true;
        super.start();
    }

    @Override
    public void run() {
        LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

        while (mRunning = true) {
            Log.i(TAG, "Retrieve location " + mDevice.getAddress());
            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (location != null) {
                Log.i(TAG, "Convert location to byte array " + mDevice.getAddress());
                // https://gist.github.com/jacklt/6711967
                Parcel parcel = Parcel.obtain();
                location.writeToParcel(parcel, 0);
                byte[] bytes = parcel.marshall();

                Log.i(TAG, "Connect to socket " + mDevice.getAddress());
                try {
                    mSocket.connect();
                } catch (IOException e) {
                    Log.i(TAG, "Error while connecting for device " + mDevice.getAddress());
                    e.printStackTrace();
                }

                Log.i(TAG, "Send location " + mDevice.getAddress());
                try {
                    DataOutputStream dOut = new DataOutputStream(mSocket.getOutputStream());
                    dOut.write(bytes, 0, bytes.length);
                    dOut.flush();
                    dOut.close();
                } catch (IOException e) {
                    Log.i(TAG, "Error while sending datas for device " + mDevice.getAddress());
                    e.printStackTrace();
                }

                try {
                    Log.i(TAG, "Close socket for device " + mDevice.getAddress());
                    mSocket.close();
                } catch (IOException e) {
                    Log.i(TAG, "Error while closing socket " + mDevice.getAddress());
                    e.printStackTrace();
                }

            }

            try {
                Log.i(TAG, "Sleep 10s for device " + mDevice.getAddress());
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        Log.i(TAG, "Thread stopped for device " + mDevice.getAddress());
        if (mListener != null) {
            mListener.onThreadFinished(mDevice);
        }
    }

    public void disconnect() {
        Log.i(TAG, "Stop thread for device " + mDevice.getAddress());
        mRunning = false;
    }

    public void setListener(SendingThreadListener listener) {
        mListener = listener;
    }
}

@@ -1663,24 +1663,59 @@
 oid run() %7B%0A
+        boolean connected = false;%0A
         Loca
@@ -1837,24 +1837,25 @@
  (mRunning =
+=
  true) %7B%0A   
@@ -2482,24 +2482,62 @@
 .connect();%0A
+                    connected = true;%0A
             
@@ -2651,32 +2651,71 @@
 .getAddress());%0A
+                    connected = false;%0A
                 
@@ -2750,32 +2750,69 @@
              %7D%0A%0A
+                if (connected) %7B%0A    
                 
@@ -2872,38 +2872,46 @@
                 
+    
 try %7B%0A
+    
                 
@@ -3003,24 +3003,28 @@
             
+ 
+   
 dOut.write(b
@@ -3063,24 +3063,28 @@
             
+   
+ 
 dOut.flush()
@@ -3081,24 +3081,28 @@
 ut.flush();%0A
+    
             
@@ -3131,32 +3131,36 @@
                 
+ 
+   
 %7D catch (IOExcep
@@ -3161,32 +3161,36 @@
 OException e) %7B%0A
+    
                 
@@ -3261,32 +3261,36 @@
 .getAddress());%0A
+    
                 
@@ -3306,32 +3306,54 @@
 ntStackTrace();%0A
+                    %7D%0A
                 

Fix issues in while loop - unstoppable thread - send datas only if connection succesful