javaandroidbluetoothuuidbluecove

Android app connecting to java application server through bluetooth only after pairing


Good day every one! So I have this android app which acts as a client and tries to connect to a java application on my PC that has a Bluetooth server component in it.

The issue I'm facing is that the connection only establishes when I don't have my PC in the paired devices list of my Nexus 5. In other words they only connect when they are paired. Therefore I've been forced to remove My PC from the list of Paired devices every time that I want to have a connection or else the connection fails.

I have used this Simple Android and Java Bluetooth Application for the base of the server side application:

import java.io.IOException;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

public class WaitThread implements Runnable{

    public WaitThread() { }

    @Override
    public void run() {
        waitForConnection();        
    }

    /** Waiting for connection from devices */
    private void waitForConnection() {
        // retrieve the local Bluetooth device object
        LocalDevice local = null;

        StreamConnectionNotifier notifier;
        StreamConnection connection = null;

        // setup the server to listen for connection
        try {
            local = LocalDevice.getLocalDevice();
            local.setDiscoverable(DiscoveryAgent.GIAC);

            UUID uuid = new UUID("04c6032b00004000800000805f9b34fc", false);

            System.out.println(uuid.toString());

            String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
            notifier = (StreamConnectionNotifier) Connector.open(url);

        } catch (BluetoothStateException e) {
            System.out.println("Bluetooth is not turned on.");
            e.printStackTrace();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        // waiting for connection
        while(true) {
            try {
                System.out.println("waiting for connection...");

                connection = notifier.acceptAndOpen();
                Thread processThread = new Thread(new ProcessConnectionThread(connection));
                processThread.start();

            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
    }
}

and used the BluetoothChat from android sample projects for base of the client side:

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

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    Handler mHandler;
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    private static final UUID MY_UUID = UUID
            .fromString("04c6032b-0000-4000-8000-00805f9b34fc");

    public ConnectThread(BluetoothDevice device, Handler handler) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmpSocket = null;
        mmDevice = device;
        mHandler = handler;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            tmpSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Message msgException = new Message();
            msgException.setTarget(mHandler);
            msgException.what = Constants.DISCONNECTED_HANDLER;
            msgException.sendToTarget();
        }
        mmSocket = tmpSocket;

    }

    public void run() {
        // Cancel discovery because it will slow down the connection
        mBluetoothAdapter.cancelDiscovery();

        Message msg1 = new Message(); // handler. we use it to know when de
                                        // device has been connected or
                                        // disconnected in the UI activity
        msg1.setTarget(mHandler);

        msg1.what = Constants.CONNECTING_HANDLER;
        msg1.sendToTarget();

        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();

            // handler
            Message msg2 = new Message();
            msg2.setTarget(mHandler);
            msg2.what = Constants.CONNECTED_HANDLER;
            msg2.setTarget(mHandler);
            msg2.sendToTarget();

        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            System.out.println("alex - NO connected");
            Message msgException = new Message();
            msgException.setTarget(mHandler);
            msgException.what = Constants.DISCONNECTED_HANDLER;
            msgException.sendToTarget();

            try {
                mmSocket.close();
            } catch (IOException closeException) {
            }
            return;
        }
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
        }
    }

    public BluetoothSocket getBTSocket() {
        return mmSocket;
    }
}

I have a feeling this might be because of the UUID that I'm using. The UUIDs that worked for me were the followings:

  1. 04c6032b00004000800000805f9b34fc
  2. 0000110500001000800000805f9b34fb

I am using the similar UUIDs in both applications. Any Idea what might be causing this issue?


Solution

  • Strangely enough, today when I used a Bluetooth dongle instead of my own laptop's Bluetooth the problem got resolved. Therefore It was not an issue on behalf of the software component.