由Google官方的API可以查到:
file:///D:/Users/android-adk/docs/reference/android/bluetooth/BluetoothGatt.html#discoverServices()
connect()和connectGatt都是连接BLE设备的方法,但二者用法不同。
connectGatt是BluetoothDevice类下的方法,功能是向BLE设备发起连接,然后得到一个BluetoothGatt类型的返回值,利用这个返回值可以进行下一步操作。
connect是BluetoothGatt类下的方法,功能是re-connect,重新连接。如果BLE设备和APP已经连接过,但是因为设备超出了蓝牙的连接范围而断掉,那么当设备重新回到连接范围内时,可以通过connect()重新连接。
贴两张API的图
两个方法的逻辑上大体是这样的
先使用connectGatt方法发起连接,连接状态的改变会回调callback对象中的onConnectionStateChange(需要自己定义一个BluetoothGattCallBack对象并重写onConnectionStateChange),并返回一个BluetoothGatt对象,这时BluetoothGatt已经实例化,下一次连接可以调用connect重新连接。
下面附上BluetoothGatt的API,可以看出,得到BluetoothGatt实例的方法就是调用connectGatt去发起连接