Android Open Source - GeoCarDroid Connect Thread






From Project

Back to project page GeoCarDroid.

License

The source code is released under:

GNU General Public License

If you think the Android project GeoCarDroid 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.geocar.bluetooth;
/*from  w w  w  .jav a2s .  c o m*/
import java.io.IOException;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

public class ConnectThread extends Thread {
    private BluetoothSocket mmSocket;
    private BluetoothDevice mmDevice;
    private SocketHandler mHandler;
    
    public ConnectThread(BluetoothDevice device, String uuid, SocketHandler handler ) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        //BluetoothSocket tmp = null;
        mmDevice = device;
        mHandler = handler;
        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // 
          mmSocket = mmDevice.createRfcommSocketToServiceRecord(UUID.fromString(uuid));
        } catch (IOException e) {
          mmSocket = null;
        }
    }
 
    public void run() {
      BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        // Cancel discovery because it will slow down the connection
      adapter.cancelDiscovery();
 
        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
            
            onConnected(mmSocket);
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
                mmSocket = null;
            } catch (IOException closeException) { }
            return;
        }
 
       
    }
 
    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
            mmSocket = null;
        } catch (IOException e) { }
    }
    
    protected void onConnected(BluetoothSocket socket){
       // Do work to manage the connection (in a separate thread)
        mHandler.onBthConnect(socket);
    }
}




Java Source Code List

com.geocar.bluetooth.AlertType.java
com.geocar.bluetooth.BthDeviceManager.java
com.geocar.bluetooth.ConnectThread.java
com.geocar.bluetooth.ConnectedThread.java
com.geocar.bluetooth.ConnectionHandler.java
com.geocar.bluetooth.GeoSendDataThread.java
com.geocar.bluetooth.SocketHandler.java
com.geocar.geocarconnect.FileInfo.java
com.geocar.geocarconnect.FileManager.java
com.geocar.geocarconnect.GeoCarAlert.java
com.geocar.geocarconnect.MainActivity.java
com.geocar.geocarconnect.PrefActivity.java
com.geocar.geocarconnect.Settings.java
com.geocar.geocarconnect.StartMenu.java
com.geocar.sms.SmsListener.java