Implemented Connection with Wi-Fi Direct and IP #9
11 changed files with 110 additions and 74 deletions
13
README.md
13
README.md
|
@ -1,3 +1,12 @@
|
|||
# Study-M1-PDS
|
||||
# Master 1 - Projet de Spécialité
|
||||
|
||||
(Université) - Projet De Spécialité
|
||||
Ce projet consiste en une application Android permettant à des enseignants de créer des session de cours où un ensemble
|
||||
d'élève est assigné à des tâches de sujet différent qui peuvent être validé collaborativement.
|
||||
|
||||
Il est possible d'utiliser le QR code des cartes étudiantes afin de substituer l'identifiant d'un étudiant et de
|
||||
pouvoir valider rapidement les tâches d'un élève.
|
||||
|
||||
# Build
|
||||
|
||||
1. Cloner le projet `git clone https://git.faraphel.fr/study-faraphel/M1-PDS`
|
||||
2. Ouvrer le dans Android Studio ou Intellij IDEA
|
|
@ -48,7 +48,8 @@
|
|||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:theme="@style/Theme.Tasksvalider">
|
||||
android:theme="@style/Theme.Tasksvalider"
|
||||
android:screenOrientation="portrait">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.faraphel.tasks_valider.connectivity.bwd
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
|
@ -41,6 +42,7 @@ class BwdManager(
|
|||
* Create a new BwfManager from an activity.
|
||||
* @param activity The activity to create the manager from
|
||||
*/
|
||||
@SuppressLint("UnspecifiedRegisterReceiverFlag")
|
||||
fun fromActivity(activity: Activity): BwdManager {
|
||||
// check if the system support WiFi-Direct
|
||||
if (!this.isSupported(activity)) {
|
||||
|
@ -67,9 +69,14 @@ class BwdManager(
|
|||
|
||||
// get the WiFi-Direct channel
|
||||
val channel = manager.initialize(activity, activity.mainLooper, null)
|
||||
return BwdManager(manager, channel)
|
||||
|
||||
// NOTE(Faraphel): the broadcast receiver should be registered in the activity onResume
|
||||
// create the manager
|
||||
val bwdManager = BwdManager(manager, channel)
|
||||
|
||||
// make the manager receive the application intents
|
||||
activity.registerReceiver(bwdManager, ALL_INTENT_FILTER)
|
||||
|
||||
return bwdManager
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -174,6 +181,7 @@ class BwdManager(
|
|||
val stateConnectionInfo = mutableStateOf<WifiP2pInfo?>(null)
|
||||
val statePeers = mutableStateOf<WifiP2pDeviceList?>(null)
|
||||
|
||||
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
// ignore empty intent
|
||||
if (intent == null)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.faraphel.tasks_valider.ui.screen.communication.connection.internet
|
||||
package com.faraphel.tasks_valider.ui.screen.communication.connection.internet.role
|
||||
|
||||
import android.app.Activity
|
||||
import androidx.compose.foundation.layout.*
|
|
@ -1,4 +1,4 @@
|
|||
package com.faraphel.tasks_valider.ui.screen.communication.connection.internet
|
||||
package com.faraphel.tasks_valider.ui.screen.communication.connection.internet.role
|
||||
|
||||
import android.app.Activity
|
||||
import android.util.Log
|
|
@ -1,8 +1,6 @@
|
|||
package com.faraphel.tasks_valider.ui.screen.communication.connection.internet
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Text
|
||||
|
@ -16,6 +14,8 @@ import androidx.navigation.compose.NavHost
|
|||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import com.faraphel.tasks_valider.database.TaskDatabase
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.connection.internet.role.CommunicationInternetClientScreen
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.connection.internet.role.CommunicationInternetServerScreen
|
||||
|
||||
|
||||
@Composable
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
package com.faraphel.tasks_valider.ui.screen.communication.connection.wifiP2p
|
||||
|
||||
import android.app.Activity
|
||||
import android.net.wifi.p2p.WifiP2pConfig
|
||||
import android.net.wifi.p2p.WifiP2pDevice
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import com.faraphel.tasks_valider.connectivity.bwd.BwdManager
|
||||
import com.faraphel.tasks_valider.ui.widgets.connectivity.WifiP2pDeviceListWidget
|
||||
|
||||
|
||||
@Composable
|
||||
fun CommunicationWifiP2pClientScreen(activity: Activity, bwdManager: BwdManager) {
|
||||
val selectedDevice = remember { mutableStateOf<WifiP2pDevice?>(null) }
|
||||
val isConnected = remember { mutableStateOf(false) }
|
||||
|
||||
// if connected, show the task group screen
|
||||
if (isConnected.value) {
|
||||
// TaskGroupScreen(activity, null)
|
||||
// TODO(Faraphel): finish the connection
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// if the device is selected but not connected, try to connect
|
||||
if (selectedDevice.value != null) {
|
||||
// TODO(Faraphel): error handling
|
||||
val config = WifiP2pConfig().apply {
|
||||
deviceAddress = selectedDevice.value!!.deviceAddress
|
||||
}
|
||||
bwdManager.connect(config) {
|
||||
isConnected.value = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// display the list of devices
|
||||
CommunicationWifiP2pClientContent(bwdManager, selectedDevice)
|
||||
}
|
||||
|
||||
|
||||
@Composable
|
||||
fun CommunicationWifiP2pClientContent(
|
||||
bwdManager: BwdManager,
|
||||
selectedDevice: MutableState<WifiP2pDevice?>
|
||||
) {
|
||||
Column {
|
||||
WifiP2pDeviceListWidget(
|
||||
peers = bwdManager.statePeers.value,
|
||||
filter = { device: WifiP2pDevice -> device.isGroupOwner },
|
||||
selectedDevice,
|
||||
)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package com.faraphel.tasks_valider.ui.screen.communication.connection.wifiP2p.role
|
||||
|
||||
import android.app.Activity
|
||||
import android.net.wifi.p2p.WifiP2pConfig
|
||||
import android.net.wifi.p2p.WifiP2pDevice
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.runtime.*
|
||||
import com.faraphel.tasks_valider.connectivity.bwd.BwdManager
|
||||
import com.faraphel.tasks_valider.connectivity.task.TaskClient
|
||||
import com.faraphel.tasks_valider.connectivity.task.session.TaskSession
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.DEFAULT_SERVER_PORT
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.authentication.AuthenticationClientScreen
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.connection.internet.role.CommunicationInternetClientContent
|
||||
import com.faraphel.tasks_valider.ui.screen.task.TaskSessionController
|
||||
import com.faraphel.tasks_valider.ui.screen.task.TaskSessionScreen
|
||||
import com.faraphel.tasks_valider.ui.widgets.connectivity.WifiP2pDeviceListWidget
|
||||
|
||||
|
||||
@Composable
|
||||
fun CommunicationWifiP2pClientScreen(activity: Activity, bwdManager: BwdManager) {
|
||||
val client = remember { mutableStateOf<TaskClient?>(null) }
|
||||
val session = remember { mutableStateOf<TaskSession?>(null) }
|
||||
|
||||
// if the client is not connected, show the connection screen
|
||||
if (client.value == null)
|
||||
return CommunicationWifiP2pClientContent(client, bwdManager)
|
||||
// if the user is not authenticated, show the authentication screen
|
||||
if (session.value == null)
|
||||
return AuthenticationClientScreen(activity, client.value!!, session)
|
||||
|
||||
// show the main screen
|
||||
TaskSessionController(
|
||||
activity,
|
||||
client.value!!,
|
||||
session.value!!.person,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@Composable
|
||||
fun CommunicationWifiP2pClientContent(
|
||||
client: MutableState<TaskClient?>,
|
||||
bwdManager: BwdManager,
|
||||
) {
|
||||
val selectedDevice = remember { mutableStateOf<WifiP2pDevice?>(null) }
|
||||
|
||||
if (selectedDevice.value == null) {
|
||||
Column {
|
||||
WifiP2pDeviceListWidget(
|
||||
peers = bwdManager.statePeers.value,
|
||||
filter = { device: WifiP2pDevice -> device.isGroupOwner },
|
||||
selectedDevice,
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(true) {
|
||||
// look for new peers
|
||||
bwdManager.discoverPeers()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
val config = WifiP2pConfig().apply {
|
||||
deviceAddress = selectedDevice.value!!.deviceAddress
|
||||
}
|
||||
bwdManager.connect(config) {
|
||||
bwdManager.requestConnectionInfo { connectionInfo ->
|
||||
// TODO(Faraphel): check if the server is reachable
|
||||
client.value = TaskClient(
|
||||
connectionInfo.groupOwnerAddress.toString(),
|
||||
DEFAULT_SERVER_PORT
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,12 @@
|
|||
package com.faraphel.tasks_valider.ui.screen.communication.connection.wifiP2p
|
||||
package com.faraphel.tasks_valider.ui.screen.communication.connection.wifiP2p.role
|
||||
|
||||
import android.app.Activity
|
||||
import android.util.Log
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.compose.NavHost
|
||||
|
@ -23,9 +21,7 @@ import com.faraphel.tasks_valider.database.entities.PersonEntity
|
|||
import com.faraphel.tasks_valider.database.entities.SessionEntity
|
||||
import com.faraphel.tasks_valider.database.populateSubjectSessionPersonTest
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.DEFAULT_SERVER_PORT
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.RANGE_SERVER_PORT
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.authentication.AuthenticationServerScreen
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.connection.internet.CommunicationInternetServerContent
|
||||
import com.faraphel.tasks_valider.ui.screen.task.TaskSessionController
|
||||
import java.time.Instant
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
package com.faraphel.tasks_valider.ui.screen.communication.connection.wifiP2p
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
|
@ -16,7 +16,10 @@ import androidx.navigation.compose.NavHost
|
|||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import com.faraphel.tasks_valider.connectivity.bwd.BwdManager
|
||||
import com.faraphel.tasks_valider.connectivity.bwd.BwdManager.Companion.ALL_INTENT_FILTER
|
||||
import com.faraphel.tasks_valider.database.TaskDatabase
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.connection.wifiP2p.role.CommunicationWifiP2pClientScreen
|
||||
import com.faraphel.tasks_valider.ui.screen.communication.connection.wifiP2p.role.CommunicationWifiP2pServerScreen
|
||||
|
||||
|
||||
@Composable
|
||||
|
|
|
@ -88,10 +88,10 @@ fun CommunicationSelectContent(controller: NavController, activity: Activity) {
|
|||
// if the WiFi-Direct is supported, navigate to the WiFi-Direct screen
|
||||
if (isWifiP2pSupported) controller.navigate("wifi-p2p")
|
||||
// if the WiFi-Direct is not supported, show a toast message
|
||||
else Toast.makeText(activity, "WiFi-Direct is not supported on this device", Toast.LENGTH_SHORT).show()
|
||||
else Toast.makeText(activity, "Wi-Fi Direct is not supported on this device", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
) {
|
||||
Text("WiFi-Direct")
|
||||
Text("Wi-Fi Direct")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue