Develop on Android
Android Lab Test
www.AndroidLabTest.com
Youku
By Bruno Delb
www.weibo.com/brunodelb
i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn
http://i.youku.com/brunoparis
Weibo
Officialsite
Lesson : The Wifi manager
The Wifi manager
• In this lesson, you will learn to use the Wifi manager.
• For this, you will use the Wifi manager WifiManager.
The Wifi manager
• Use getSystemService() to access to the Wifi manager.
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
• To start the scan of networks, call the method startScan().
wifiManager.startScan();
• To control the status active or inactive, use the methods isWifiEnabled() and
setWifiEnabled().
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
} else {
wifiManager.setWifiEnabled(true);
}
The Wifi manager
• To get notified when the search is finished, register a BroadcastReceiver.
Receiver receiver = new Receiver();
registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
registerReceiver(receiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
The Wifi manager
• This receiver receives the status in the action
(SCAN_RESULTS_AVAILABLE_ACTION). Then the SSIDs can be rad with the
method getScanResults() of the WifiManager.
public class Receiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
List<ScanResult> results = wifiManager.getScanResults();
for(int i=0;i<results.size();i++){
String ssid = results.get(i).SSID;
}
}
}
}
Layout main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/btnScan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Scan" />
<Button
android:id="@+id/btnActivate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Activate / disactivate" />
<EditText
android:id="@+id/et_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
Layout main.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SSID" />
<EditText
android:id="@+id/et_ssid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10" />
</LinearLayout>
File Main.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnScan = (Button)findViewById (R.id.btnScan);
btnScan.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
wifiManager.startScan();
}
});
Button btnEnable = (Button)this.findViewById(R.id.btnActivate);
btnEnable.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
et_ssid.setText("Disabled");
} else {
wifiManager.setWifiEnabled(true);
et_ssid.setText("Enabled");
}
}
});
File Main.java
et_ssid = (TextView)findViewById (R.id.et_ssid);
et_status = (TextView)findViewById (R.id.et_status);
wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
Receiver receiver = new Receiver();
registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
registerReceiver(receiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
public class Receiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
List<ScanResult> results = wifiManager.getScanResults();
String ssid = "";
for (int i = 0; i < results.size(); i++) {
ssid += results.get(i).SSID + ", ";
}
et_ssid.setText(ssid);
}
}
}
}
Project AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Test on your mobile
System_Wifi
Follow me on my channel PengYooTV …
On my Youku channel
http://i.youku.com/brunoparis
Who am I ?
Bruno Delb (www.delb.cn),
Author of the first french book of development of Java mobile application (2002),
Consultant, project manager and developer of social & mobile applications,
let’s talk about your needs ...
And on Weibo :
http://www.weibo.com/brunodelb

Android Lab Test : Using the WIFI (english)

  • 1.
    Develop on Android AndroidLab Test www.AndroidLabTest.com Youku By Bruno Delb www.weibo.com/brunodelb i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn http://i.youku.com/brunoparis Weibo Officialsite Lesson : The Wifi manager
  • 2.
    The Wifi manager •In this lesson, you will learn to use the Wifi manager. • For this, you will use the Wifi manager WifiManager.
  • 3.
    The Wifi manager •Use getSystemService() to access to the Wifi manager. WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); • To start the scan of networks, call the method startScan(). wifiManager.startScan(); • To control the status active or inactive, use the methods isWifiEnabled() and setWifiEnabled(). if (wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(false); } else { wifiManager.setWifiEnabled(true); }
  • 4.
    The Wifi manager •To get notified when the search is finished, register a BroadcastReceiver. Receiver receiver = new Receiver(); registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); registerReceiver(receiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
  • 5.
    The Wifi manager •This receiver receives the status in the action (SCAN_RESULTS_AVAILABLE_ACTION). Then the SSIDs can be rad with the method getScanResults() of the WifiManager. public class Receiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){ List<ScanResult> results = wifiManager.getScanResults(); for(int i=0;i<results.size();i++){ String ssid = results.get(i).SSID; } } } }
  • 6.
    Layout main.xml <?xml version="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/btnScan" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Scan" /> <Button android:id="@+id/btnActivate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Activate / disactivate" /> <EditText android:id="@+id/et_status" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" />
  • 7.
  • 8.
    File Main.java public voidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnScan = (Button)findViewById (R.id.btnScan); btnScan.setOnClickListener(new OnClickListener() { public void onClick(View v) { wifiManager.startScan(); } }); Button btnEnable = (Button)this.findViewById(R.id.btnActivate); btnEnable.setOnClickListener(new OnClickListener() { public void onClick(View v) { if(wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(false); et_ssid.setText("Disabled"); } else { wifiManager.setWifiEnabled(true); et_ssid.setText("Enabled"); } } });
  • 9.
    File Main.java et_ssid =(TextView)findViewById (R.id.et_ssid); et_status = (TextView)findViewById (R.id.et_status); wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); Receiver receiver = new Receiver(); registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); registerReceiver(receiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } public class Receiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (intent.getAction().equals (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { List<ScanResult> results = wifiManager.getScanResults(); String ssid = ""; for (int i = 0; i < results.size(); i++) { ssid += results.get(i).SSID + ", "; } et_ssid.setText(ssid); } } } }
  • 10.
    Project AndroidManifest.xml <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.WAKE_LOCK"/>
  • 11.
    Test on yourmobile System_Wifi
  • 12.
    Follow me onmy channel PengYooTV … On my Youku channel http://i.youku.com/brunoparis Who am I ? Bruno Delb (www.delb.cn), Author of the first french book of development of Java mobile application (2002), Consultant, project manager and developer of social & mobile applications, let’s talk about your needs ... And on Weibo : http://www.weibo.com/brunodelb