Android close() 和 disconnect() 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23110295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:47:28  来源:igfitidea点击:

Difference between close() and disconnect()?

androidbluetoothbluetooth-lowenergy

提问by Arturs Vancans

Android Bluetooth Low Energy API implements 1 method to connect to the device connectGatt()but 2 methods to close the connection disconnect()and close().

Android 低功耗蓝牙 API 实现了 1 种连接设备的connectGatt()方法,但实现了 2 种关闭连接的方法disconnect()close().

Documentation says:

文档说:

  • disconnect(): Disconnects an established connection, or cancels a connection attempt currently in progress.

  • close(): Application should call this method as early as possible after it is done with this GATT client.

  • disconnect():断开已建立的连接,或取消当前正在进行的连接尝试。

  • close():应用程序应在此 GATT 客户端完成后尽早调用此方法。

The source code of BluetoothGatt.javashows that close() unregisters the application and disconnect() disconnect the client. However it does not say what that actually means. I mean, if there is only 1 way to connect to the client, why there are 2 ways to close/disconnect the connection?

BluetoothGatt.java的源代码显示 close() 取消注册应用程序,并且 disconnect() 断开客户端。然而,它并没有说明这实际上意味着什么。我的意思是,如果只有 1 种方法可以连接到客户端,为什么有 2 种方法可以关闭/断开连接?

回答by Douglas Jones

With disconnect()you can later call connect()and continue with that cycle.

有了disconnect()你以后可以打电话connect(),并继续与周期。

Once you call close()you are done. If you want to connect again you will have to call connectGatt()on the BluetoothDeviceagain; close()will release any resources held by BluetoothGatt.

一旦你打电话close()就完成了。如果您想再次连接,你将不得不调用connectGatt()BluetoothDevice再次; close()将释放BluetoothGatt.持有的任何资源。

回答by Droid Chris

Here is some food for thought:

这里有一些值得思考的食物:

As long as you have not called close on the Gatt, you can still try to connect to it, or discover. So when I try to discover services for a machine, I will usually run a thread or runnable that makes the request to connect to the machine for a certain period of time.

只要您还没有在 Gatt 上调用 close,您仍然可以尝试连接到它,或者发现它。因此,当我尝试为机器发现服务时,我通常会运行一个线程或可运行对象,以请求连接到机器一段时间。

The first attempt with a machine connection, will return a BluetoothGatt object that you can later use to try to discover the services for the BluetoothDevice object. It seems pretty easy to connect, but much harder to discover the machine servces.

与机器连接的第一次尝试将返回一个 BluetoothGatt 对象,您可以稍后使用它来尝试发现 BluetoothDevice 对象的服务。连接似乎很容易,但发现机器服务要困难得多。

mBluetoothGatt = machine.getDevice().connectGatt(this, false, mGattCallback);

So in my thread / runnable, I will check to see if the BluetoothGatt is null. If it is, I will call the above line of code again, else I will attempt to discover the BluetoothGatt services as such.

所以在我的线程/runnable 中,我将检查 BluetoothGatt 是否为空。如果是,我将再次调用上面的代码行,否则我将尝试发现 BluetoothGatt 服务。

mBluetoothGatt.discoverServices();

Oh, and I ALWAYS make sure to call BluetoothAdapter.cancelDiscovery() before any attempt at connecting of discovering the service.

哦,我总是确保在尝试连接发现服务之前调用 BluetoothAdapter.cancelDiscovery()。

mBluetoothAdapter.cancelDiscovery();

Here is a method is use to connect in my runnable etc:

这是一种用于连接我的可运行等的方法:

public void connectToMachineService(BLEMachine machine) {
    Log.i(SERVICE_NAME, "ATTEMPTING TO CONNECT TO machine.getDevice().getName());

    mBluetoothAdapter.cancelDiscovery();

    if(mBluetoothGatt == null)
        mBluetoothGatt = machine.getDevice().connectGatt(this, false, mGattCallback);
    else
        mBluetoothGatt.discoverServices();
}

Lastly, make sure that you close out any BluetoothGatt objects that you have connected to. It appears that Android can handle five BluetoothGatt objects before it starts saying "unable to connect to Gatt server" or other something like that.

最后,确保关闭所有已连接的 BluetoothGatt 对象。在开始说“无法连接到 Gatt 服务器”或其他类似内容之前,Android 似乎可以处理五个 BluetoothGatt 对象。

On every BluetoothGatt that I create, I will call close on it then broadcast an update stating the connection is closed. It seems that there are a lot of times where the BluetootGatt will not respond with a state change when it becomes disconnected. My method of closing the BluetoothGatt goes something like this. I leave the method open up for the Activity to call the service and disconnect if a machine becomes non - responsive and the disconnect state is not called.

在我创建的每个 BluetoothGatt 上,我都会调用 close ,然后广播一个更新,说明连接已关闭。似乎有很多时候 BluetootGatt 在断开连接时不会响应状态更改。我关闭 BluetoothGatt 的方法是这样的。如果机器变得无响应并且未调用断开连接状态,我会为 Activity 打开该方法以调用服务并断开连接。

public void disconnectGatt(BluetoothGatt gatt) {
    if(gatt != null) {
        gatt.close();
        gatt = null;
    }

    broadcastUpdate(ACTION_STATE_CLOSED);
}