이 튜토리얼에서는 Android 앱에서 현재 연결된 Wi-Fi의 최대 링크 속도를 조회하여 화면에 표시하는 방법을 단계별로 알아봅니다. WifiManager와 WifiInfo 클래스를 활용하면 몇 줄의 코드만으로 간단하게 구현할 수 있습니다.
1단계 – 새 프로젝트 생성
Android Studio에서 File → New Project를 선택하고, 필요한 정보를 모두 입력하여 새 프로젝트를 생성합니다.
2단계 – 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>위 코드에서는 Wi-Fi 속도 정보를 화면에 보여주기 위한 TextView 하나를 배치했습니다.
3단계 – MainActivity 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다.
package com.example.myapplication;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
textView.setText(""+ linkSpeed+" Mbps");
}
}
}코드 설명: 먼저 getSystemService(WIFI_SERVICE)를 통해 WifiManager 인스턴스를 가져옵니다. 이후 getConnectionInfo() 메서드로 현재 Wi-Fi 연결 정보(WifiInfo)를 얻고, getLinkSpeed()를 호출하면 현재 링크 속도가 Mbps 단위(WifiInfo.LINK_SPEED_UNITS 기준)로 반환됩니다. 마지막으로 이 값을 TextView에 출력합니다.
4단계 – 매니페스트 권한 설정
AndroidManifest.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
android:roundIcon = "@mipmap/ic_launcher_round"
android:supportsRtl = "true"
android:theme = "@style/AppTheme">
<activity android:name = ".MainActivity">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<action android:name = "android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>Wi-Fi 상태를 읽어오려면 ACCESS_WIFI_STATE, Wi-Fi 상태를 변경하려면 CHANGE_WIFI_STATE 권한이 반드시 선언되어 있어야 합니다.
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 Android 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바의 Run 아이콘을 클릭하세요. 옵션 목록에서 사용 중인 모바일 기기를 선택하면, 기기 화면에 현재 Wi-Fi 링크 속도가 Mbps 단위로 표시되는 것을 확인할 수 있습니다.
