Android-wi-fi
Android-Wi-Fi
Androidでは、アプリケーションがアクセスして、ワイヤレス接続の状態を非常に低いレベルで表示できます。 アプリケーションは、wifi接続のほぼすべての情報にアクセスできます。
アプリケーションがアクセスできる情報には、接続されたネットワークのリンク速度、IPアドレス、ネゴシエーション状態、その他のネットワーク情報が含まれます。 アプリケーションは、Wi-Fi接続をスキャン、追加、保存、終了、および開始することもできます。
Androidは、WIFI接続のすべての側面を管理する WifiManager APIを提供します。 getSystemService メソッドを呼び出すことにより、このクラスをインスタンス化できます。 その構文は以下のとおりです-
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
ワイヤレスネットワークのリストをスキャンするには、BroadcastReceiverも登録する必要があります。 registerReceiver メソッドを使用して、レシーバクラスオブジェクトの引数を使用して登録できます。 その構文は以下のとおりです-
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
}
}
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
WifiManagerクラスの startScan メソッドを呼び出すことで、wifiスキャンを開始できます。 このメソッドは、ScanResultオブジェクトのリストを返します。 リストの get メソッドを呼び出すことにより、任意のオブジェクトにアクセスできます。 その構文は以下のとおりです-
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
String data = wifiScanList.get(0).toString();
スキャンだけでなく、WifiManagerクラスで定義されたメソッドを使用して、WIFIをより詳細に制御できます。 それらは次のようにリストされています-
Sr.No | Method & Description |
---|---|
1 |
addNetwork(WifiConfiguration config) このメソッドは、構成されたネットワークのセットに新しいネットワーク記述を追加します。 |
2 |
createWifiLock(String tag) このメソッドは、新しいWifiLockを作成します。 |
3 |
disconnect() このメソッドは、現在アクティブなアクセスポイントとの関連付けを解除します。 |
4 |
enableNetwork(int netId, boolean disableOthers) この方法により、以前に構成されたネットワークを関連付けることができます。 |
5 |
getWifiState() このメソッドは、Wi-Fi対応状態を取得します |
6 |
isWifiEnabled() このメソッドは、Wi-Fiが有効か無効かを返します。 |
7 |
setWifiEnabled(boolean enabled) このメソッドは、Wi-Fiを有効または無効にします。 |
8 |
updateNetwork(WifiConfiguration config) このメソッドは、既存の構成済みネットワークのネットワーク記述を更新します。 |
例
以下は、WIFIの使用を示す例です。 それはあなたの無線LANを開き、あなたの無線LANを閉じる基本的なアプリケーションを作成します
この例を試すには、wifiがオンになっている実際のデバイスでこれを実行する必要があります。
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add WebView code. |
3 | Modify the res/layout/activity_main to add respective XML components |
4 | Modify the AndroidManifest.xml to add the necessary permissions |
5 | Run the application and choose a running android device and install the application on it and verify the results. |
以下は、変更されたメインアクティビティファイル src/MainActivity.java の内容です。
package com.example.sairamkrishna.myapplication;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button enableButton,disableButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enableButton=(Button)findViewById(R.id.button1);
disableButton=(Button)findViewById(R.id.button2);
enableButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
}
});
disableButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);
}
});
}
}
以下は、xml res/layout/activity_main.xml の変更されたコンテンツです。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="76dp"
android:text="Enable Wifi"
android:layout_centerVertical="true"
android:layout_alignEnd="@+id/imageView"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable Wifi"
android:layout_marginBottom="93dp"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/imageView"/>
</RelativeLayout>
以下は AndroidManifest.xml ファイルの内容です。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.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:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。
オプションとしてモバイルデバイスを選択すると、次の画像が表示されます。
今wifiボタンを無効にするをクリックしてください。サンプル出力は次のようになります-