[WIP] added very basic p2p test (seeing peers and group information)
This commit is contained in:
parent
449bb6f5d3
commit
10e01c3a9b
1 changed files with 38 additions and 108 deletions
|
@ -1,8 +1,7 @@
|
||||||
package com.faraphel.tasks_valider
|
package com.faraphel.tasks_valider
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.wifi.p2p.WifiP2pConfig
|
import android.net.wifi.p2p.*
|
||||||
import android.net.wifi.p2p.WifiP2pManager
|
|
||||||
import android.net.wifi.p2p.WifiP2pManager.ActionListener
|
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_CHANGED_ACTION
|
||||||
import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED
|
import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED
|
||||||
|
@ -19,9 +18,11 @@ import java.net.Socket
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
|
private var p2pManager: WifiP2pManager? = null
|
||||||
|
private var p2pChannel: WifiP2pManager.Channel? = null
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
lateinit var database: Database
|
private lateinit var database: Database
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
|
@ -42,19 +43,34 @@ class MainActivity : ComponentActivity() {
|
||||||
this.registerReceiver(MyBroadcastReceiver(this), intentFilter)
|
this.registerReceiver(MyBroadcastReceiver(this), intentFilter)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// get the WiFi-Direct manager
|
||||||
// get the WiFi direct manager
|
this.p2pManager = this.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager?
|
||||||
val manager: WifiP2pManager? = this.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager?
|
if (this.p2pManager == null) {
|
||||||
if (manager == null) {
|
Log.e("wifi-p2p", "cannot access the WiFi-Direct manager")
|
||||||
Log.e("wifi-p2p", "cannot get the manager")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Log.d("wifi-p2p", "manager: $manager")
|
|
||||||
|
|
||||||
val channel = manager.initialize(this, this.mainLooper, null)
|
// get the Wifi-Direct channel
|
||||||
Log.d("wifi-p2p", "channel: $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
|
// get the list of peers in the group
|
||||||
manager.discoverPeers(channel, object : ActionListener {
|
manager.discoverPeers(channel, object : ActionListener {
|
||||||
override fun onSuccess() {
|
override fun onSuccess() {
|
||||||
|
@ -67,107 +83,21 @@ class MainActivity : ComponentActivity() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
*/
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
private fun callWithP2pPeers(callback: (Collection<WifiP2pDevice>) -> Unit) {
|
||||||
manager.requestPeers(channel) { devices ->
|
this.p2pManager?.requestPeers(this.p2pChannel) { devices ->
|
||||||
// NOTE: might be empty if the "Location" permission is not granted
|
// 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}")
|
callback(devices.deviceList)
|
||||||
devices.deviceList.forEach { device ->
|
|
||||||
Log.d(
|
|
||||||
"wifi-p2p",
|
|
||||||
"peer found : [${device.deviceAddress}] ${device.deviceName}"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
|
|
||||||
/*
|
private fun callWithP2pGroup(callback: (WifiP2pGroup) -> Unit) {
|
||||||
manager.requestGroupInfo(channel) { group ->
|
this.p2pManager?.requestGroupInfo(this.p2pChannel) { group ->
|
||||||
val config = WifiP2pConfig().apply {
|
callback(group)
|
||||||
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")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
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()
|
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue