-
Notifications
You must be signed in to change notification settings - Fork 0
Usage Examples
Eliminater74 edited this page Jul 20, 2024
·
2 revisions
This example demonstrates how to use adblib
to track all new devices connected through ADB, similar to adb track-devices
:
import com.android.adblib.AdbSession
import com.android.adblib.AdbSessionHost
import com.android.adblib.trackDevices
import kotlinx.coroutines.runBlocking
fun main(args: Array<String>) {
val host = AdbSessionHost()
val session = AdbSession.create(host)
val devicesFlow = session.trackDevices()
runBlocking {
devicesFlow.collect { deviceList ->
println("### Change in state ###")
deviceList.devices.forEach { device ->
println("${device.model}: ${device.serialNumber}: ${device.deviceState}")
}
}
}
}
Output while connecting and disconnecting a Pixel 8 phone:
### Change in state ###
### Change in state ###
Pixel_8: XXXXXXXXXXXXXX: ONLINE
### Change in state ###
Pixel_8: XXXXXXXXXXXXXX: OFFLINE
### Change in state ###
This example demonstrates how to execute shell commands on a connected device:
import com.android.adblib.AdbSession
import com.android.adblib.AdbSessionHost
import kotlinx.coroutines.runBlocking
fun main(args: Array<String>) {
val host = AdbSessionHost()
val session = AdbSession.create(host)
runBlocking {
val device = session.device("XXXXXXXXXXXXXX") // Replace with actual device serial number
val output = device.executeShellCommand("echo Hello, World!")
println(output)
}
}
This example shows how to transfer files between the host and a connected device:
import com.android.adblib.AdbSession
import com.android.adblib.AdbSessionHost
import kotlinx.coroutines.runBlocking
import java.io.File
fun main(args: Array<String>) {
val host = AdbSessionHost()
val session = AdbSession.create(host)
runBlocking {
val device = session.device("XXXXXXXXXXXXXX") // Replace with actual device serial number
val localFile = File("path/to/local/file.txt")
val remotePath = "/sdcard/remote_file.txt"
device.push(localFile, remotePath)
println("File transferred successfully to $remotePath")
}
}
This example demonstrates how to pair a device using the new ADB Wi-Fi pairing introduced in Android 12:
import com.android.adblib.AdbSession
import com.android.adblib.AdbSessionHost
import kotlinx.coroutines.runBlocking
fun main(args: Array<String>) {
val host = AdbSessionHost()
val session = AdbSession.create(host)
runBlocking {
// Step 1: Pairing with device
val ipAddress = "192.168.1.100" // Replace with the device's IP address
val pairingPort = 37199 // Replace with the device's pairing port
val pairingCode = "123456" // Replace with the pairing code shown on the device
val pairingCommand = "adb pair $ipAddress:$pairingPort $pairingCode"
val pairingOutput = session.host.executeShellCommand(pairingCommand)
println(pairingOutput)
// Step 2: Connect to the device
val connectCommand = "adb connect $ipAddress"
val connectOutput = session.host.executeShellCommand(connectCommand)
println(connectOutput)
}
}
Here’s a reusable function for pairing and connecting to devices using the new ADB Wi-Fi pairing:
import com.android.adblib.AdbSession
import com.android.adblib.AdbSessionHost
import kotlinx.coroutines.runBlocking
fun pairAndConnect(session: AdbSession, ipAddress: String, pairingPort: Int, pairingCode: String): String {
runBlocking {
// Pair with the device
val pairingCommand = "adb pair $ipAddress:$pairingPort $pairingCode"
val pairingOutput = session.host.executeShellCommand(pairingCommand)
println("Pairing output: $pairingOutput")
// Connect to the device
val connectCommand = "adb connect $ipAddress"
val connectOutput = session.host.executeShellCommand(connectCommand)
println("Connect output: $connectOutput")
return connectOutput
}
}
fun main() {
val host = AdbSessionHost()
val session = AdbSession.create(host)
val ipAddress = "
192.168.1.100" // Replace with the device's IP address
val pairingPort = 37199 // Replace with the device's pairing port
val pairingCode = "123456" // Replace with the pairing code shown on the device
val result = pairAndConnect(session, ipAddress, pairingPort, pairingCode)
println(result)
}
- Home
- Overview
- Features
- Installation
- Usage Examples
- Integrating with Android Studio
- Integrating with Visual Studio
- Metrics
- Contribution
- License
- Why We Created This Repository
- Disclaimer
- Repository
For more information or if you have any questions, please open an issue on our GitHub repository.
© 2024 DevOculus-Meta-Quest. All rights reserved.