Skip to content

Usage Examples

Eliminater74 edited this page Jul 20, 2024 · 2 revisions

Usage Examples

Example 1: Tracking Devices

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 ###

Example 2: Executing Shell Commands

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)
    }
}

Example 3: Transferring Files

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")
    }
}

Example 4: Pairing a Device Using ADB Wi-Fi Pairing

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)
    }
}

Example 5: Function to Pair and Connect Devices

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)
}