// Path: btutillib/src/main/java/com/devpaul/bluetoothutillib/handlers/BluetoothHandler.java
// public class BluetoothHandler extends Handler {
//     public static final int MESSAGE_READ = 121;
//     public static final int MESSAGE_WAIT_FOR_CONNECTION = 143;
//     public static final int MESSAGE_CONNECTION_MADE = 155;
//     public static final int MESSAGE_A2DP_PROXY_RECEIVED = 157;
//     public SimpleBluetoothListener mListener;
//     public Activity mActivity;
//     public ProgressDialog dialog;
//     public boolean shouldShowSnackbars;
// 
//     /**
//      * Bluetooth Handler class for handling messages from the bluetooth device.
//      * @param listener listener for simple bluetooth.
//      * @param context reference context.
//      */
//     public BluetoothHandler(SimpleBluetoothListener listener, Context context) {
//         this.mListener = listener;
//         if(context instanceof Activity) {
//             this.mActivity = (Activity) context;
//         }
//         dialog = new ProgressDialog(context);
//     }
// 
//     public void setShowSnackbars(boolean show) {
//         this.shouldShowSnackbars = show;
//     }
// }


import android.app.Activity;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.design.widget.Snackbar;
import android.util.Log;
import com.devpaul.bluetoothutillib.errordialogs.InvalidMacAddressDialog;
import com.devpaul.bluetoothutillib.handlers.BluetoothHandler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
package com.devpaul.bluetoothutillib.utils;




/**
 * Created by Paul Tsouchlos
 * Class that handles all bluetooth adapter stuff and connecting to devices, establishing sockets
 * listening for connections and reading/writing data on the sockets.
 */
public class BluetoothUtility implements BluetoothProfile.ServiceListener {

    /**
     * Debug tag
     */
    private static final String TAG = "BluetoothUtility";

    /**
     * {@code BluetoothAdapter} that handles bluetooth methods.
     */
    private BluetoothAdapter bluetoothAdapter;

    /**
     * Context field.
     */
    private Context mContext;

    /**
     * Current {@code BluetoothSocket}
     */
    private BluetoothSocket bluetoothSocket;

    /**
     * Current {@code BluetoothDevice}
     */
    private BluetoothDevice bluetoothDevice;

    /**
     * {@code UUID} for a normal device connection.
     */
    private static final UUID NORMAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

    /**
     * {@code UUID} for a server device connection.
     */
    private static final UUID SERVER_UUID = UUID.fromString("03107005-0000-4000-8000-00805F9B34FB");

    /**
     * Name of the bluetooth server.
     */
    private static final String SERVER_NAME = "bluetoothServer";

    /*
    Threads that handle all the connections and accepting of connections and what happens after
    you're connected.
     */

    /**
     * {@code Thread} that handles a connected socket.
     */
    private ConnectedThread connectedThread;

    /**
     * {@code Thread} that handles connecting to a device.
     */
    private ConnectDeviceThread connectThread;

    /**
     * {@code Thread} that listens for an incoming connection.
     */
    private AcceptThread acceptThread;

    /**
     * {@code Thread} that connects a device to an already set up bluetooth server.
     */
    private ConnectDeviceToServerThread connectToServerThread;

    /**
     * {@code BluetoothHandler} that handles all the calls for a reading incoming data and other
     * messages.
     */
// Path: btutillib/src/main/java/com/devpaul/bluetoothutillib/handlers/BluetoothHandler.java
// public class BluetoothHandler extends Handler {
//     public static final int MESSAGE_READ = 121;
//     public static final int MESSAGE_WAIT_FOR_CONNECTION = 143;
//     public static final int MESSAGE_CONNECTION_MADE = 155;
//     public static final int MESSAGE_A2DP_PROXY_RECEIVED = 157;
//     public SimpleBluetoothListener mListener;
//     public Activity mActivity;
//     public ProgressDialog dialog;
//     public boolean shouldShowSnackbars;
// 
//     /**
//      * Bluetooth Handler class for handling messages from the bluetooth device.
//      * @param listener listener for simple bluetooth.
//      * @param context reference context.
//      */
//     public BluetoothHandler(SimpleBluetoothListener listener, Context context) {
//         this.mListener = listener;
//         if(context instanceof Activity) {
//             this.mActivity = (Activity) context;
//         }
//         dialog = new ProgressDialog(context);
//     }
// 
//     public void setShowSnackbars(boolean show) {
//         this.shouldShowSnackbars = show;
//     }
// }


// Path: btutillib/src/main/java/com/devpaul/bluetoothutillib/utils/BluetoothUtility.java
import android.app.Activity;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.design.widget.Snackbar;
import android.util.Log;
import com.devpaul.bluetoothutillib.errordialogs.InvalidMacAddressDialog;
import com.devpaul.bluetoothutillib.handlers.BluetoothHandler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

package com.devpaul.bluetoothutillib.utils;




/**
 * Created by Paul Tsouchlos
 * Class that handles all bluetooth adapter stuff and connecting to devices, establishing sockets
 * listening for connections and reading/writing data on the sockets.
 */
public class BluetoothUtility implements BluetoothProfile.ServiceListener {

    /**
     * Debug tag
     */
    private static final String TAG = "BluetoothUtility";

    /**
     * {@code BluetoothAdapter} that handles bluetooth methods.
     */
    private BluetoothAdapter bluetoothAdapter;

    /**
     * Context field.
     */
    private Context mContext;

    /**
     * Current {@code BluetoothSocket}
     */
    private BluetoothSocket bluetoothSocket;

    /**
     * Current {@code BluetoothDevice}
     */
    private BluetoothDevice bluetoothDevice;

    /**
     * {@code UUID} for a normal device connection.
     */
    private static final UUID NORMAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

    /**
     * {@code UUID} for a server device connection.
     */
    private static final UUID SERVER_UUID = UUID.fromString("03107005-0000-4000-8000-00805F9B34FB");

    /**
     * Name of the bluetooth server.
     */
    private static final String SERVER_NAME = "bluetoothServer";

    /*
    Threads that handle all the connections and accepting of connections and what happens after
    you're connected.
     */

    /**
     * {@code Thread} that handles a connected socket.
     */
    private ConnectedThread connectedThread;

    /**
     * {@code Thread} that handles connecting to a device.
     */
    private ConnectDeviceThread connectThread;

    /**
     * {@code Thread} that listens for an incoming connection.
     */
    private AcceptThread acceptThread;

    /**
     * {@code Thread} that connects a device to an already set up bluetooth server.
     */
    private ConnectDeviceToServerThread connectToServerThread;

    /**
     * {@code BluetoothHandler} that handles all the calls for a reading incoming data and other
     * messages.
     */
    private BluetoothHandler bluetoothHandler;