Skip to main content

Overview

The Scrcpy class is the main Android service that manages the client-side screen mirroring functionality. It handles socket connections to the scrcpy server, manages video and audio decoders, and processes touch and key events. Package: org.client.scrcpy Extends: android.app.Service

Starting the Service

start()

Initializes and starts the scrcpy service with screen mirroring.
Surface
required
The Android Surface to render video output
String
required
Server address in format “host:port” (e.g., “127.0.0.1:7008”)
int
required
Height of the display surface in pixels
int
required
Width of the display surface in pixels
int
required
Network delay tolerance in milliseconds for frame dropping
Example:

Touch Event Handling

touchevent()

Processes touch events and sends them to the remote device with coordinate transformation.
MotionEvent
required
The Android MotionEvent to process
boolean
required
Whether the remote device is in landscape orientation
int
required
Current display width in pixels
int
required
Current display height in pixels
boolean
Returns true if the event was processed successfully
Supported Actions:
  • ACTION_DOWN - First finger touch
  • ACTION_UP - Last finger lift
  • ACTION_MOVE - Touch movement (all fingers)
  • ACTION_POINTER_DOWN - Additional finger touch
  • ACTION_POINTER_UP - Additional finger lift
Example:
Multi-touch Support: The method handles multi-touch by iterating through all pointer indices for ACTION_MOVE events:

Key Event Handling

sendKeyevent()

Sends a key event to the remote device.
int
required
Android KeyEvent keycode (e.g., KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_HOME)
Example:

Lifecycle Methods

pause()

Pauses video and audio decoding without disconnecting.
Example:

resume()

Resumes video and audio decoding after pause.
Example:

StopService()

Stops all decoders and terminates the service.
Example:

ServiceCallbacks Interface

Implement this interface to receive service events.

setServiceCallbacks()

Registers a callback listener.
ServiceCallbacks
required
Implementation of the ServiceCallbacks interface
Callback Methods:
void
Called when the remote device rotation changes and a new surface is needed
void
Called when the connection is lost or an error occurs
Example:

Connection Information

get_remote_device_resolution()

Returns the remote device’s native resolution.
int[]
Array of two integers [width, height] representing the device resolution
Example:

check_socket_connection()

Checks if the socket connection is established.
boolean
Returns true if connected, false otherwise
Example:

Constants

String
Default local IP address: "127.0.0.1"
int
Default forwarding port: 7008
int
Default ADB port: 5555

Event Data Format

Touch and key events are serialized as byte arrays before transmission: Touch Event Format (20 bytes):
Key Event Format (4 bytes):

See Also