This is the native Android client for the custom Smart Home control system. It acts as a remote control, communicating with the C++ backend server via a secured REST API to manage IoT devices in real-time.
The application follows a clean client-server model tailored for Android:
- UI Layer (Frontend): XML layouts define the user interface, providing intuitive controls (Buttons, TextViews, EditTexts) for each device type.
- Business Logic (Activity):
MainActivityhandles user interactions, validates input (e.g., temperature limits), and manages the application lifecycle. - Network Layer (Data): Powered by Retrofit 2, this layer handles all HTTP communication. It uses Kotlin Coroutines for asynchronous non-blocking calls and automatically injects the required
X-API-Keysecurity token into every request.
- Real-time Control: Instantly toggle lights, lock/unlock doors, and adjust air conditioner temperature.
- Live Status Updates: The app fetches and displays the current state of all devices directly from the server upon launch and after every action.
- Secure Communication: All API requests are authenticated using a custom
X-API-Keyheader. - Input Validation: Prevents sending invalid data (e.g., temperature outside the 13-31°C range) to the server.
- Language: Kotlin
- Core Components: Android SDK, AppCompat
- Networking: Retrofit 2, OkHttp, GSON (for JSON parsing)
- Concurrency: Kotlin Coroutines (suspend functions, lifecycleScope)
- Build System: Gradle
Before building the project, you must configure it to match your local network environment.
- Android Studio Iguana (or newer)
- Android SDK (API level 31 or higher recommended)
- An Android device or emulator connected to the same Wi-Fi network as the C++ server.
-
Configure Server IP: Open
app/src/main/java/com/example/smarthomeapp/RetrofitClient.ktand update theBASE_URLwith your C++ server's local IP address:// Replace '192.168.0.104' with your actual server IP found via 'ipconfig' private const val BASE_URL = "[http://192.168.0.104:18080/](http://192.168.0.104:18080/)"
-
Configure Security Token: Open
app/src/main/java/com/example/smarthomeapp/MainActivity.ktand ensure theAPI_KEYmatches the one configured on your backend:private val API_KEY = "my-secret-key-12345"
-
Network Permissions: Ensure your
AndroidManifest.xmlincludes the necessary permissions for local network communication:<uses-permission android:name="android.permission.INTERNET" /> <application ... android:usesCleartextTraffic="true" ...>
- Open Project: Launch Android Studio and open the project root folder.
- Sync Gradle: Allow Android Studio to download necessary dependencies.
- Run: Connect your Android device via USB (ensure USB Debugging is enabled) or launch an AVD (Android Virtual Device). Click the Run button (▶) in Android Studio.
- "Network Error" / Timeout:
- Ensure both phone and PC are on the same Wi-Fi network.
- Verify the IP address in
RetrofitClient.ktmatches your PC's current IP. - Check Windows Firewall on your PC — allow incoming traffic on port
18080for Private networks.
- "Cleartext traffic not permitted":
- Android blocks unencrypted HTTP by default. Verify that
android:usesCleartextTraffic="true"is present in yourAndroidManifest.xmltag.
- Android blocks unencrypted HTTP by default. Verify that
- 401 Unauthorized:
- The
API_KEYinMainActivity.ktdoes not match the key expected by the C++ server.
- The