如何在Android中断开已配对的蓝牙耳机但不取消配对。

huangapple go评论65阅读模式
英文:

How to disconnect but not unpair an already paired bluetooth headphone in Android

问题

我将为您翻译代码中的注释和文本部分:

// 我将`HeadphoneConnectThread`传递给蓝牙设备,即从以下代码中获取的SONY WH-1000XM3:
// Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
// `HeadphoneConnectThread`类尝试与蓝牙耳机建立连接。
// 连接部分正常工作,即已连接到配对的耳机并可以将数据流传输到它。
// 要断开连接,我调用`close()`,该方法更新`BluetoothSocket`的状态(如下所示),但耳机未断开连接。
// socket.isConnected()=false, headphone.getBondState()=12
// Bond状态为12:
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice#BOND_BONDED
// HeadphoneConnectThread类的定义
public class HeadphoneConnectThread extends Thread {

    public static final UUID uuid = UUID.fromString("96cc203e-5068-46ad-b32d-e316f5e069ba");

    private final BluetoothDevice headphone;
    private BluetoothSocket socket;

    public HeadphoneConnectThread(BluetoothDevice headphone) {
        BluetoothSocket tmp = null;
        this.headphone = headphone;

        try {
            tmp = headphone.createRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {
            Log.e(TAG, "Socket's create() method failed", e);
        }
        socket = tmp;
    }

    public void run() {
        try {
            Log.d(TAG, "Connecting to " +  headphone.getName() + ", address=" + headphone.getAddress());
            if (!socket.isConnected()) {
                socket.connect();
                Log.d(TAG, "Connected");
            } else {
                Log.d(TAG, "Headphone already connected");
            }

            Thread.sleep(3000);

            if (socket.isConnected()) {
                disconnectHeadphone();
            }
        } catch (IOException e) {
            ...
        }
    }

    public void disconnectHeadphone() {
        try {
            Log.d(TAG,"Disconnecting " +  headphone.getName() + ", address=" + headphone.getAddress());
            socket.getOutputStream().close();
            socket.getInputStream().close();
            socket.close();
            Log.d(TAG, "Disconnected, socket.isConnected()=" + socket.isConnected() +
                    ", headphone.getBondState()=" + headphone.getBondState());
            socket = null;
        } catch (IOException e) {
            Log.e(TAG, "Error occurred while disconnecting", e);
        }
    }
}

请注意,这些翻译仅包括代码中的注释和文本部分,不包括代码本身。如果您需要进一步的帮助,请随时提出。

英文:

I pass HeadphoneConnectThread the bluetooth device i.e. SONY WH-1000XM3 that I get from: Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();.

The HeadphoneConnectThread class attempts to establish a connection with the Bluetooth headphones.

The connection part is working fine i.e. the paired headphones are connected and data can be streamed to it.

To disconnect, I call close() which updates the state of the BluetoothSocket (see below) however, headphones are not disconnected.

socket.isConnected()=false, headphone.getBondState()=12

Bond state 12:

https://developer.android.com/reference/android/bluetooth/BluetoothDevice#BOND_BONDED


public class HeadphoneConnectThread extends Thread {
public static final UUID uuid = UUID.fromString("96cc203e-5068-46ad-b32d-e316f5e069ba");
private final BluetoothDevice headphone;
private BluetoothSocket socket;
public HeadphoneConnectThread(BluetoothDevice headphone) {
BluetoothSocket tmp = null;
this.headphone = headphone;
try {
tmp = headphone.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
Log.e(TAG, "Socket's create() method failed", e);
}
socket = tmp;
}
public void run() {
try {
Log.d(TAG, "Connecting to " +  headphone.getName() + ", address=" + headphone.getAddress());
if (!socket.isConnected()) {
socket.connect();
Log.d(TAG, "Connected");
} else {
Log.d(TAG, "Headphone already connected");
}
Thread.sleep(3000);
if (socket.isConnected()) {
disconnectHeadphone();
}
} catch (IOException e) {
...
}
}
public void disconnectHeadphone() {
try {
Log.d(TAG,"Disconnecting " +  headphone.getName() + ", address=" + headphone.getAddress());
socket.getOutputStream().close();
socket.getInputStream().close();
socket.close();
Log.d(TAG, "Disconnected, socket.isConnected()=" + socket.isConnected() +
", headphone.getBondState()=" + headphone.getBondState());
socket = null;
} catch (IOException e) {
Log.e(TAG, "Error occurred while disconnecting", e);
}
}

答案1

得分: 1

从您提供的文档中:

您提供的文档中提到:

与远程设备配对(配对)并不一定意味着该设备目前处于连接状态。这只是意味着挂起的过程在较早的时间完成,并且链接密钥仍然存储在本地,可以在下一次连接时使用。

因此,如果您的代码输出 socket.isConnected = falsebondedState=12,这只是表示设备已配对但未连接。

您的代码没有错误。

英文:

From the docs you've linked:

Being bonded (paired) with a remote device does not necessarily mean the device is currently connected. It just means that the pending procedure was completed at some earlier time, and the link key is still stored locally, ready to use on the next connection.

So if your code outputs socket.isConnected = false and bondedState=12, it just means it's paired and disconnected.

There is no error in your code.

huangapple
  • 本文由 发表于 2020年8月18日 02:53:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63456984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定