Keywords: ADB commands | Android device information | getprop properties
Abstract: This article provides an in-depth exploration of using ADB commands to accurately obtain detailed information about specific Android devices, including product names, models, and device identifiers. By analyzing the limitations of the adb devices -l command, it focuses on the solution using adb -s <device_id> shell getprop, explaining key properties such as ro.product.name, ro.product.model, and ro.product.device. The discussion covers technical details like newline handling across platforms, with complete code examples and practical guidance to help developers efficiently manage debugging in multi-device environments.
Technical Background of ADB Commands for Device Information
Android Debug Bridge (ADB) is an essential tool in Android development, offering various functionalities for communicating with Android devices. During development, it is often necessary to retrieve detailed information about connected devices, such as product names, models, and device identifiers, which is crucial for multi-device debugging, automated testing, and device management.
Limitations of the adb devices -l Command
The commonly used adb devices -l command lists detailed information for all connected devices, typically outputting in this format:
123abc12 device product:<id> model:<id> device:<id>
456abc45 device product:<id> model:<id> device:<id>However, when targeting a specific device (e.g., with serial number "123abc12"), this command outputs data for all devices, leading to redundancy and filtering challenges. This is particularly inconvenient in automated scripts or multi-device parallel testing scenarios.
Solution for Precise Retrieval of Specific Device Information
To address this issue, the adb -s <device_id> shell getprop command can be used. Here, the -s parameter specifies the target device's serial number, and getprop is the Android system property retrieval tool.
The basic usage is as follows:
adb -s 123abc12 shell getpropThis command returns all system properties and their values for the device, potentially spanning hundreds of lines. To precisely obtain the required information, property names can be specified further:
adb -s 123abc12 shell getprop ro.product.modelThe three key properties displayed in adb devices -l output correspond as follows:
product:<id>corresponds toro.product.namemodel:<id>corresponds toro.product.modeldevice:<id>corresponds toro.product.device
Thus, to fully retrieve device information, execute:
adb -s 123abc12 shell getprop ro.product.name
adb -s 123abc12 shell getprop ro.product.model
adb -s 123abc12 shell getprop ro.product.deviceTechnical Details and Cross-Platform Considerations
ADB shell output lines typically end with \r\n (carriage return and newline), which may cause parsing differences across operating systems. For instance, on Unix/Linux systems, you might receive a value like "Nexus 7\r" instead of a clean "Nexus 7". Developers need to handle these newline characters in scripts to ensure data accuracy.
Here is a Python example demonstrating how to clean the return value:
import subprocess
def get_device_property(device_id, prop_name):
cmd = f"adb -s {device_id} shell getprop {prop_name}"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
# Remove trailing newline and carriage return characters
cleaned_result = result.stdout.strip().replace('\r', '').replace('\n', '')
return cleaned_result
# Usage example
model = get_device_property("123abc12", "ro.product.model")
print(f"Device model: {model}")Practical Applications and Extensions
In real-world development, this can be combined with device serial number verification and batch processing. For example, writing a script to automatically detect all connected devices and generate a detailed report:
#!/bin/bash
# Get all device serial numbers
devices=$(adb devices | grep '\tdevice' | cut -f1)
for device in $devices
do
echo "Device: $device"
echo "Product: $(adb -s $device shell getprop ro.product.name | tr -d '\r')"
echo "Model: $(adb -s $device shell getprop ro.product.model | tr -d '\r')"
echo "Device identifier: $(adb -s $device shell getprop ro.product.device | tr -d '\r')"
echo "---"
doneThis approach not only enhances efficiency in multi-device management but also provides reliable data support for automated testing and continuous integration.