Android Open Source - BluetoothGlass Bluetooth Connection Handler






From Project

Back to project page BluetoothGlass.

License

The source code is released under:

GNU General Public License

If you think the Android project BluetoothGlass 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.vicmns.bluetoothglass.server.handlers;
/*  w w  w  .  ja va  2  s .  c  o m*/
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.AsyncTask;
import android.util.Log;

import com.vicmns.bluetoothglass.server.data.BluetoothParametersHolder;

import java.io.IOException;

/**
 * Created by Victor Cervantes on 3/14/14.
 */
public class BluetoothConnectionHandler extends AsyncTask<Void, Void, BluetoothSocket> {
    private static final String TAG = BluetoothConnectionHandler.class.getSimpleName();

    private BluetoothServerSocket mmServerSocket;
    private BluetoothConnectionHandlerCallbacks callbacks;

    public BluetoothConnectionHandler(BluetoothAdapter bluetoothAdapter,
                                      BluetoothConnectionHandlerCallbacks callbacks) {
        try {
            mmServerSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(
                    BluetoothParametersHolder.NAME, BluetoothParametersHolder.uuids[0]);
            this.callbacks = callbacks;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected BluetoothSocket doInBackground(Void... voids) {
        Log.e(TAG, "Bluetooth on listening mode");
        BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned
        while (true) {
            try {
                socket = mmServerSocket.accept();
                if (socket != null)
                    mmServerSocket.close();
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
        return socket;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(BluetoothSocket socket) {
        if(callbacks != null)
            callbacks.onConnectionSuccessful(socket);
        super.onPostExecute(socket);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onCancelled(BluetoothSocket socket) {
        super.onCancelled(socket);
    }

    @Override
    protected void onCancelled() {
        try {
            mmServerSocket.close();
            if(callbacks != null)
                callbacks.onConnectionCancel();
        } catch (IOException e) {
        }
        super.onCancelled();
    }


    public interface BluetoothConnectionHandlerCallbacks {
        public void onConnectionSuccessful(BluetoothSocket socket);
        public void onConnectionCancel();
    }
}




Java Source Code List

com.vicmns.bluetoothglass.client.MainApplication.java
com.vicmns.bluetoothglass.client.activities.BluetoothClient.java
com.vicmns.bluetoothglass.client.activities.MainActivity.java
com.vicmns.bluetoothglass.client.adapters.BluetoothDevicesAdapter.java
com.vicmns.bluetoothglass.client.bluetooth.SendFileToDeviceTask.java
com.vicmns.bluetoothglass.client.callbacks.CardScrollCallBacks.java
com.vicmns.bluetoothglass.client.data.BluetoothParametersHolder.java
com.vicmns.bluetoothglass.client.models.BluetoothDeviceModel.java
com.vicmns.bluetoothglass.client.services.GoogleVoiceTriggerService.java
com.vicmns.bluetoothglass.client.services.SendPictureToDevice.java
com.vicmns.bluetoothglass.client.tools.FileExtensionFilter.java
com.vicmns.bluetoothglass.client.views.CameraView.java
com.vicmns.bluetoothglass.client.views.OverlayView.java
com.vicmns.bluetoothglass.server.MainActivity.java
com.vicmns.bluetoothglass.server.MainApplication.java
com.vicmns.bluetoothglass.server.data.BluetoothParametersHolder.java
com.vicmns.bluetoothglass.server.handlers.BluetoothConnectionHandler.java
com.vicmns.bluetoothglass.server.handlers.BluetoothReadFromSocketHandler.java
com.vicmns.bluetoothglass.server.receivers.DeviceBootReceiver.java
com.vicmns.bluetoothglass.server.service.BluetoothService.java