Android Open Source - GlassToPhoneBluetooth Bluetooth Connection Thread






From Project

Back to project page GlassToPhoneBluetooth.

License

The source code is released under:

MIT License

If you think the Android project GlassToPhoneBluetooth 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 ktlab.lib.connection.bluetooth;
/*from  w w w  . ja v  a  2 s  . c  o  m*/
import java.io.IOException;

import ktlab.lib.connection.ConnectionThread;

import android.bluetooth.BluetoothSocket;
import android.os.Message;
import android.util.Log;

public abstract class BluetoothConnectionThread extends ConnectionThread {

    private static final String TAG = "BluetoothConnection";

    protected BluetoothSocket mSocket;

    protected BluetoothConnectionThread(Message msg) {
        super(msg);
    }

    /**
     * get connection
     */
    public void run() {
        Log.i(TAG, "start connecting");

        getSocket();

        if (mSocket == null) {
            Log.e(TAG, "Failed to connect");
            mMessage.what = BluetoothConnection.EVENT_CONNECTION_FAIL;
            mMessage.sendToTarget();
            return;
        }

        boolean result = setupConnection();

        if (!result) {
            Log.e(TAG, "Failed to connect");
            mMessage.what = BluetoothConnection.EVENT_CONNECTION_FAIL;
            mMessage.sendToTarget();
            return;
        }

        mMessage.sendToTarget();
    }

    @Override
    public boolean close() {
        boolean ret = super.close();

        if (mSocket != null) {
            try {
                mSocket.close();
            } catch (IOException e) {
                ret = false;
            }
        }
        return ret;
    }

    private boolean setupConnection() {
        if (mSocket == null) {
            return false;
        }

        try {
            mInput = mSocket.getInputStream();
            mOutput = mSocket.getOutputStream();
        } catch (IOException e) {
            return false;
        }

        return true;
    }

    abstract protected void getSocket();
}




Java Source Code List

com.space150.android.glass.camerazoom.DeviceSelectActivity.java
com.space150.android.glass.camerazoom.MainActivity.java
com.zlglassworks.glassbluetoothtest.DeviceSelectDialogFragment.java
com.zlglassworks.glassbluetoothtest.MainActivity.java
ktlab.lib.connection.CommandReceiveThread.java
ktlab.lib.connection.CommandReceiveThread.java
ktlab.lib.connection.CommandSendThread.java
ktlab.lib.connection.CommandSendThread.java
ktlab.lib.connection.ConnectionCallback.java
ktlab.lib.connection.ConnectionCallback.java
ktlab.lib.connection.ConnectionCommand.java
ktlab.lib.connection.ConnectionCommand.java
ktlab.lib.connection.ConnectionThread.java
ktlab.lib.connection.ConnectionThread.java
ktlab.lib.connection.Connection.java
ktlab.lib.connection.Connection.java
ktlab.lib.connection.bluetooth.BluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.BluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.BluetoothConnection.java
ktlab.lib.connection.bluetooth.BluetoothConnection.java
ktlab.lib.connection.bluetooth.ClientBluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.ClientBluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.ClientBluetoothConnection.java
ktlab.lib.connection.bluetooth.ClientBluetoothConnection.java
ktlab.lib.connection.bluetooth.ServerBluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.ServerBluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.ServerBluetoothConnection.java
ktlab.lib.connection.bluetooth.ServerBluetoothConnection.java