AdSense

AdSense3

Friday, 30 October 2015

Android Wifi Example

The android.net.wifi.WifiManager class can be used to manage the wifi connectivity. It can be used to add network, disable network, scan for access points, disconnect etc.

Android wifi example to enable and disable wifi

Let's see the simple example of wifi to enable and disable the wifi service.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="76dp"  
  14.         android:layout_marginTop="67dp"  
  15.         android:text="Enable Wifi" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/button2"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignLeft="@+id/button1"  
  22.         android:layout_below="@+id/button1"  
  23.         android:layout_marginTop="44dp"  
  24.         android:text="Disable Wifi" />  
  25.   
  26. </RelativeLayout>  

Activity class

File: MainActivity.java
  1. package com.example.wifi;  
  2.   
  3. import android.net.wifi.WifiManager;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     Button enableButton,disableButton;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.           
  19.         enableButton=(Button)findViewById(R.id.button1);  
  20.         disableButton=(Button)findViewById(R.id.button2);  
  21.           
  22.         enableButton.setOnClickListener(new OnClickListener(){  
  23.             public void onClick(View v){  
  24.                 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  25.                 wifi.setWifiEnabled(true);  
  26.             }  
  27.         });  
  28.         disableButton.setOnClickListener(new OnClickListener(){  
  29.             public void onClick(View v){  
  30.                 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  31.                 wifi.setWifiEnabled(false);  
  32.             }  
  33.         });  
  34.     }  
  35.   
  36.     @Override  
  37.     public boolean onCreateOptionsMenu(Menu menu) {  
  38.         // Inflate the menu; this adds items to the action bar if it is present.  
  39.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  40.         return true;  
  41.     }  
  42.   
  43. }  

Add Permission in AndroidManifest.xml

You need to add following permissions in AndroidManifest.xml file.
  1. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  2. <uses-permission android:name="android.permission.INTERNET" />  
  3. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>  

Output:

android wifi example output 1

Wednesday, 28 October 2015

Android Bluetooth List Paired Devices Example

The getBoundedDevices() method of BluetoothAdapter class provides a set containing list of all paired or bounded bluetooth devices.

In this example, we are checking if the bluetooth is turned off, if yes then turn it on and list all the paired devices.

activity_main.xml

Drag one textview from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="18dp"  
  14.         android:layout_marginTop="61dp"  
  15.         android:text="Showing Paired Devices:" />  
  16.   
  17. </RelativeLayout>  

Provide Permission

You need to provide following permissions in AndroidManifest.xml file.
  1. <uses-permission android:name="android.permission.BLUETOOTH" />  
  2. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  
The full code of AndroidManifest.xml file is given below.
File: AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:androclass="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.bluetoothshowpaired"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.     <uses-permission android:name="android.permission.BLUETOOTH" />  
  12.     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  
  13.       
  14.     <application  
  15.         android:allowBackup="true"  
  16.         android:icon="@drawable/ic_launcher"  
  17.         android:label="@string/app_name"  
  18.         android:theme="@style/AppTheme" >  
  19.         <activity  
  20.             android:name="com.example.bluetoothshowpaired.MainActivity"  
  21.             android:label="@string/app_name" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>  
  28.     </application>  
  29.   
  30. </manifest>  

Activity class

Let's write the code to provide the list of paired (bounded) bluetooth devices.
File: MainActivity.java
  1. package com.example.bluetoothshowpaired;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import java.util.Set;  
  7. import android.bluetooth.BluetoothAdapter;  
  8. import android.bluetooth.BluetoothDevice;  
  9. import android.content.Intent;  
  10. import android.widget.TextView;  
  11.    
  12. public class MainActivity extends Activity {  
  13.       TextView textview1;  
  14.       private static final int REQUEST_ENABLE_BT = 1;  
  15.       BluetoothAdapter btAdapter;   
  16.        
  17.       /** Called when the activity is first created. */  
  18.       @Override  
  19.       public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.        
  23.         textview1 = (TextView) findViewById(R.id.textView1);  
  24.            
  25.         // Getting the Bluetooth adapter  
  26.         btAdapter = BluetoothAdapter.getDefaultAdapter();  
  27.         textview1.append("\nAdapter: " + btAdapter);  
  28.            
  29.         CheckBluetoothState();  
  30.       }  
  31.            
  32.       /* It is called when an activity completes.*/  
  33.       @Override  
  34.       protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  35.         super.onActivityResult(requestCode, resultCode, data);  
  36.         if (requestCode == REQUEST_ENABLE_BT) {  
  37.           CheckBluetoothState();  
  38.         }  
  39.       }  
  40.        
  41.       @Override  
  42.       protected void onDestroy() {  
  43.         super.onDestroy();  
  44.       }  
  45.        
  46.       private void CheckBluetoothState() {  
  47.         // Checks for the Bluetooth support and then makes sure it is turned on  
  48.         // If it isn't turned on, request to turn it on  
  49.         // List paired devices  
  50.         if(btAdapter==null) {   
  51.           textview1.append("\nBluetooth NOT supported. Aborting.");  
  52.           return;  
  53.         } else {  
  54.           if (btAdapter.isEnabled()) {  
  55.             textview1.append("\nBluetooth is enabled...");  
  56.                
  57.             // Listing paired devices  
  58.             textview1.append("\nPaired Devices are:");  
  59.             Set<BluetoothDevice> devices = btAdapter.getBondedDevices();  
  60.             for (BluetoothDevice device : devices) {  
  61.               textview1.append("\n  Device: " + device.getName() + ", " + device);  
  62.             }  
  63.           } else {  
  64.             //Prompt user to turn on Bluetooth  
  65.             Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  66.             startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
  67.           }  
  68.         }  
  69.       }  
  70.            
  71.   
  72.     @Override  
  73.     public boolean onCreateOptionsMenu(Menu menu) {  
  74.         // Inflate the menu; this adds items to the action bar if it is present.  
  75.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  76.         return true;  
  77.     }  
  78.   
  79. }