getDeviceIpAddress method

Future<String?> getDeviceIpAddress(
  1. String serial
)

Implementation

Future<String?> getDeviceIpAddress(String serial) async {
  try {
    final result = await _exec.run(
      adbPath,
      arguments: ['-s', serial, 'shell', 'ip', 'route'],
    );

    if (result.success) {
      final match = RegExp(
        r'src\s+(\d+\.\d+\.\d+\.\d+)',
      ).firstMatch(result.stdout);
      if (match != null) {
        return match.group(1);
      }
    }

    final ifconfig = await _exec.run(
      adbPath,
      arguments: ['-s', serial, 'shell', 'ifconfig', 'wlan0'],
    );

    if (ifconfig.success) {
      final match = RegExp(
        r'inet addr:(\d+\.\d+\.\d+\.\d+)',
      ).firstMatch(ifconfig.stdout);
      if (match != null) {
        return match.group(1);
      }
    }

    return null;
  } catch (_) {
    return null;
  }
}