From 10e01c3a9b521cd194bf4f6100587c93cd77d931 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Sat, 27 Apr 2024 22:15:11 +0200 Subject: [PATCH] [WIP] added very basic p2p test (seeing peers and group information) --- .../faraphel/tasks_valider/MainActivity.kt | 146 +++++------------- 1 file changed, 38 insertions(+), 108 deletions(-) diff --git a/app/src/main/java/com/faraphel/tasks_valider/MainActivity.kt b/app/src/main/java/com/faraphel/tasks_valider/MainActivity.kt index 4131ee2..20253aa 100644 --- a/app/src/main/java/com/faraphel/tasks_valider/MainActivity.kt +++ b/app/src/main/java/com/faraphel/tasks_valider/MainActivity.kt @@ -1,8 +1,7 @@ package com.faraphel.tasks_valider import android.content.Context -import android.net.wifi.p2p.WifiP2pConfig -import android.net.wifi.p2p.WifiP2pManager +import android.net.wifi.p2p.* import android.net.wifi.p2p.WifiP2pManager.ActionListener import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED @@ -19,9 +18,11 @@ import java.net.Socket class MainActivity : ComponentActivity() { + private var p2pManager: WifiP2pManager? = null + private var p2pChannel: WifiP2pManager.Channel? = null companion object { - lateinit var database: Database + private lateinit var database: Database } @RequiresApi(Build.VERSION_CODES.O) @@ -42,19 +43,34 @@ class MainActivity : ComponentActivity() { this.registerReceiver(MyBroadcastReceiver(this), intentFilter) */ - - // get the WiFi direct manager - val manager: WifiP2pManager? = this.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager? - if (manager == null) { - Log.e("wifi-p2p", "cannot get the manager") + // get the WiFi-Direct manager + this.p2pManager = this.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager? + if (this.p2pManager == null) { + Log.e("wifi-p2p", "cannot access the WiFi-Direct manager") return } - Log.d("wifi-p2p", "manager: $manager") - val channel = manager.initialize(this, this.mainLooper, null) - Log.d("wifi-p2p", "channel: $channel") + // get the Wifi-Direct channel + this.p2pChannel = this.p2pManager!!.initialize(this, this.mainLooper, null) + + // test the current channel + this.callWithP2pPeers { devices -> + // DEBUG + Log.d("wifi-p2p", "Peers found : ${devices.size}") + devices.forEach { device -> + Log.d( + "wifi-p2p", + "peer found : [${device.deviceAddress}] ${device.deviceName}" + ) + } + } + + this.callWithP2pGroup { group -> + Log.d("wifi-p2p", "Is group owner : ${group.isGroupOwner}") + } /* + // NOTE(Faraphel): it seem that this feature is deprecated and should be done manually. // get the list of peers in the group manager.discoverPeers(channel, object : ActionListener { override fun onSuccess() { @@ -67,107 +83,21 @@ class MainActivity : ComponentActivity() { } }) */ + } - /* - manager.requestPeers(channel) { devices -> - // NOTE: might be empty if the "Location" permission is not granted + private fun callWithP2pPeers(callback: (Collection) -> Unit) { + this.p2pManager?.requestPeers(this.p2pChannel) { devices -> + // NOTE(Faraphel): might be empty if the "Location" permission is not granted + if (devices.deviceList.isEmpty()) + Log.w("wifi-p2p", "No WiFi-Direct devices found. Have you enabled, granted required permissions and created the group ?") - Log.d("wifi-p2p", "Peers found : ${devices.deviceList.size}") - devices.deviceList.forEach { device -> - Log.d( - "wifi-p2p", - "peer found : [${device.deviceAddress}] ${device.deviceName}" - ) - } + callback(devices.deviceList) } - */ + } - /* - manager.requestGroupInfo(channel) { group -> - val config = WifiP2pConfig().apply { - this.deviceAddress = group.owner.deviceAddress - } - - if (group.isGroupOwner) { - // the device is the host - Log.d("wifi-p2p", "I am the owner ! (${group.owner})") - - Thread { - while (true) { - // declare the server - val serverSocket = ServerSocket() - serverSocket.bind(InetSocketAddress(8888)) - - // accept a connection - val clientSocket = serverSocket.accept() - val data = clientSocket.getInputStream().read() - - // print the data - Log.i("wifi-p2p", "Data received (${clientSocket.inetAddress}) : $data") - - // close the connection - clientSocket.close() - } - }.start() - - } else { - // the device is a simple peer - Log.d("wifi-p2p", "I am a peer !") - - // connect to the host and send a packet - Thread { - manager.connect(channel, config, object : ActionListener { - override fun onSuccess() { - val hostAddress = InetAddress.getByName(config.deviceAddress) - val port = 8888 - - Log.d("wifi-p2p", "Sending packet to host") - val socket = Socket() - socket.connect(InetSocketAddress(hostAddress, port)) - socket.outputStream.write("test".toByteArray()) - } - - override fun onFailure(reason: Int) { - Log.e("wifi-p2p", "Error when connecting to host : $reason") - } - }) - } - } + private fun callWithP2pGroup(callback: (WifiP2pGroup) -> Unit) { + this.p2pManager?.requestGroupInfo(this.p2pChannel) { group -> + callback(group) } - */ - - /* - Log.d("socket", "Test") - */ - - /* - Thread { - try { - Log.d("socket", "Starting server") - - ServerSocket(26203).use { serverSocket -> - while (true) { - val clientSocket = serverSocket.accept() - val data = clientSocket.getInputStream().read() - - Log.i("socket", "Data received (${clientSocket.inetAddress}) : $data") - - clientSocket.close() - } - } - } catch (e: Exception) { - Log.d("socket", "server already started. Becoming client") - - while (true) { - Socket("localhost", 26203).use { socket -> - socket.outputStream.write("test".toByteArray()) - - Log.i("socket", "Data sent") - } - } - } - }.start() - */ - } }