package it.andoma.legocarcontroller;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.AsyncTask;
import android.util.Log;

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

/**
 * Created by mling_000 on 11/09/2016.
 */
public class ConnectionTask extends AsyncTask<Void, Void, Void> {

    private BluetoothSocket btS = null;

    public ConnectionTask(BluetoothDevice server, UUID uuid) {
        try {
            btS = server.createRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {}
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            btS.connect();
        } catch (IOException e) {
            try {
                btS.close();
            } catch (IOException e1) {}

            return null;
        }

        Log.d("legocar", "connection accepted");

        ConnectionManager cm = ConnectionManager.getInstance();
        cm.setChannel(btS);
        cm.start();

        return null;
    }
}
