2. Stream logcat
2. Stream logcat
Some requests in adam stream their output. Logcat is one of them. The device produces logcat output continuously. The stream ends only when you stop reading or the device stops producing output.
Start with the boilerplate from part 1 to set up the communication with the device:
StartAdbInteractor().execute()
val adb = AndroidDebugBridgeClientFactory().build()
val devices: List<Device> = adb.execute(ListDevicesRequest())
val device = devices.firstOrNull { it.state == DeviceState.DEVICE } ?: throw RuntimeException("no devices available")
Now execute the request:
launch {
val channel: ReceiveChannel<String> = adb.execute(
request = ChanneledLogcatRequest(),
scope = this,
serial = device.serial
)
do {
val line: String? = channel.receiveCatching().getOrNull()
if (line != null) {
println(line)
}
if (stopStreaming) { //your stop condition, e.g. a user action
channel.cancel()
break
}
} while (line != null)
}
Note the return type ReceiveChannel<String>. The channel can deliver more String instances over time. One iteration of the loop handles one value:
receiveCatching()waits for the next chunk. When a chunk arrives, the code prints it.- When the channel closes without a chunk,
getOrNull()returnsnull. The channel closes when the device stops producing output. stopStreamingis your external stop condition, for example a user action. Cancel the channel to release the transport resources. Then exit the loop.
The loop repeats until the device stops producing output or you stop it.
There are more options for ChanneledLogcatRequest. They change the format of the output, the filtering, and more.