问题描述
Android 低功耗蓝牙 API 实现了 1 种连接设备的方法 connectGatt()
但有 2 种关闭连接的方法 disconnect()
和 close()
.
Android Bluetooth Low Energy API implements 1 method to connect to the device connectGatt()
but 2 methods to close the connection disconnect()
and close()
.
文档说:
disconnect()
:断开已建立的连接,或取消连接尝试目前正在进行中.
disconnect()
: Disconnects an established connection, or cancels a connection attempt currently in progress.
close()
:应用程序应在完成后尽早调用此方法这个 GATT 客户端.
close()
: Application should call this method as early as possible after it is done with this GATT client.
的源代码BluetoothGatt.java 显示 close() 取消注册应用程序,并且 disconnect() 断开客户端连接.然而,它并没有说明这实际上意味着什么.我的意思是,如果只有 1 种方法可以连接到客户端,为什么有 2 种方法可以关闭/断开连接?
The source code of BluetoothGatt.java shows 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?
推荐答案
使用 disconnect()
您可以稍后调用 connect()
并继续该循环.
With disconnect()
you can later call connect()
and continue with that cycle.
一旦你调用 close()
你就完成了.如果您想再次连接,则必须再次调用 BluetoothDevice
上的 connectGatt()
;close()
将释放 BluetoothGatt
持有的所有资源.
Once you call close()
you are done. If you want to connect again you will have to call connectGatt()
on the BluetoothDevice
again; close()
will release any resources held by BluetoothGatt
.
这篇关于close() 和 disconnect() 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!