1

今天是个好日子!所以我有这个 android 应用程序,它充当客户端并尝试连接到我的 PC 上的一个 Java 应用程序,其中有一个蓝牙服务器组件。

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

我已将这个简单的 Android 和 Java 蓝牙应用程序用作服务器端应用程序的基础:

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

我在两个应用程序中都使用了类似的 UUID。任何想法可能导致此问题?

4

1 回答 1

0

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

于 2014-08-09T18:53:46.963 回答