in amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/AmazonFreeRTOSDevice.java [445:490]
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
Log.i(TAG, "BLE connection state changed: " + status + "; new state: "
+ AmazonFreeRTOSConstants.BleConnectionState.values()[newState]);
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
int bondState = mBluetoothDevice.getBondState();
Log.i(TAG, "Connected to GATT server.");
mBleConnectionState = AmazonFreeRTOSConstants.BleConnectionState.BLE_CONNECTED;
mBleConnectionStatusCallback.onBleConnectionStatusChanged(mBleConnectionState);
// If the device is already bonded or will not bond we can call discoverServices() immediately
if (mBluetoothDevice.getBondState() != BOND_BONDING) {
discoverServices();
} else {
registerBondStateCallback();
}
} else {
Log.i(TAG, "Disconnected from GATT server due to error or peripheral initiated disconnect.");
mBleConnectionState = AmazonFreeRTOSConstants.BleConnectionState.BLE_DISCONNECTED;
if (mMqttConnectionState != AmazonFreeRTOSConstants.MqttConnectionState.MQTT_Disconnected) {
disconnectFromIot();
}
unRegisterBondStateCallback();
cleanUp();
mBleConnectionStatusCallback.onBleConnectionStatusChanged(mBleConnectionState);
/**
* If auto reconnect is enabled, start a reconnect procedure in background
* using connect() method. Else close the GATT.
* Auto reconnect will be disabled when user initiates disconnect.
*/
if (!mGattAutoReconnect) {
gatt.close();
mBluetoothGatt = null;
} else {
mBluetoothGatt.connect();
}
}
}