java - Android - 关闭特定的蓝牙套接字

标签 java android sockets bluetooth connection

我正在尝试使用 Android 设备连接到蓝牙设备以检索一些信息。特别是我尝试通过此 UUID 连接到蓝牙耳机:

"0000111E-0000-1000-8000-00805F9B34FB"

为此,我创建一个套接字并以这种方式将其连接到远程设备:

public ConnectThread(BluetoothDevice device) {
    // Use a temporary object that is later assigned to mmSocket
    // because mmSocket is final.
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothSocket tmp = null;
    mmDevice = device;

    try {
        // Get a BluetoothSocket to connect with the given BluetoothDevice.
        // MY_UUID is the app's UUID string, also used in the server code.
        tmp = device.createRfcommSocketToServiceRecord(UUID_HF);

    } catch (IOException e) {
        Log.e(TAG, "Socket's create() method failed", e);
    }
    mmSocket = tmp;
}

public void run() {
    // Cancel discovery because it otherwise slows down the connection.
    bluetoothAdapter.cancelDiscovery();

    try {
        // Connect to the remote device through the socket. This call blocks
        // until it succeeds or throws an exception.

        mmSocket.connect();
    } catch (IOException connectException) {
        // Unable to connect; close the socket and return.
        try {
            mmSocket.close();
        } catch (IOException closeException) {
            Log.e(TAG, "Could not close the client socket", closeException);
        }
        return;
    }

    // The connection attempt succeeded. Perform work associated with
    // the connection in a separate thread.
    manageMyConnectedSocket(mmSocket);}

当耳机尚未与我的 Android 设备连接时,它可以正常工作。但由于操作系统本身,耳机会自动与我的 Android 设备连接。在这种情况下,当我执行 mmSocket.connect() 方法时,它不会返回。我想也许 Android 已经自动连接了另一个具有相同 UUID 的套接字,所以我的不起作用。你认为这是问题所在吗?如果是的话,有没有办法关闭我的 Android 设备和远程蓝牙设备之间的所有套接字?或者也许只是困扰我的过程的一个? 提前致谢。

最佳答案

实际发生的情况是操作系统正在执行配对设备标准以节省一些电池,因为搜索过程会消耗大量能量。 既然您已经完成了搜索,您应该在配对设备中进行搜索,而不是正常搜索,并且搜索结果应取自 查询配对设备

在执行设备发现之前,值得查询配对设备集以查看所需设备是否已知。为此,请调用 getBondedDevices()。这将返回一组表示配对设备的 BluetoothDevice 对象。例如,您可以查询所有配对的设备并获取每个设备的名称和 MAC 地址,如以下代码 fragment 所示:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
    String deviceName = device.getName();
    String deviceHardwareAddress = device.getAddress(); // MAC address
    }
}

要启动与蓝牙设备的连接,关联的 BluetoothDevice 对象只需要 MAC 地址,您可以通过调用 getAddress() 来检索该地址。您可以在有关连接设备的部分中了解有关创建连接的更多信息。

这是来自谷歌的官方文档,涵盖了有关蓝牙的所有细节: https://developer.android.com/guide/topics/connectivity/bluetooth

关于java - Android - 关闭特定的蓝牙套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52139666/

相关文章:

java - 如何创建具有多个静态库的 JNI Android Studio 项目

java - 关于 XML 模式的 Json 到 XML 到 Json

Android:在 ListView 项目上显示实时时间戳

android - 如何在 android 中每 5 秒更新一次进度对话框文本?

java - TCP 消息中的垃圾 - C++/Java 通信

linux - 如何告诉另一个线程一个线程在 recv() 调用中*现在*

java - 使用 Hibernate 说明表列属性

java - Wildfly module.xml 一个java类的配置

android - 刷新我的 fragment 没有像我想象的那样工作

windows - 在 Windows XP 中基于每个套接字启用 RFC1323?