仅在配对后通过蓝牙连接到Java应用程序服务器的Android应用程序

时间:2014-08-07 18:08:05

标签: java android bluetooth uuid bluecove

每个人都过得愉快!所以我有这个Android应用程序作为客户端,并尝试连接到我的PC上的一个Java应用程序,其中有一个蓝牙服务器组件。

我面临的问题是只有在我的Nexus 5 的配对设备列表中没有我的电脑时才建立连接。换句话说,它们仅在配对时才连接。因此,每次我想要连接或连接失败时,我都被迫从配对设备列表中删除我的电脑。

我已将此Simple Android and Java Bluetooth 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;
            }
        }
    }
}

并使用来自android示例项目的BluetoothChat作为客户端的基础:

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;
    }
}

我觉得这可能是因为我正在使用的UUID。对我有用的UUID如下:

  1. 04c6032b00004000800000805f9b34fc
  2. 0000110500001000800000805f9b34fb
  3. 我在两个应用程序中都使用类似的UUID。什么可能导致这个问题的想法?

1 个答案:

答案 0 :(得分:0)

奇怪的是,今天当我使用蓝牙适配器而不是我自己的笔记本电脑的蓝牙时,问题得到了解决。因此,这不是代表软件组件的问题。