//Function to connect with a selected device. fun connectToDevice(device: BluetoothDevice): Boolean { try { logManager.saveLogToFile("Connect to device initiated with MAC ID: ${device.address ?: ""} name: ${device.name ?: ""}") } catch (_: Exception) { } _isConnecting.value = true _connectionStatus.value = "Connecting..." Log.d(TAG, "Connecting to ${device.address}") device.connectGatt(context, false, bluetoothGattCallback) connecteddDevice = device stopScan() return true } //Bluetooth Gatt Callback to handle bluetooth responses private val bluetoothGattCallback = object : BluetoothGattCallback() { @SuppressLint("MissingPermission") override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) { super.onConnectionStateChange(gatt, status, newState) Log.d(TAG, "onConnectionStateChange: triggered $newState") if (newState == BluetoothGatt.STATE_CONNECTING) { _connectionStatus.value = "Connecting..." Log.d(TAG, "onConnectionStateChange: Connecting") } if (newState == BluetoothGatt.STATE_DISCONNECTING) { _connectionStatus.value = "Disconnecting..." Log.d(TAG, "onConnectionStateChange: Disconnecting") } if (newState == BluetoothGatt.STATE_CONNECTED) { Log.d(TAG, "Connected to ${gatt.device.address}") bluetoothGatt = gatt storage.saveStringData(storage.macId, gatt.device.address) Log.d(TAG, "GATT: " + gatt); gatt.discoverServices() Log.d(TAG, "Connected to ${gatt.device}") } if (newState == BluetoothGatt.STATE_DISCONNECTED) { _connectionStatus.value = "Disconnected" _isConnecting.value = false Log.d(TAG, "Disconnected from ${gatt.device.address}") try { bluetoothGatt?.close() bluetoothGatt = null connecteddDevice = null } catch (_: Exception) { } if (status != BluetoothGatt.GATT_SUCCESS) { Log.e(TAG, "Connection failed with status: $status") } } } override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) { super.onServicesDiscovered(gatt, status) if (status == BluetoothGatt.GATT_SUCCESS) { Log.d(TAG, "Services discovered") // Now discover characteristics for each service for (service in gatt.services) { Log.d(TAG, "Service UUID: ${service.uuid}") if (service.uuid.toString() .uppercase() == TiBleConstants.ResetService.SERVICE_ID.uppercase() ) { applicationViewModel.setMode(DeviceMode.USER_APPLICATION) } if (service.uuid.toString() .uppercase() == TiBleConstants.OADService.SERVICE_ID.uppercase() ) { applicationViewModel.setMode(DeviceMode.PERSISTENT_APPLICATION) } // Discover characteristics for (characteristic in service.characteristics) { Log.d(TAG, "Characteristic UUID: ${characteristic.uuid}") list.add(characteristic) } Log.d(TAG, "chars List :${list} ") val updatedServices = _serviceList.value.toMutableMap() updatedServices[service.uuid.toString()] = list _serviceList.value = updatedServices } _connectionStatus.value = "Connected" _isConnecting.value = false if (applicationViewModel.deviceMode.value == DeviceMode.PERSISTENT_APPLICATION) { subscribeForNotificationResponse() } } else { Log.e(TAG, "Failed to discover services with status $status") } } }